bitcasts.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #ifndef FP16_BITCASTS_H
  4. #define FP16_BITCASTS_H
  5. #if defined(__cplusplus) && (__cplusplus >= 201103L)
  6. #include <cstdint>
  7. #elif !defined(__OPENCL_VERSION__)
  8. #include <stdint.h>
  9. #endif
  10. #if defined(__INTEL_COMPILER)
  11. #include <immintrin.h>
  12. #endif
  13. #if defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  14. #include <intrin.h>
  15. #endif
  16. static inline float fp32_from_bits(uint32_t w) {
  17. #if defined(__OPENCL_VERSION__)
  18. return as_float(w);
  19. #elif defined(__CUDA_ARCH__)
  20. return __uint_as_float((unsigned int) w);
  21. #elif defined(__INTEL_COMPILER)
  22. return _castu32_f32(w);
  23. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  24. return _CopyFloatFromInt32((__int32) w);
  25. #else
  26. union {
  27. uint32_t as_bits;
  28. float as_value;
  29. } fp32 = { w };
  30. return fp32.as_value;
  31. #endif
  32. }
  33. static inline uint32_t fp32_to_bits(float f) {
  34. #if defined(__OPENCL_VERSION__)
  35. return as_uint(f);
  36. #elif defined(__CUDA_ARCH__)
  37. return (uint32_t) __float_as_uint(f);
  38. #elif defined(__INTEL_COMPILER)
  39. return _castf32_u32(f);
  40. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  41. return (uint32_t) _CopyInt32FromFloat(f);
  42. #else
  43. union {
  44. float as_value;
  45. uint32_t as_bits;
  46. } fp32 = { f };
  47. return fp32.as_bits;
  48. #endif
  49. }
  50. static inline double fp64_from_bits(uint64_t w) {
  51. #if defined(__OPENCL_VERSION__)
  52. return as_double(w);
  53. #elif defined(__CUDA_ARCH__)
  54. return __longlong_as_double((long long) w);
  55. #elif defined(__INTEL_COMPILER)
  56. return _castu64_f64(w);
  57. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  58. return _CopyDoubleFromInt64((__int64) w);
  59. #else
  60. union {
  61. uint64_t as_bits;
  62. double as_value;
  63. } fp64 = { w };
  64. return fp64.as_value;
  65. #endif
  66. }
  67. static inline uint64_t fp64_to_bits(double f) {
  68. #if defined(__OPENCL_VERSION__)
  69. return as_ulong(f);
  70. #elif defined(__CUDA_ARCH__)
  71. return (uint64_t) __double_as_longlong(f);
  72. #elif defined(__INTEL_COMPILER)
  73. return _castf64_u64(f);
  74. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  75. return (uint64_t) _CopyInt64FromDouble(f);
  76. #else
  77. union {
  78. double as_value;
  79. uint64_t as_bits;
  80. } fp64 = { f };
  81. return fp64.as_bits;
  82. #endif
  83. }
  84. #endif /* FP16_BITCASTS_H */
  85. #else
  86. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  87. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)