Caffe2 - C++ API
A deep learning, cross platform ML framework
cvtsh_ss_bugfix.h
1 #pragma once
2 
3 // Apple clang was fixed in 8.1
4 #if defined(__apple_build_version__) && ((__clang_major__ < 8) || ((__clang_major__ == 8) && (__clang_minor__ < 1)))
5 #define __APPLE_NEED_FIX 1
6 #endif
7 
8 // Regular clang was fixed in 3.9
9 #if defined(__clang__) && (__clang_major__ < 4) && (__clang_minor__ < 9)
10 #define __CLANG_NEED_FIX 1
11 #endif
12 
13 #if __APPLE_NEED_FIX || __CLANG_NEED_FIX
14 
15 #include <emmintrin.h>
16 
17 // This version of clang has a bug that _cvtsh_ss is not defined, see
18 // https://reviews.llvm.org/D16177
19 static __inline float
20  __attribute__((__always_inline__, __nodebug__, __target__("f16c")))
21 _cvtsh_ss(unsigned short a)
22 {
23  __v8hi v = {(short)a, 0, 0, 0, 0, 0, 0, 0};
24  __v4sf r = __builtin_ia32_vcvtph2ps(v);
25  return r[0];
26 }
27 
28 #endif // __APPLE_NEED_FIX || __CLANG_NEED_FIX
29 
30 #undef __APPLE_NEED_FIX
31 #undef __CLANG_NEED_FIX
32 
33 #ifdef _MSC_VER
34 
35 // It seems that microsoft msvc does not have a _cvtsh_ss implementation so
36 // we will add a dummy version to it.
37 
38 static inline float
39 _cvtsh_ss(unsigned short x) {
40  union {
41  uint32_t intval;
42  float floatval;
43  } t1;
44  uint32_t t2, t3;
45  t1.intval = x & 0x7fff; // Non-sign bits
46  t2 = x & 0x8000; // Sign bit
47  t3 = x & 0x7c00; // Exponent
48  t1.intval <<= 13; // Align mantissa on MSB
49  t2 <<= 16; // Shift sign bit into position
50  t1.intval += 0x38000000; // Adjust bias
51  t1.intval = (t3 == 0 ? 0 : t1.intval); // Denormals-as-zero
52  t1.intval |= t2; // Re-insert sign bit
53  return t1.floatval;
54 }
55 
56 #endif // _MSC_VER