Faiss

- Main Page
- Namespaces
- Classes
- Files

- File List

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
fp16_emu.cu
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 #include "fp16_emu.cuh"
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 // Host functions for converting between FP32 and FP16 formats
66 // Paulius Micikevicius (pauliusm@nvidia.com)
67
68 half1 cpu_float2half_rn(float f)
69 {
70 half1 ret;
71
72 union {
73 float f;
74 unsigned u;
75 } un;
76
77 un.f = f;
78
79 unsigned x = un.u;
80 unsigned u = (x & 0x7fffffff), remainder, shift, lsb, lsb_s1, lsb_m1;
81 unsigned sign, exponent, mantissa;
82
83 // Get rid of +NaN/-NaN case first.
84 if (u > 0x7f800000) {
85 ret.x = 0x7fffU;
86 return ret;
87 }
88
89 sign = ((x >> 16) & 0x8000);
90
91 // Get rid of +Inf/-Inf, +0/-0.
92 if (u > 0x477fefff) {
93 ret.x = sign | 0x7c00U;
94 return ret;
95 }
96 if (u < 0x33000001) {
97 ret.x = (sign | 0x0000);
98 return ret;
99 }
100
101 exponent = ((u >> 23) & 0xff);
102 mantissa = (u & 0x7fffff);
103
104 if (exponent > 0x70) {
105 shift = 13;
106 exponent -= 0x70;
107 } else {
108 shift = 0x7e - exponent;
109 exponent = 0;
110 mantissa |= 0x800000;
111 }
112 lsb = (1 << shift);
113 lsb_s1 = (lsb >> 1);
114 lsb_m1 = (lsb - 1);
115
116 // Round to nearest even.
117 remainder = (mantissa & lsb_m1);
118 mantissa >>= shift;
119 if (remainder > lsb_s1 || (remainder == lsb_s1 && (mantissa & 0x1))) {
120 ++mantissa;
121 if (!(mantissa & 0x3ff)) {
122 ++exponent;
123 mantissa = 0;
124 }
125 }
126
127 ret.x = (sign | (exponent << 10) | mantissa);
128
129 return ret;
130 }
131
132
133 float cpu_half2float(half1 h)
134 {
135 unsigned sign = ((h.x >> 15) & 1);
136 unsigned exponent = ((h.x >> 10) & 0x1f);
137 unsigned mantissa = ((h.x & 0x3ff) << 13);
138
139 if (exponent == 0x1f) { /* NaN or Inf */
140 mantissa = (mantissa ? (sign = 0, 0x7fffff) : 0);
141 exponent = 0xff;
142 } else if (!exponent) { /* Denorm or Zero */
143 if (mantissa) {
144 unsigned int msb;
145 exponent = 0x71;
146 do {
147 msb = (mantissa & 0x400000);
148 mantissa <<= 1; /* normalize */
149 --exponent;
150 } while (!msb);
151 mantissa &= 0x7fffff; /* 1.mantissa is implicit */
152 }
153 } else {
154 exponent += 0x70;
155 }
156
157 union {
158 int i;
159 float f;
160 } un;
161
162 un.i = ((sign << 31) | (exponent << 23) | mantissa);
163
164 return un.f;
165 }
166
167 } } // namespace