File indexing completed on 2025-12-16 10:14:57
0001 #pragma once
0002 #ifndef FP16_BITCASTS_H
0003 #define FP16_BITCASTS_H
0004
0005 #if defined(__cplusplus) && (__cplusplus >= 201103L)
0006 #include <cstdint>
0007 #elif !defined(__OPENCL_VERSION__)
0008 #include <stdint.h>
0009 #endif
0010
0011 #if defined(__INTEL_COMPILER)
0012 #include <immintrin.h>
0013 #endif
0014
0015 #if defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
0016 #include <intrin.h>
0017 #endif
0018
0019
0020 static inline float fp32_from_bits(uint32_t w) {
0021 #if defined(__OPENCL_VERSION__)
0022 return as_float(w);
0023 #elif defined(__CUDA_ARCH__)
0024 return __uint_as_float((unsigned int) w);
0025 #elif defined(__INTEL_COMPILER)
0026 return _castu32_f32(w);
0027 #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
0028 return _CopyFloatFromInt32((__int32) w);
0029 #else
0030 union {
0031 uint32_t as_bits;
0032 float as_value;
0033 } fp32 = { w };
0034 return fp32.as_value;
0035 #endif
0036 }
0037
0038 static inline uint32_t fp32_to_bits(float f) {
0039 #if defined(__OPENCL_VERSION__)
0040 return as_uint(f);
0041 #elif defined(__CUDA_ARCH__)
0042 return (uint32_t) __float_as_uint(f);
0043 #elif defined(__INTEL_COMPILER)
0044 return _castf32_u32(f);
0045 #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
0046 return (uint32_t) _CopyInt32FromFloat(f);
0047 #else
0048 union {
0049 float as_value;
0050 uint32_t as_bits;
0051 } fp32 = { f };
0052 return fp32.as_bits;
0053 #endif
0054 }
0055
0056 static inline double fp64_from_bits(uint64_t w) {
0057 #if defined(__OPENCL_VERSION__)
0058 return as_double(w);
0059 #elif defined(__CUDA_ARCH__)
0060 return __longlong_as_double((long long) w);
0061 #elif defined(__INTEL_COMPILER)
0062 return _castu64_f64(w);
0063 #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
0064 return _CopyDoubleFromInt64((__int64) w);
0065 #else
0066 union {
0067 uint64_t as_bits;
0068 double as_value;
0069 } fp64 = { w };
0070 return fp64.as_value;
0071 #endif
0072 }
0073
0074 static inline uint64_t fp64_to_bits(double f) {
0075 #if defined(__OPENCL_VERSION__)
0076 return as_ulong(f);
0077 #elif defined(__CUDA_ARCH__)
0078 return (uint64_t) __double_as_longlong(f);
0079 #elif defined(__INTEL_COMPILER)
0080 return _castf64_u64(f);
0081 #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
0082 return (uint64_t) _CopyInt64FromDouble(f);
0083 #else
0084 union {
0085 double as_value;
0086 uint64_t as_bits;
0087 } fp64 = { f };
0088 return fp64.as_bits;
0089 #endif
0090 }
0091
0092 #endif