MCPcopy Create free account
hub / github.com/numpy/numpy / Half

Class Half

numpy/core/src/common/half.hpp:23–225  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

21/// This type is ensured to be 16-bit size.
22#if 1 // ndef __ARM_FP16_FORMAT_IEEE
23class Half final {
24 public:
25 /// Whether `Half` has a full native HW support.
26 static constexpr bool kNative = false;
27 /// Whether `Half` has a native HW support for single/double conversion.
28 template<typename T>
29 static constexpr bool kNativeConversion = (
30 (
31 std::is_same_v<T, float> &&
32 #if defined(NPY_HAVE_FP16) || defined(NPY_HAVE_VSX3)
33 true
34 #else
35 false
36 #endif
37 ) || (
38 std::is_same_v<T, double> &&
39 #if defined(NPY_HAVE_AVX512FP16) || (defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX3_HALF_DOUBLE))
40 true
41 #else
42 false
43 #endif
44 )
45 );
46
47 /// Default constructor. initialize nothing.
48 Half() = default;
49
50 /// Construct from float
51 /// If there are no hardware optimization available, rounding will always
52 /// be set to ties to even.
53 explicit Half(float f)
54 {
55 #if defined(NPY_HAVE_FP16)
56 __m128 mf = _mm_load_ss(&f);
57 bits_ = static_cast<uint16_t>(_mm_cvtsi128_si32(_mm_cvtps_ph(mf, _MM_FROUND_TO_NEAREST_INT)));
58 #elif defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX_ASM)
59 __vector float vf32 = vec_splats(f);
60 __vector unsigned short vf16;
61 __asm__ __volatile__ ("xvcvsphp %x0,%x1" : "=wa" (vf16) : "wa" (vf32));
62 #ifdef __BIG_ENDIAN__
63 bits_ = vec_extract(vf16, 1);
64 #else
65 bits_ = vec_extract(vf16, 0);
66 #endif
67 #else
68 bits_ = half_private::FromFloatBits(BitCast<uint32_t>(f));
69 #endif
70 }
71
72 /// Construct from double.
73 /// If there are no hardware optimization available, rounding will always
74 /// be set to ties to even.
75 explicit Half(double f)
76 {
77 #if defined(NPY_HAVE_AVX512FP16)
78 __m128d md = _mm_load_sd(&f);
79 bits_ = static_cast<uint16_t>(_mm_cvtsi128_si32(_mm_castph_si128(_mm_cvtpd_ph(md))));
80 #elif defined(NPY_HAVE_VSX3) && defined(NPY_HAVE_VSX3_HALF_DOUBLE)

Callers 4

npy_float_to_halfFunction · 0.85
npy_double_to_halfFunction · 0.85

Calls 2

ToFloatBitsFunction · 0.85
ToDoubleBitsFunction · 0.85

Tested by

no test coverage detected