Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
fp16_emu.cuh
1 /**
2  * Copyright (c) Facebook, Inc. and its affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 // from Nvidia cuDNN library samples; modified to compile within faiss
9 
10 #pragma once
11 
12 namespace faiss { namespace gpu {
13 
14 /*
15  * Copyright 1993-2014 NVIDIA Corporation. All rights reserved.
16  *
17  * NOTICE TO LICENSEE:
18  *
19  * This source code and/or documentation ("Licensed Deliverables") are
20  * subject to NVIDIA intellectual property rights under U.S. and
21  * international Copyright laws.
22  *
23  * These Licensed Deliverables contained herein is PROPRIETARY and
24  * CONFIDENTIAL to NVIDIA and is being provided under the terms and
25  * conditions of a form of NVIDIA software license agreement by and
26  * between NVIDIA and Licensee ("License Agreement") or electronically
27  * accepted by Licensee. Notwithstanding any terms or conditions to
28  * the contrary in the License Agreement, reproduction or disclosure
29  * of the Licensed Deliverables to any third party without the express
30  * written consent of NVIDIA is prohibited.
31  *
32  * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
33  * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
34  * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE. IT IS
35  * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
36  * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
37  * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
38  * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
39  * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
40  * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
41  * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
42  * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
44  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
45  * OF THESE LICENSED DELIVERABLES.
46  *
47  * U.S. Government End Users. These Licensed Deliverables are a
48  * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
49  * 1995), consisting of "commercial computer software" and "commercial
50  * computer software documentation" as such terms are used in 48
51  * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
52  * only as a commercial end item. Consistent with 48 C.F.R.12.212 and
53  * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
54  * U.S. Government End Users acquire the Licensed Deliverables with
55  * only those rights set forth herein.
56  *
57  * Any use of the Licensed Deliverables in individual and commercial
58  * software must include, in the user documentation and internal
59  * comments to the code, the above Disclaimer and U.S. Government End
60  * Users Notice.
61  */
62 
63 // Conversion from/to 16-bit floating point (half-precision).
64 
65 #define HLF_EPSILON 4.887581E-04
66 
67 typedef struct __align__(2) {
68  unsigned short x;
69 } half1;
70 
71 half1 cpu_float2half_rn(float f);
72 
73 float cpu_half2float(half1 h);
74 
75 static __inline__ __device__ __host__ half1 habs(half1 h)
76 {
77  h.x &= 0x7fffU;
78  return h;
79 }
80 
81 static __inline__ __device__ __host__ half1 hneg(half1 h)
82 {
83  h.x ^= 0x8000U;
84  return h;
85 }
86 
87 static __inline__ __device__ __host__ int ishnan(half1 h)
88 {
89  // When input is NaN, exponent is all ones and mantissa is non-zero.
90  return (h.x & 0x7c00U) == 0x7c00U && (h.x & 0x03ffU) != 0;
91 }
92 
93 static __inline__ __device__ __host__ int ishinf(half1 h)
94 {
95  // When input is +/- inf, exponent is all ones and mantissa is zero.
96  return (h.x & 0x7c00U) == 0x7c00U && (h.x & 0x03ffU) == 0;
97 }
98 
99 static __inline__ __device__ __host__ int ishequ(half1 x, half1 y)
100 {
101  return ishnan(x) == 0 && ishnan(y) == 0 && x.x == y.x;
102 }
103 
104 static __inline__ __device__ __host__ half1 hzero()
105 {
106  half1 ret;
107  ret.x = 0x0000U;
108  return ret;
109 }
110 
111 static __inline__ __device__ __host__ half1 hone()
112 {
113  half1 ret;
114  ret.x = 0x3c00U;
115  return ret;
116 }
117 
118 } } // namespace