util_half.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright 2011-2013 Blender Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __UTIL_HALF_H__
  17. #define __UTIL_HALF_H__
  18. #include "util/util_types.h"
  19. #include "util/util_math.h"
  20. #ifdef __KERNEL_SSE2__
  21. # include "util/util_simd.h"
  22. #endif
  23. CCL_NAMESPACE_BEGIN
  24. /* Half Floats */
  25. #ifdef __KERNEL_OPENCL__
  26. # define float4_store_half(h, f, scale) vstore_half4(f *(scale), 0, h);
  27. #else
  28. /* CUDA has its own half data type, no need to define then */
  29. # ifndef __KERNEL_CUDA__
  30. /* Implementing this as a class rather than a typedef so that the compiler can tell it apart from
  31. * unsigned shorts. */
  32. class half {
  33. public:
  34. half() : v(0)
  35. {
  36. }
  37. half(const unsigned short &i) : v(i)
  38. {
  39. }
  40. operator unsigned short()
  41. {
  42. return v;
  43. }
  44. half &operator=(const unsigned short &i)
  45. {
  46. v = i;
  47. return *this;
  48. }
  49. private:
  50. unsigned short v;
  51. };
  52. # endif
  53. struct half4 {
  54. half x, y, z, w;
  55. };
  56. # ifdef __KERNEL_CUDA__
  57. ccl_device_inline void float4_store_half(half *h, float4 f, float scale)
  58. {
  59. h[0] = __float2half(f.x * scale);
  60. h[1] = __float2half(f.y * scale);
  61. h[2] = __float2half(f.z * scale);
  62. h[3] = __float2half(f.w * scale);
  63. }
  64. # else
  65. ccl_device_inline void float4_store_half(half *h, float4 f, float scale)
  66. {
  67. # ifndef __KERNEL_SSE2__
  68. for (int i = 0; i < 4; i++) {
  69. /* optimized float to half for pixels:
  70. * assumes no negative, no nan, no inf, and sets denormal to 0 */
  71. union {
  72. uint i;
  73. float f;
  74. } in;
  75. float fscale = f[i] * scale;
  76. in.f = (fscale > 0.0f) ? ((fscale < 65504.0f) ? fscale : 65504.0f) : 0.0f;
  77. int x = in.i;
  78. int absolute = x & 0x7FFFFFFF;
  79. int Z = absolute + 0xC8000000;
  80. int result = (absolute < 0x38800000) ? 0 : Z;
  81. int rshift = (result >> 13);
  82. h[i] = (rshift & 0x7FFF);
  83. }
  84. # else
  85. /* same as above with SSE */
  86. ssef fscale = load4f(f) * scale;
  87. ssef x = min(max(fscale, 0.0f), 65504.0f);
  88. # ifdef __KERNEL_AVX2__
  89. ssei rpack = _mm_cvtps_ph(x, 0);
  90. # else
  91. ssei absolute = cast(x) & 0x7FFFFFFF;
  92. ssei Z = absolute + 0xC8000000;
  93. ssei result = andnot(absolute < 0x38800000, Z);
  94. ssei rshift = (result >> 13) & 0x7FFF;
  95. ssei rpack = _mm_packs_epi32(rshift, rshift);
  96. # endif
  97. _mm_storel_pi((__m64 *)h, _mm_castsi128_ps(rpack));
  98. # endif
  99. }
  100. ccl_device_inline float half_to_float(half h)
  101. {
  102. float f;
  103. *((int *)&f) = ((h & 0x8000) << 16) | (((h & 0x7c00) + 0x1C000) << 13) | ((h & 0x03FF) << 13);
  104. return f;
  105. }
  106. ccl_device_inline float4 half4_to_float4(half4 h)
  107. {
  108. float4 f;
  109. f.x = half_to_float(h.x);
  110. f.y = half_to_float(h.y);
  111. f.z = half_to_float(h.z);
  112. f.w = half_to_float(h.w);
  113. return f;
  114. }
  115. ccl_device_inline half float_to_half(float f)
  116. {
  117. const uint u = __float_as_uint(f);
  118. /* Sign bit, shifted to it's position. */
  119. uint sign_bit = u & 0x80000000;
  120. sign_bit >>= 16;
  121. /* Exponent. */
  122. uint exponent_bits = u & 0x7f800000;
  123. /* Non-sign bits. */
  124. uint value_bits = u & 0x7fffffff;
  125. value_bits >>= 13; /* Align mantissa on MSB. */
  126. value_bits -= 0x1c000; /* Adjust bias. */
  127. /* Flush-to-zero. */
  128. value_bits = (exponent_bits < 0x38800000) ? 0 : value_bits;
  129. /* Clamp-to-max. */
  130. value_bits = (exponent_bits > 0x47000000) ? 0x7bff : value_bits;
  131. /* Denormals-as-zero. */
  132. value_bits = (exponent_bits == 0 ? 0 : value_bits);
  133. /* Re-insert sign bit and return. */
  134. return (value_bits | sign_bit);
  135. }
  136. # endif
  137. #endif
  138. CCL_NAMESPACE_END
  139. #endif /* __UTIL_HALF_H__ */