FPHelper.hx 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package haxe.io;
  23. /**
  24. Helper that converts between floating point and binary representation.
  25. Always works in low-endian encoding.
  26. **/
  27. class FPHelper {
  28. #if neko_v21
  29. // stored in helper
  30. #elseif neko
  31. static var i64tmp = new sys.thread.Tls<Int64>();
  32. #elseif !(java || cpp)
  33. static var i64tmp = Int64.ofInt(0);
  34. static inline var LN2 = 0.6931471805599453; // Math.log(2)
  35. static inline function _i32ToFloat(i:Int):Float {
  36. var sign = 1 - ((i >>> 31) << 1);
  37. var e = (i >> 23) & 0xff;
  38. if (e == 255)
  39. return i & 0x7fffff == 0 ? (sign > 0 ? Math.POSITIVE_INFINITY : Math.NEGATIVE_INFINITY) : Math.NaN;
  40. var m = e == 0 ? (i & 0x7fffff) << 1 : (i & 0x7fffff) | 0x800000;
  41. return sign * m * Math.pow(2, e - 150);
  42. }
  43. static inline function _i64ToDouble(lo:Int, hi:Int):Float {
  44. var sign = 1 - ((hi >>> 31) << 1);
  45. var e = (hi >> 20) & 0x7ff;
  46. if (e == 2047)
  47. return lo == 0 && (hi & 0xFFFFF) == 0 ? (sign > 0 ? Math.POSITIVE_INFINITY : Math.NEGATIVE_INFINITY) : Math.NaN;
  48. var m = 2.220446049250313e-16 * ((hi & 0xFFFFF) * 4294967296. + (lo >>> 31) * 2147483648. + (lo & 0x7FFFFFFF));
  49. m = e == 0 ? m * 2.0 : m + 1.0;
  50. return sign * m * Math.pow(2, e - 1023);
  51. }
  52. static inline function _floatToI32(f:Float):Int {
  53. if (f == 0)
  54. return 0;
  55. var af = f < 0 ? -f : f;
  56. var exp = Math.floor(Math.log(af) / LN2);
  57. if (exp > 127) {
  58. return 0x7F800000;
  59. } else {
  60. if (exp <= -127) {
  61. exp = -127;
  62. af *= 7.1362384635298e+44; // af * 0.5 * 0x800000 / Math.pow(2, -127)
  63. } else {
  64. af = (af / Math.pow(2, exp) - 1.0) * 0x800000;
  65. }
  66. return (f < 0 ? 0x80000000 : 0) | ((exp + 127) << 23) | Math.round(af);
  67. }
  68. }
  69. static inline function _doubleToI64(v:Float):Int64@:privateAccess {
  70. var i64 = i64tmp;
  71. if (v == 0) {
  72. i64.set_low(0);
  73. i64.set_high(0);
  74. } else if (!Math.isFinite(v)) {
  75. i64.set_low(0);
  76. i64.set_high(v > 0 ? 0x7FF00000 : 0xFFF00000);
  77. } else {
  78. var av = v < 0 ? -v : v;
  79. var exp = Math.floor(Math.log(av) / LN2);
  80. if (exp > 1023) {
  81. i64.set_low(0xFFFFFFFF);
  82. i64.set_high(0x7FEFFFFF);
  83. } else {
  84. if (exp <= -1023) {
  85. exp = -1023;
  86. av = av / 2.2250738585072014e-308;
  87. } else {
  88. av = av / Math.pow(2, exp) - 1.0;
  89. }
  90. var sig = Math.fround(av * 4503599627370496.); // 2^52
  91. // Note: If "sig" is outside of the signed Int32 range, the result is unspecified in HL, Java and Neko.
  92. var sig_l = Std.int(sig);
  93. var sig_h = Std.int(sig / 4294967296.0);
  94. i64.set_low(sig_l);
  95. i64.set_high((v < 0 ? 0x80000000 : 0) | ((exp + 1023) << 20) | sig_h);
  96. }
  97. }
  98. return i64;
  99. }
  100. #end
  101. #if neko
  102. #if neko_v21
  103. static var helpers = new sys.thread.Tls<neko.NativeArray<Dynamic>>();
  104. #else
  105. static var helperf = new sys.thread.Tls<neko.NativeString>();
  106. static var helperd = new sys.thread.Tls<neko.NativeString>();
  107. static var _float_of_bytes = neko.Lib.load("std", "float_of_bytes", 2);
  108. static var _double_of_bytes = neko.Lib.load("std", "double_of_bytes", 2);
  109. static var _float_bytes = neko.Lib.load("std", "float_bytes", 2);
  110. static var _double_bytes = neko.Lib.load("std", "double_bytes", 2);
  111. #end
  112. #elseif flash
  113. static var helper = {
  114. var b = new flash.utils.ByteArray();
  115. b.endian = flash.utils.Endian.LITTLE_ENDIAN;
  116. b;
  117. }
  118. #elseif js
  119. static var helper = new js.lib.DataView(new js.lib.ArrayBuffer(8));
  120. #end
  121. #if neko_v21
  122. inline
  123. #end
  124. public static function i32ToFloat(i:Int):Float {
  125. #if neko
  126. #if neko_v21
  127. return untyped $itof(i, false);
  128. #else
  129. var helper = helperf.value;
  130. if (helper == null)
  131. helperf.value = helper = neko.NativeString.alloc(4);
  132. untyped $sset(helper, 0, i & 0xFF);
  133. untyped $sset(helper, 1, (i >> 8) & 0xFF);
  134. untyped $sset(helper, 2, (i >> 16) & 0xFF);
  135. untyped $sset(helper, 3, i >>> 24);
  136. return _float_of_bytes(helper, false);
  137. #end
  138. #elseif cpp
  139. return untyped __global__.__hxcpp_reinterpret_le_int32_as_float32(i);
  140. #elseif java
  141. return java.lang.Float.FloatClass.intBitsToFloat(i);
  142. #elseif flash
  143. var helper = helper;
  144. helper.position = 0;
  145. helper.writeUnsignedInt(i);
  146. helper.position = 0;
  147. return helper.readFloat();
  148. #elseif js
  149. helper.setInt32(0, i, true);
  150. return helper.getFloat32(0, true);
  151. #else
  152. return _i32ToFloat(i);
  153. #end
  154. }
  155. #if neko_v21
  156. inline
  157. #end
  158. public static function floatToI32(f:Float):Int {
  159. #if neko
  160. #if neko_v21
  161. return untyped $ftoi(f, false);
  162. #else
  163. var r = _float_bytes(f, false);
  164. return untyped $sget(r, 0) | ($sget(r, 1) << 8) | ($sget(r, 2) << 16) | ($sget(r, 3) << 24);
  165. #end
  166. #elseif cpp
  167. return untyped __global__.__hxcpp_reinterpret_float32_as_le_int32(f);
  168. #elseif java
  169. return java.lang.Float.FloatClass.floatToRawIntBits(f);
  170. #elseif flash
  171. var helper = helper;
  172. helper.position = 0;
  173. helper.writeFloat(f);
  174. helper.position = 0;
  175. return helper.readUnsignedInt();
  176. #elseif js
  177. helper.setFloat32(0, f, true);
  178. return helper.getInt32(0, true);
  179. #else
  180. return _floatToI32(f);
  181. #end
  182. }
  183. #if neko_v21
  184. inline
  185. #end
  186. public static function i64ToDouble(low:Int, high:Int):Float {
  187. #if neko
  188. #if neko_v21
  189. return untyped $itod(low, high, false);
  190. #else
  191. var helper = helperd.value;
  192. if (helper == null)
  193. helperd.value = helper = neko.NativeString.alloc(8);
  194. untyped $sset(helper, 0, low & 0xFF);
  195. untyped $sset(helper, 1, (low >> 8) & 0xFF);
  196. untyped $sset(helper, 2, (low >> 16) & 0xFF);
  197. untyped $sset(helper, 3, low >>> 24);
  198. untyped $sset(helper, 4, high & 0xFF);
  199. untyped $sset(helper, 5, (high >> 8) & 0xFF);
  200. untyped $sset(helper, 6, (high >> 16) & 0xFF);
  201. untyped $sset(helper, 7, high >>> 24);
  202. return _double_of_bytes(helper, false);
  203. #end
  204. #elseif cpp
  205. return untyped __global__.__hxcpp_reinterpret_le_int32s_as_float64(low, high);
  206. #elseif java
  207. return java.lang.Double.DoubleClass.longBitsToDouble(Int64.make(high, low));
  208. #elseif flash
  209. var helper = helper;
  210. helper.position = 0;
  211. helper.writeUnsignedInt(low);
  212. helper.writeUnsignedInt(high);
  213. helper.position = 0;
  214. return helper.readDouble();
  215. #elseif js
  216. helper.setInt32(0, low, true);
  217. helper.setInt32(4, high, true);
  218. return helper.getFloat64(0, true);
  219. #else
  220. return _i64ToDouble(low, high);
  221. #end
  222. }
  223. /**
  224. Returns an Int64 representing the bytes representation of the double precision IEEE float value.
  225. WARNING : for performance reason, the same Int64 value might be reused every time. Copy its low/high values before calling again.
  226. We still ensure that this is safe to use in a multithread environment
  227. **/
  228. public static function doubleToI64(v:Float):Int64 {
  229. #if neko
  230. #if neko_v21
  231. var helper = helpers.value;
  232. if (helper == null) {
  233. helpers.value = helper = neko.NativeArray.alloc(2);
  234. helper[0] = neko.NativeArray.alloc(2);
  235. helper[1] = haxe.Int64.ofInt(0);
  236. }
  237. var i64:haxe.Int64 = helper[1], int2 = helper[0];
  238. untyped $dtoi(v, int2, false);
  239. @:privateAccess {
  240. i64.set_low(int2[0]);
  241. i64.set_high(int2[1]);
  242. }
  243. return i64;
  244. #else
  245. var r = _double_bytes(v, false), i64 = i64tmp.value;
  246. if (i64 == null)
  247. i64 = i64tmp.value = haxe.Int64.ofInt(0);
  248. @:privateAccess {
  249. i64.set_low(untyped $sget(r, 0) | ($sget(r, 1) << 8) | ($sget(r, 2) << 16) | ($sget(r, 3) << 24));
  250. i64.set_high(untyped $sget(r, 4) | ($sget(r, 5) << 8) | ($sget(r, 6) << 16) | ($sget(r, 7) << 24));
  251. }
  252. return i64;
  253. #end
  254. #elseif cpp
  255. return Int64.make(untyped __global__.__hxcpp_reinterpret_float64_as_le_int32_high(v),
  256. untyped __global__.__hxcpp_reinterpret_float64_as_le_int32_low(v));
  257. #elseif java
  258. return java.lang.Double.DoubleClass.doubleToRawLongBits(v);
  259. #elseif flash
  260. var helper = helper;
  261. helper.position = 0;
  262. helper.writeDouble(v);
  263. helper.position = 0;
  264. var i64 = i64tmp;
  265. @:privateAccess {
  266. i64.set_low(cast helper.readUnsignedInt());
  267. i64.set_high(cast helper.readUnsignedInt());
  268. }
  269. return i64;
  270. #elseif js
  271. var i64 = i64tmp;
  272. helper.setFloat64(0, v, true);
  273. @:privateAccess {
  274. i64.set_low(helper.getInt32(0, true));
  275. i64.set_high(helper.getInt32(4, true));
  276. }
  277. return i64;
  278. #else
  279. return _doubleToI64(v);
  280. #end
  281. }
  282. }