Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

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