You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

318 rivejä
9.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #if defined __CELLOS_LV2__
  14. # if defined __SNC__
  15. # include <ppu_altivec_internals.h>
  16. # else
  17. # include <altivec.h>
  18. # endif
  19. #endif
  20. #include "core.h"
  21. using namespace std;
  22. namespace lol
  23. {
  24. /* These macros implement a finite iterator useful to build lookup
  25. * tables. For instance, S64(0) will call S1(x) for all values of x
  26. * between 0 and 63.
  27. * Due to the exponential behaviour of the calls, the stress on the
  28. * compiler may be important. */
  29. #define S4(x) S1((x)), S1((x)+1), S1((x)+2), S1((x)+3)
  30. #define S16(x) S4((x)), S4((x)+4), S4((x)+8), S4((x)+12)
  31. #define S64(x) S16((x)), S16((x)+16), S16((x)+32), S16((x)+48)
  32. #define S256(x) S64((x)), S64((x)+64), S64((x)+128), S64((x)+192)
  33. #define S1024(x) S256((x)), S256((x)+256), S256((x)+512), S256((x)+768)
  34. /* Lookup table-based algorithm from “Fast Half Float Conversions”
  35. * by Jeroen van der Zijp, November 2008. No rounding is performed,
  36. * and some NaN values may be incorrectly converted to Inf. */
  37. static inline uint16_t float_to_half_nobranch(uint32_t x)
  38. {
  39. static uint16_t const basetable[512] =
  40. {
  41. #define S1(i) (((i) < 103) ? 0x0000 : \
  42. ((i) < 113) ? 0x0400 >> (113 - (i)) : \
  43. ((i) < 143) ? ((i) - 112) << 10 : 0x7c00)
  44. S256(0),
  45. #undef S1
  46. #define S1(i) (0x8000 | (((i) < 103) ? 0x0000 : \
  47. ((i) < 113) ? 0x0400 >> (113 - (i)) : \
  48. ((i) < 143) ? ((i) - 112) << 10 : 0x7c00))
  49. S256(0),
  50. #undef S1
  51. };
  52. static uint8_t const shifttable[512] =
  53. {
  54. #define S1(i) (((i) < 103) ? 24 : \
  55. ((i) < 113) ? 126 - (i) : \
  56. ((i) < 143 || (i) == 255) ? 13 : 24)
  57. S256(0), S256(0),
  58. #undef S1
  59. };
  60. uint16_t bits = basetable[(x >> 23) & 0x1ff];
  61. bits |= (x & 0x007fffff) >> shifttable[(x >> 23) & 0x1ff];
  62. return bits;
  63. }
  64. /* This method is faster than the OpenEXR implementation (very often
  65. * used, eg. in Ogre), with the additional benefit of rounding, inspired
  66. * by James Tursa’s half-precision code. */
  67. static inline uint16_t float_to_half_branch(uint32_t x)
  68. {
  69. uint16_t bits = (x >> 16) & 0x8000; /* Get the sign */
  70. uint16_t m = (x >> 12) & 0x07ff; /* Keep one extra bit for rounding */
  71. unsigned int e = (x >> 23) & 0xff; /* Using int is faster here */
  72. /* If zero, or denormal, or exponent underflows too much for a denormal
  73. * half, return signed zero. */
  74. if (e < 103)
  75. return bits;
  76. /* If NaN, return NaN. If Inf or exponent overflow, return Inf. */
  77. if (e > 142)
  78. {
  79. bits |= 0x7c00u;
  80. /* If exponent was 0xff and one mantissa bit was set, it means NaN,
  81. * not Inf, so make sure we set one mantissa bit too. */
  82. bits |= e == 255 && (x & 0x007fffffu);
  83. return bits;
  84. }
  85. /* If exponent underflows but not too much, return a denormal */
  86. if (e < 113)
  87. {
  88. m |= 0x0800u;
  89. /* Extra rounding may overflow and set mantissa to 0 and exponent
  90. * to 1, which is OK. */
  91. bits |= (m >> (114 - e)) + ((m >> (113 - e)) & 1);
  92. return bits;
  93. }
  94. bits |= ((e - 112) << 10) | (m >> 1);
  95. /* Extra rounding. An overflow will set mantissa to 0 and increment
  96. * the exponent, which is OK. */
  97. bits += m & 1;
  98. return bits;
  99. }
  100. #if 0
  101. static inline void float_to_half_vector(half *dst, float const *src)
  102. {
  103. vector unsigned int const v7 = vec_splat_u32(7);
  104. vector unsigned short const v6 = vec_splat_u16(6);
  105. #if _XBOX
  106. vector signed short const v9 = vec_splat_u16(9);
  107. vector unsigned short const v10 = vec_splat_u16(10);
  108. #else
  109. vector signed short const v0x0040 = {
  110. 0x0040, 0x0040, 0x0040, 0x0040, 0x0040, 0x0040, 0x0040, 0x0040};
  111. vector unsigned short const v0x0400 = {
  112. 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400, 0x0400};
  113. #endif
  114. vector unsigned char const shuffle_high = {
  115. 0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20, 21, 24, 25, 28, 29};
  116. vector unsigned char const shuffle_low = {
  117. 2, 3, 6, 7, 10, 11, 14, 15, 18, 19, 22, 23, 26, 27, 30, 31};
  118. vector unsigned char const v0xbf70 = {
  119. 0xbf, 0x70, 0xbf, 0x70, 0xbf, 0x70, 0xbf, 0x70,
  120. 0xbf, 0x70, 0xbf, 0x70, 0xbf, 0x70, 0xbf, 0x70};
  121. vector unsigned short v_mant, v_ret;
  122. vector signed short v_exp;
  123. vector unsigned int in0 = (vector unsigned int)vec_ld(0, src);
  124. vector unsigned int in1 = (vector unsigned int)vec_ld(16, src);
  125. in0 = vec_sra(in0, v7);
  126. in1 = vec_sra(in1, v7);
  127. v_exp = (vector signed short)vec_perm(in0, in1, shuffle_high);
  128. v_mant = (vector unsigned short)vec_perm(in0, in1, shuffle_low);
  129. v_exp = (vector signed short)vec_subs((vector unsigned char)v_exp, v0xbf70);
  130. #if _XBOX
  131. v_ret = (vector unsigned short)vec_or(v_exp, vec_sr(v_exp, v9));
  132. #else
  133. v_ret = (vector unsigned short)vec_madds(v_exp, v0x0040, v_exp);
  134. #endif
  135. v_mant = vec_sr(v_mant, v6);
  136. #if _XBOX
  137. v_ret = vec_or(v_mant, vec_sl(v_ret, v10));
  138. #else
  139. v_ret = vec_mladd(v_ret, v0x0400, v_mant);
  140. #endif
  141. vec_st(v_ret, 0, (uint16_t *)dst);
  142. }
  143. #endif
  144. static int const shifttable[32] =
  145. {
  146. 23, 14, 22, 0, 0, 0, 21, 0, 0, 0, 0, 0, 0, 0, 20, 0,
  147. 15, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 17, 0, 18, 19, 0,
  148. };
  149. static uint32_t const shiftmagic = 0x07c4acddu;
  150. /* Lookup table-based algorithm from “Fast Half Float Conversions”
  151. * by Jeroen van der Zijp, November 2008. Tables are generated using
  152. * the C++ preprocessor, thanks to a branchless implementation also
  153. * used in half_to_float_branch(). This code is very fast when performing
  154. * conversions on arrays of values. */
  155. static inline uint32_t half_to_float_nobranch(uint16_t x)
  156. {
  157. #define M3(i) ((i) | ((i) >> 1))
  158. #define M7(i) (M3(i) | (M3(i) >> 2))
  159. #define MF(i) (M7(i) | (M7(i) >> 4))
  160. #define MFF(i) (MF(i) | (MF(i) >> 8))
  161. #define E(i) shifttable[(unsigned int)(MFF(i) * shiftmagic) >> 27]
  162. static uint32_t const mantissatable[2048] =
  163. {
  164. #define S1(i) (((i) == 0) ? 0 : ((125 - E(i)) << 23) + ((i) << E(i)))
  165. S1024(0),
  166. #undef S1
  167. #define S1(i) (0x38000000u + ((i) << 13))
  168. S1024(0),
  169. #undef S1
  170. };
  171. static uint32_t const exponenttable[64] =
  172. {
  173. #define S1(i) (((i) == 0) ? 0 : \
  174. ((i) < 31) ? ((i) << 23) : \
  175. ((i) == 31) ? 0x47800000u : \
  176. ((i) == 32) ? 0x80000000u : \
  177. ((i) < 63) ? (0x80000000u + (((i) - 32) << 23)) : 0xc7800000)
  178. S64(0),
  179. #undef S1
  180. };
  181. static int const offsettable[64] =
  182. {
  183. #define S1(i) (((i) == 0 || (i) == 32) ? 0 : 1024)
  184. S64(0),
  185. #undef S1
  186. };
  187. return mantissatable[offsettable[x >> 10] + (x & 0x3ff)]
  188. + exponenttable[x >> 10];
  189. }
  190. /* This algorithm is similar to the OpenEXR implementation, except it
  191. * uses branchless code in the denormal path. This is slower than the
  192. * table version, but will be more friendly to the cache for occasional
  193. * uses. */
  194. static inline uint32_t half_to_float_branch(uint16_t x)
  195. {
  196. uint32_t s = (x & 0x8000u) << 16;
  197. if ((x & 0x7fffu) == 0)
  198. return (uint32_t)x << 16;
  199. uint32_t e = x & 0x7c00u;
  200. uint32_t m = x & 0x03ffu;
  201. if (e == 0)
  202. {
  203. uint32_t v = m | (m >> 1);
  204. v |= v >> 2;
  205. v |= v >> 4;
  206. v |= v >> 8;
  207. e = shifttable[(v * shiftmagic) >> 27];
  208. /* We don't have to remove the 10th mantissa bit because it gets
  209. * added to our underestimated exponent. */
  210. return s | (((125 - e) << 23) + (m << e));
  211. }
  212. if (e == 0x7c00u)
  213. {
  214. /* The amd64 pipeline likes the if() better than a ternary operator
  215. * or any other trick I could find. --sam */
  216. if (m == 0)
  217. return s | 0x7f800000u;
  218. return s | 0x7fc00000u;
  219. }
  220. return s | (((e >> 10) + 112) << 23) | (m << 13);
  221. }
  222. /* Constructor from float. Uses the non-branching version because benchmarks
  223. * indicate it is about 80% faster on amd64, and 20% faster on the PS3. The
  224. * penalty of loading the lookup tables does not seem important. */
  225. half half::makefast(float f)
  226. {
  227. union { float f; uint32_t x; } u = { f };
  228. return makebits(float_to_half_nobranch(u.x));
  229. }
  230. /* Constructor from float with better precision. */
  231. half half::makeaccurate(float f)
  232. {
  233. union { float f; uint32_t x; } u = { f };
  234. return makebits(float_to_half_branch(u.x));
  235. }
  236. /* Cast to float. Uses the branching version because loading the tables
  237. * for only one value is going to be cache-expensive. */
  238. float half::tofloat(half h)
  239. {
  240. union { float f; uint32_t x; } u;
  241. u.x = half_to_float_branch(h.bits);
  242. return u.f;
  243. }
  244. size_t half::convert(half *dst, float const *src, size_t nelem)
  245. {
  246. for (size_t i = 0; i < nelem; i++)
  247. {
  248. union { float f; uint32_t x; } u;
  249. u.f = *src++;
  250. *dst++ = makebits(float_to_half_nobranch(u.x));
  251. #if 0
  252. /* AltiVec code. Will work one day. */
  253. float_to_half_vector(dst, src);
  254. src += 8;
  255. dst += 8;
  256. i += 7;
  257. #endif
  258. }
  259. return nelem;
  260. }
  261. size_t half::convert(float *dst, half const *src, size_t nelem)
  262. {
  263. for (size_t i = 0; i < nelem; i++)
  264. {
  265. union { float f; uint32_t x; } u;
  266. #if !defined __CELLOS_LV2__
  267. /* This code is really too slow on the PS3, even with the denormal
  268. * handling stripped off. */
  269. u.x = half_to_float_nobranch((*src++).bits);
  270. #else
  271. u.x = half_to_float_branch((*src++).bits);
  272. #endif
  273. *dst++ = u.f;
  274. }
  275. return nelem;
  276. }
  277. } /* namespace lol */