Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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