Faiss
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
ConversionOperators.cuh
1 /**
2  * Copyright (c) 2015-present, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under the CC-by-NC license found in the
6  * LICENSE file in the root directory of this source tree.
7  */
8 
9 // Copyright 2004-present Facebook. All Rights Reserved.
10 
11 #pragma once
12 
13 #include <cuda.h>
14 #include "Float16.cuh"
15 
16 namespace faiss { namespace gpu {
17 
18 //
19 // Conversion utilities
20 //
21 
22 template <typename T>
23 struct ConvertTo {
24 };
25 
26 template <>
27 struct ConvertTo<float> {
28  static inline __device__ float to(float v) { return v; }
29 #ifdef FAISS_USE_FLOAT16
30  static inline __device__ float to(half v) { return __half2float(v); }
31 #endif
32 };
33 
34 template <>
35 struct ConvertTo<float2> {
36  static inline __device__ float2 to(float2 v) { return v; }
37 #ifdef FAISS_USE_FLOAT16
38  static inline __device__ float2 to(half2 v) { return __half22float2(v); }
39 #endif
40 };
41 
42 template <>
43 struct ConvertTo<float4> {
44  static inline __device__ float4 to(float4 v) { return v; }
45 #ifdef FAISS_USE_FLOAT16
46  static inline __device__ float4 to(Half4 v) { return half4ToFloat4(v); }
47 #endif
48 };
49 
50 #ifdef FAISS_USE_FLOAT16
51 template <>
52 struct ConvertTo<half> {
53  static inline __device__ half to(float v) { return __float2half(v); }
54  static inline __device__ half to(half v) { return v; }
55 };
56 
57 template <>
58 struct ConvertTo<half2> {
59  static inline __device__ half2 to(float2 v) { return __float22half2_rn(v); }
60  static inline __device__ half2 to(half2 v) { return v; }
61 };
62 
63 template <>
64 struct ConvertTo<Half4> {
65  static inline __device__ Half4 to(float4 v) { return float4ToHalf4(v); }
66  static inline __device__ Half4 to(Half4 v) { return v; }
67 };
68 #endif
69 
70 
71 } } // namespace