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