xref: /aosp_15_r20/external/pytorch/c10/util/floating_point_utils.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 #pragma once
2 
3 #include <c10/macros/Macros.h>
4 #include <cstdint>
5 
6 namespace c10::detail {
7 
fp32_from_bits(uint32_t w)8 C10_HOST_DEVICE inline float fp32_from_bits(uint32_t w) {
9 #if defined(__OPENCL_VERSION__)
10   return as_float(w);
11 #elif defined(__CUDA_ARCH__)
12   return __uint_as_float((unsigned int)w);
13 #elif defined(__INTEL_COMPILER)
14   return _castu32_f32(w);
15 #else
16   union {
17     uint32_t as_bits;
18     float as_value;
19   } fp32 = {w};
20   return fp32.as_value;
21 #endif
22 }
23 
fp32_to_bits(float f)24 C10_HOST_DEVICE inline uint32_t fp32_to_bits(float f) {
25 #if defined(__OPENCL_VERSION__)
26   return as_uint(f);
27 #elif defined(__CUDA_ARCH__)
28   return (uint32_t)__float_as_uint(f);
29 #elif defined(__INTEL_COMPILER)
30   return _castf32_u32(f);
31 #else
32   union {
33     float as_value;
34     uint32_t as_bits;
35   } fp32 = {f};
36   return fp32.as_bits;
37 #endif
38 }
39 
40 } // namespace c10::detail
41