koffi 0.9.2 → 0.9.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (416) hide show
  1. package/CMakeLists.txt +60 -60
  2. package/README.md +163 -153
  3. package/package.json +19 -18
  4. package/src/call.hh +27 -27
  5. package/src/call_arm64.cc +482 -482
  6. package/src/call_arm64_fwd.S +115 -115
  7. package/src/call_x64_sysv.cc +477 -477
  8. package/src/call_x64_sysv_fwd.S +131 -131
  9. package/src/call_x64_win.cc +243 -243
  10. package/src/call_x64_win_fwd.asm +105 -105
  11. package/src/call_x86.cc +259 -259
  12. package/src/call_x86_fwd.S +48 -48
  13. package/src/call_x86_fwd.asm +50 -50
  14. package/src/ffi.cc +504 -504
  15. package/src/ffi.hh +135 -135
  16. package/src/util.cc +296 -296
  17. package/src/util.hh +80 -80
  18. package/vendor/dragonbox/CMakeLists.txt +122 -122
  19. package/vendor/dragonbox/LICENSE-Apache2-LLVM +218 -218
  20. package/vendor/dragonbox/LICENSE-Boost +23 -23
  21. package/vendor/dragonbox/README.md +277 -277
  22. package/vendor/dragonbox/cmake/dragonboxConfig.cmake +1 -1
  23. package/vendor/dragonbox/include/dragonbox/dragonbox.h +2670 -2670
  24. package/vendor/dragonbox/include/dragonbox/dragonbox_to_chars.h +108 -108
  25. package/vendor/dragonbox/other_files/unknown_win64_vc2019.html +539 -539
  26. package/vendor/dragonbox/source/dragonbox_to_chars.cpp +303 -303
  27. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/CMakeLists.txt +23 -23
  28. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/fp_to_chars.cpp +238 -238
  29. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/fp_to_chars.h +95 -95
  30. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/grisu_exact.h +2666 -2666
  31. package/vendor/dragonbox/subproject/3rdparty/ryu/CMakeLists.txt +16 -16
  32. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/common.h +114 -114
  33. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s.c +509 -509
  34. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s_full_table.h +367 -367
  35. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s_intrinsics.h +357 -357
  36. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/digit_table.h +35 -35
  37. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s.c +345 -345
  38. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s_full_table.h +55 -55
  39. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s_intrinsics.h +128 -128
  40. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/ryu.h +46 -46
  41. package/vendor/dragonbox/subproject/3rdparty/schubfach/CMakeLists.txt +21 -21
  42. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_32.cc +699 -699
  43. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_32.h +31 -31
  44. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_64.cc +1354 -1354
  45. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_64.h +31 -31
  46. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/example_shaded_plots.m +68 -68
  47. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/license.txt +25 -25
  48. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_distribution.m +92 -92
  49. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_distribution_prctile.m +121 -121
  50. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_histogram_shaded.m +99 -99
  51. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_shaded.m +93 -93
  52. package/vendor/dragonbox/subproject/benchmark/CMakeLists.txt +64 -64
  53. package/vendor/dragonbox/subproject/benchmark/include/benchmark.h +40 -40
  54. package/vendor/dragonbox/subproject/benchmark/matlab/plot_benchmarks.m +21 -21
  55. package/vendor/dragonbox/subproject/benchmark/matlab/plot_digit_benchmark.m +78 -78
  56. package/vendor/dragonbox/subproject/benchmark/matlab/plot_uniform_benchmark.m +95 -95
  57. package/vendor/dragonbox/subproject/benchmark/source/benchmark.cpp +237 -237
  58. package/vendor/dragonbox/subproject/benchmark/source/dragonbox.cpp +30 -30
  59. package/vendor/dragonbox/subproject/benchmark/source/grisu_exact.cpp +36 -36
  60. package/vendor/dragonbox/subproject/benchmark/source/ryu.cpp +27 -27
  61. package/vendor/dragonbox/subproject/benchmark/source/schubfach.cpp +31 -31
  62. package/vendor/dragonbox/subproject/common/CMakeLists.txt +41 -41
  63. package/vendor/dragonbox/subproject/common/include/best_rational_approx.h +96 -96
  64. package/vendor/dragonbox/subproject/common/include/big_uint.h +217 -217
  65. package/vendor/dragonbox/subproject/common/include/continued_fractions.h +173 -173
  66. package/vendor/dragonbox/subproject/common/include/good_rational_approx.h +266 -266
  67. package/vendor/dragonbox/subproject/common/include/random_float.h +182 -182
  68. package/vendor/dragonbox/subproject/common/include/rational_continued_fractions.h +56 -56
  69. package/vendor/dragonbox/subproject/common/source/big_uint.cpp +601 -601
  70. package/vendor/dragonbox/subproject/meta/CMakeLists.txt +40 -40
  71. package/vendor/dragonbox/subproject/meta/results/binary32_generated_cache.txt +81 -81
  72. package/vendor/dragonbox/subproject/meta/results/binary64_compressed_cache_error_table.txt +9 -9
  73. package/vendor/dragonbox/subproject/meta/results/binary64_generated_cache.txt +622 -622
  74. package/vendor/dragonbox/subproject/meta/source/generate_cache.cpp +126 -126
  75. package/vendor/dragonbox/subproject/meta/source/live_test.cpp +81 -81
  76. package/vendor/dragonbox/subproject/meta/source/perf_test.cpp +104 -104
  77. package/vendor/dragonbox/subproject/meta/source/sandbox.cpp +20 -20
  78. package/vendor/dragonbox/subproject/test/CMakeLists.txt +69 -69
  79. package/vendor/dragonbox/subproject/test/results/binary32.csv +255 -255
  80. package/vendor/dragonbox/subproject/test/results/binary64.csv +2047 -2047
  81. package/vendor/dragonbox/subproject/test/results/plot_required_bits.m +17 -17
  82. package/vendor/dragonbox/subproject/test/source/test_all_shorter_interval_cases.cpp +88 -88
  83. package/vendor/dragonbox/subproject/test/source/uniform_random_test.cpp +95 -95
  84. package/vendor/dragonbox/subproject/test/source/verify_cache_precision.cpp +337 -337
  85. package/vendor/dragonbox/subproject/test/source/verify_compressed_cache.cpp +154 -154
  86. package/vendor/dragonbox/subproject/test/source/verify_fast_multiplication.cpp +168 -168
  87. package/vendor/dragonbox/subproject/test/source/verify_log_computation.cpp +251 -251
  88. package/vendor/dragonbox/subproject/test/source/verify_magic_division.cpp +113 -113
  89. package/vendor/libcc/libcc.cc +7651 -7651
  90. package/vendor/libcc/libcc.hh +4312 -4312
  91. package/vendor/node-addon-api/CHANGELOG.md +859 -859
  92. package/vendor/node-addon-api/CODE_OF_CONDUCT.md +4 -4
  93. package/vendor/node-addon-api/CONTRIBUTING.md +93 -93
  94. package/vendor/node-addon-api/LICENSE.md +12 -12
  95. package/vendor/node-addon-api/README.md +293 -293
  96. package/vendor/node-addon-api/appveyor.yml +37 -37
  97. package/vendor/node-addon-api/benchmark/README.md +47 -47
  98. package/vendor/node-addon-api/benchmark/binding.gyp +25 -25
  99. package/vendor/node-addon-api/benchmark/function_args.cc +217 -217
  100. package/vendor/node-addon-api/benchmark/function_args.js +60 -60
  101. package/vendor/node-addon-api/benchmark/index.js +34 -34
  102. package/vendor/node-addon-api/benchmark/property_descriptor.cc +91 -91
  103. package/vendor/node-addon-api/benchmark/property_descriptor.js +37 -37
  104. package/vendor/node-addon-api/common.gypi +21 -21
  105. package/vendor/node-addon-api/doc/addon.md +163 -163
  106. package/vendor/node-addon-api/doc/array.md +81 -81
  107. package/vendor/node-addon-api/doc/array_buffer.md +155 -155
  108. package/vendor/node-addon-api/doc/async_context.md +86 -86
  109. package/vendor/node-addon-api/doc/async_operations.md +31 -31
  110. package/vendor/node-addon-api/doc/async_worker.md +427 -427
  111. package/vendor/node-addon-api/doc/async_worker_variants.md +557 -557
  112. package/vendor/node-addon-api/doc/bigint.md +97 -97
  113. package/vendor/node-addon-api/doc/boolean.md +68 -68
  114. package/vendor/node-addon-api/doc/buffer.md +150 -150
  115. package/vendor/node-addon-api/doc/callback_scope.md +54 -54
  116. package/vendor/node-addon-api/doc/callbackinfo.md +97 -97
  117. package/vendor/node-addon-api/doc/checker-tool.md +32 -32
  118. package/vendor/node-addon-api/doc/class_property_descriptor.md +115 -115
  119. package/vendor/node-addon-api/doc/cmake-js.md +68 -68
  120. package/vendor/node-addon-api/doc/conversion-tool.md +27 -27
  121. package/vendor/node-addon-api/doc/creating_a_release.md +62 -62
  122. package/vendor/node-addon-api/doc/dataview.md +248 -248
  123. package/vendor/node-addon-api/doc/date.md +68 -68
  124. package/vendor/node-addon-api/doc/env.md +196 -196
  125. package/vendor/node-addon-api/doc/error.md +120 -120
  126. package/vendor/node-addon-api/doc/error_handling.md +254 -254
  127. package/vendor/node-addon-api/doc/escapable_handle_scope.md +80 -80
  128. package/vendor/node-addon-api/doc/external.md +63 -63
  129. package/vendor/node-addon-api/doc/function.md +402 -402
  130. package/vendor/node-addon-api/doc/function_reference.md +238 -238
  131. package/vendor/node-addon-api/doc/generator.md +13 -13
  132. package/vendor/node-addon-api/doc/handle_scope.md +63 -63
  133. package/vendor/node-addon-api/doc/hierarchy.md +91 -91
  134. package/vendor/node-addon-api/doc/instance_wrap.md +408 -408
  135. package/vendor/node-addon-api/doc/maybe.md +76 -76
  136. package/vendor/node-addon-api/doc/memory_management.md +27 -27
  137. package/vendor/node-addon-api/doc/name.md +29 -29
  138. package/vendor/node-addon-api/doc/node-gyp.md +82 -82
  139. package/vendor/node-addon-api/doc/number.md +163 -163
  140. package/vendor/node-addon-api/doc/object.md +432 -432
  141. package/vendor/node-addon-api/doc/object_lifetime_management.md +83 -83
  142. package/vendor/node-addon-api/doc/object_reference.md +117 -117
  143. package/vendor/node-addon-api/doc/object_wrap.md +561 -561
  144. package/vendor/node-addon-api/doc/prebuild_tools.md +16 -16
  145. package/vendor/node-addon-api/doc/promises.md +79 -79
  146. package/vendor/node-addon-api/doc/property_descriptor.md +286 -286
  147. package/vendor/node-addon-api/doc/propertylvalue.md +50 -50
  148. package/vendor/node-addon-api/doc/range_error.md +59 -59
  149. package/vendor/node-addon-api/doc/reference.md +113 -113
  150. package/vendor/node-addon-api/doc/setup.md +110 -110
  151. package/vendor/node-addon-api/doc/string.md +93 -93
  152. package/vendor/node-addon-api/doc/symbol.md +60 -60
  153. package/vendor/node-addon-api/doc/threadsafe.md +121 -121
  154. package/vendor/node-addon-api/doc/threadsafe_function.md +290 -290
  155. package/vendor/node-addon-api/doc/type_error.md +59 -59
  156. package/vendor/node-addon-api/doc/typed_array.md +78 -78
  157. package/vendor/node-addon-api/doc/typed_array_of.md +137 -137
  158. package/vendor/node-addon-api/doc/typed_threadsafe_function.md +306 -306
  159. package/vendor/node-addon-api/doc/value.md +340 -340
  160. package/vendor/node-addon-api/doc/version_management.md +43 -43
  161. package/vendor/node-addon-api/except.gypi +25 -25
  162. package/vendor/node-addon-api/index.js +11 -11
  163. package/vendor/node-addon-api/napi-inl.deprecated.h +192 -192
  164. package/vendor/node-addon-api/napi-inl.h +6209 -6209
  165. package/vendor/node-addon-api/napi.h +2983 -2983
  166. package/vendor/node-addon-api/node_api.gyp +9 -9
  167. package/vendor/node-addon-api/noexcept.gypi +26 -26
  168. package/vendor/node-addon-api/package-support.json +21 -21
  169. package/vendor/node-addon-api/package.json +399 -399
  170. package/vendor/node-addon-api/test/README.md +91 -91
  171. package/vendor/node-addon-api/test/addon.cc +36 -36
  172. package/vendor/node-addon-api/test/addon.js +11 -11
  173. package/vendor/node-addon-api/test/addon_build/index.js +49 -49
  174. package/vendor/node-addon-api/test/addon_build/tpl/addon.cc +17 -17
  175. package/vendor/node-addon-api/test/addon_build/tpl/binding.gyp +62 -62
  176. package/vendor/node-addon-api/test/addon_build/tpl/index.js +9 -9
  177. package/vendor/node-addon-api/test/addon_build/tpl/package.json +11 -11
  178. package/vendor/node-addon-api/test/addon_data.cc +99 -99
  179. package/vendor/node-addon-api/test/addon_data.js +46 -46
  180. package/vendor/node-addon-api/test/array_buffer.cc +243 -243
  181. package/vendor/node-addon-api/test/array_buffer.js +69 -69
  182. package/vendor/node-addon-api/test/async_context.cc +21 -21
  183. package/vendor/node-addon-api/test/async_context.js +86 -86
  184. package/vendor/node-addon-api/test/async_progress_queue_worker.cc +83 -83
  185. package/vendor/node-addon-api/test/async_progress_queue_worker.js +46 -46
  186. package/vendor/node-addon-api/test/async_progress_worker.cc +134 -134
  187. package/vendor/node-addon-api/test/async_progress_worker.js +61 -61
  188. package/vendor/node-addon-api/test/async_worker.cc +106 -106
  189. package/vendor/node-addon-api/test/async_worker.js +179 -179
  190. package/vendor/node-addon-api/test/async_worker_nocallback.js +13 -13
  191. package/vendor/node-addon-api/test/async_worker_persistent.cc +63 -63
  192. package/vendor/node-addon-api/test/async_worker_persistent.js +24 -24
  193. package/vendor/node-addon-api/test/basic_types/array.cc +40 -40
  194. package/vendor/node-addon-api/test/basic_types/array.js +35 -35
  195. package/vendor/node-addon-api/test/basic_types/boolean.cc +38 -38
  196. package/vendor/node-addon-api/test/basic_types/boolean.js +35 -35
  197. package/vendor/node-addon-api/test/basic_types/number.cc +99 -99
  198. package/vendor/node-addon-api/test/basic_types/number.js +114 -114
  199. package/vendor/node-addon-api/test/basic_types/value.cc +120 -120
  200. package/vendor/node-addon-api/test/basic_types/value.js +133 -133
  201. package/vendor/node-addon-api/test/bigint.cc +91 -91
  202. package/vendor/node-addon-api/test/bigint.js +53 -53
  203. package/vendor/node-addon-api/test/binding-swallowexcept.cc +12 -12
  204. package/vendor/node-addon-api/test/binding.cc +171 -171
  205. package/vendor/node-addon-api/test/binding.gyp +117 -117
  206. package/vendor/node-addon-api/test/buffer.cc +183 -183
  207. package/vendor/node-addon-api/test/buffer.js +69 -69
  208. package/vendor/node-addon-api/test/callbackscope.cc +22 -22
  209. package/vendor/node-addon-api/test/callbackscope.js +49 -49
  210. package/vendor/node-addon-api/test/common/index.js +113 -113
  211. package/vendor/node-addon-api/test/common/test_helper.h +61 -61
  212. package/vendor/node-addon-api/test/dataview/dataview.cc +48 -48
  213. package/vendor/node-addon-api/test/dataview/dataview.js +35 -35
  214. package/vendor/node-addon-api/test/dataview/dataview_read_write.cc +115 -115
  215. package/vendor/node-addon-api/test/dataview/dataview_read_write.js +90 -90
  216. package/vendor/node-addon-api/test/date.cc +44 -44
  217. package/vendor/node-addon-api/test/date.js +18 -18
  218. package/vendor/node-addon-api/test/env_cleanup.cc +88 -88
  219. package/vendor/node-addon-api/test/env_cleanup.js +56 -56
  220. package/vendor/node-addon-api/test/error.cc +287 -287
  221. package/vendor/node-addon-api/test/error.js +81 -81
  222. package/vendor/node-addon-api/test/error_handling_for_primitives.cc +13 -13
  223. package/vendor/node-addon-api/test/error_handling_for_primitives.js +29 -29
  224. package/vendor/node-addon-api/test/error_terminating_environment.js +94 -94
  225. package/vendor/node-addon-api/test/external.cc +81 -81
  226. package/vendor/node-addon-api/test/external.js +88 -88
  227. package/vendor/node-addon-api/test/function.cc +295 -295
  228. package/vendor/node-addon-api/test/function.js +121 -121
  229. package/vendor/node-addon-api/test/function_reference.cc +202 -202
  230. package/vendor/node-addon-api/test/function_reference.js +157 -157
  231. package/vendor/node-addon-api/test/globalObject/global_object.cc +61 -61
  232. package/vendor/node-addon-api/test/globalObject/global_object_delete_property.cc +31 -31
  233. package/vendor/node-addon-api/test/globalObject/global_object_delete_property.js +61 -61
  234. package/vendor/node-addon-api/test/globalObject/global_object_get_property.cc +40 -40
  235. package/vendor/node-addon-api/test/globalObject/global_object_get_property.js +57 -57
  236. package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.cc +28 -28
  237. package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.js +48 -48
  238. package/vendor/node-addon-api/test/globalObject/global_object_set_property.cc +30 -30
  239. package/vendor/node-addon-api/test/globalObject/global_object_set_property.js +58 -58
  240. package/vendor/node-addon-api/test/handlescope.cc +60 -60
  241. package/vendor/node-addon-api/test/handlescope.js +14 -14
  242. package/vendor/node-addon-api/test/index.js +136 -136
  243. package/vendor/node-addon-api/test/maybe/check.cc +23 -23
  244. package/vendor/node-addon-api/test/maybe/index.js +38 -38
  245. package/vendor/node-addon-api/test/memory_management.cc +17 -17
  246. package/vendor/node-addon-api/test/memory_management.js +9 -9
  247. package/vendor/node-addon-api/test/movable_callbacks.cc +23 -23
  248. package/vendor/node-addon-api/test/movable_callbacks.js +21 -21
  249. package/vendor/node-addon-api/test/name.cc +108 -108
  250. package/vendor/node-addon-api/test/name.js +59 -59
  251. package/vendor/node-addon-api/test/napi_child.js +14 -14
  252. package/vendor/node-addon-api/test/object/delete_property.cc +38 -38
  253. package/vendor/node-addon-api/test/object/delete_property.js +41 -41
  254. package/vendor/node-addon-api/test/object/finalizer.cc +29 -29
  255. package/vendor/node-addon-api/test/object/finalizer.js +28 -28
  256. package/vendor/node-addon-api/test/object/get_property.cc +34 -34
  257. package/vendor/node-addon-api/test/object/get_property.js +40 -40
  258. package/vendor/node-addon-api/test/object/has_own_property.cc +34 -34
  259. package/vendor/node-addon-api/test/object/has_own_property.js +34 -34
  260. package/vendor/node-addon-api/test/object/has_property.cc +38 -38
  261. package/vendor/node-addon-api/test/object/has_property.js +37 -37
  262. package/vendor/node-addon-api/test/object/object.cc +348 -348
  263. package/vendor/node-addon-api/test/object/object.js +217 -217
  264. package/vendor/node-addon-api/test/object/object_deprecated.cc +66 -66
  265. package/vendor/node-addon-api/test/object/object_deprecated.js +47 -47
  266. package/vendor/node-addon-api/test/object/object_freeze_seal.cc +25 -25
  267. package/vendor/node-addon-api/test/object/object_freeze_seal.js +61 -61
  268. package/vendor/node-addon-api/test/object/set_property.cc +37 -37
  269. package/vendor/node-addon-api/test/object/set_property.js +29 -29
  270. package/vendor/node-addon-api/test/object/subscript_operator.cc +42 -42
  271. package/vendor/node-addon-api/test/object/subscript_operator.js +17 -17
  272. package/vendor/node-addon-api/test/object_reference.cc +219 -219
  273. package/vendor/node-addon-api/test/object_reference.js +259 -259
  274. package/vendor/node-addon-api/test/objectwrap.cc +268 -268
  275. package/vendor/node-addon-api/test/objectwrap.js +284 -284
  276. package/vendor/node-addon-api/test/objectwrap_constructor_exception.cc +26 -26
  277. package/vendor/node-addon-api/test/objectwrap_constructor_exception.js +18 -18
  278. package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.cc +30 -30
  279. package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.js +13 -13
  280. package/vendor/node-addon-api/test/objectwrap_removewrap.cc +45 -45
  281. package/vendor/node-addon-api/test/objectwrap_removewrap.js +40 -40
  282. package/vendor/node-addon-api/test/objectwrap_worker_thread.js +19 -19
  283. package/vendor/node-addon-api/test/promise.cc +29 -29
  284. package/vendor/node-addon-api/test/promise.js +18 -18
  285. package/vendor/node-addon-api/test/reference.cc +24 -24
  286. package/vendor/node-addon-api/test/reference.js +14 -14
  287. package/vendor/node-addon-api/test/run_script.cc +56 -56
  288. package/vendor/node-addon-api/test/run_script.js +45 -45
  289. package/vendor/node-addon-api/test/symbol.cc +79 -79
  290. package/vendor/node-addon-api/test/symbol.js +73 -73
  291. package/vendor/node-addon-api/test/testUtil.js +54 -54
  292. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.cc +195 -195
  293. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.js +188 -188
  294. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.cc +63 -63
  295. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.js +12 -12
  296. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.cc +115 -115
  297. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.js +14 -14
  298. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.cc +26 -26
  299. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.js +7 -7
  300. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.cc +225 -225
  301. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.js +59 -59
  302. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.cc +42 -42
  303. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.js +53 -53
  304. package/vendor/node-addon-api/test/thunking_manual.cc +140 -140
  305. package/vendor/node-addon-api/test/thunking_manual.js +17 -17
  306. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.cc +215 -215
  307. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.js +188 -188
  308. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc +68 -68
  309. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js +12 -12
  310. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc +127 -127
  311. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.js +14 -14
  312. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc +28 -28
  313. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.js +7 -7
  314. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc +237 -237
  315. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.js +59 -59
  316. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc +53 -53
  317. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.js +53 -53
  318. package/vendor/node-addon-api/test/typedarray-bigint.js +58 -58
  319. package/vendor/node-addon-api/test/typedarray.cc +216 -216
  320. package/vendor/node-addon-api/test/typedarray.js +69 -69
  321. package/vendor/node-addon-api/test/version_management.cc +27 -27
  322. package/vendor/node-addon-api/test/version_management.js +31 -31
  323. package/vendor/node-addon-api/tools/README.md +73 -73
  324. package/vendor/node-addon-api/tools/check-napi.js +100 -100
  325. package/vendor/node-addon-api/tools/clang-format.js +68 -68
  326. package/vendor/node-addon-api/tools/conversion.js +309 -309
  327. package/vendor/node-addon-api/tools/eslint-format.js +71 -71
  328. package/build/ALL_BUILD.vcxproj +0 -186
  329. package/build/ALL_BUILD.vcxproj.filters +0 -8
  330. package/build/CMakeCache.txt +0 -417
  331. package/build/CMakeFiles/3.23.0-rc1/CMakeASMCompiler.cmake +0 -20
  332. package/build/CMakeFiles/3.23.0-rc1/CMakeASM_MASMCompiler.cmake +0 -20
  333. package/build/CMakeFiles/3.23.0-rc1/CMakeCCompiler.cmake +0 -72
  334. package/build/CMakeFiles/3.23.0-rc1/CMakeCXXCompiler.cmake +0 -83
  335. package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_C.bin +0 -0
  336. package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_CXX.bin +0 -0
  337. package/build/CMakeFiles/3.23.0-rc1/CMakeRCCompiler.cmake +0 -6
  338. package/build/CMakeFiles/3.23.0-rc1/CMakeSystem.cmake +0 -15
  339. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CMakeCCompilerId.c +0 -828
  340. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CompilerIdC.exe +0 -0
  341. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CompilerIdC.vcxproj +0 -71
  342. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CMakeCCompilerId.obj +0 -0
  343. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.exe.recipe +0 -11
  344. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.command.1.tlog +0 -0
  345. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.read.1.tlog +0 -0
  346. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CL.write.1.tlog +0 -0
  347. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/CompilerIdC.lastbuildstate +0 -2
  348. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.command.1.tlog +0 -0
  349. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.read.1.tlog +0 -0
  350. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CompilerIdC.tlog/link.write.1.tlog +0 -0
  351. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CMakeCXXCompilerId.cpp +0 -816
  352. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CompilerIdCXX.exe +0 -0
  353. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CompilerIdCXX.vcxproj +0 -71
  354. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CMakeCXXCompilerId.obj +0 -0
  355. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.exe.recipe +0 -11
  356. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.command.1.tlog +0 -0
  357. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.read.1.tlog +0 -0
  358. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CL.write.1.tlog +0 -0
  359. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/CompilerIdCXX.lastbuildstate +0 -2
  360. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.command.1.tlog +0 -0
  361. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.read.1.tlog +0 -0
  362. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CompilerIdCXX.tlog/link.write.1.tlog +0 -0
  363. package/build/CMakeFiles/3.23.0-rc1/VCTargetsPath.txt +0 -1
  364. package/build/CMakeFiles/3.23.0-rc1/VCTargetsPath.vcxproj +0 -31
  365. package/build/CMakeFiles/3.23.0-rc1/x64/Debug/VCTargetsPath.recipe +0 -11
  366. package/build/CMakeFiles/3.23.0-rc1/x64/Debug/VCTargetsPath.tlog/VCTargetsPath.lastbuildstate +0 -2
  367. package/build/CMakeFiles/41bcd16856091d4a38fd1f71fbe2f202/generate.stamp.rule +0 -1
  368. package/build/CMakeFiles/CMakeError.log +0 -108
  369. package/build/CMakeFiles/CMakeOutput.log +0 -413
  370. package/build/CMakeFiles/TargetDirectories.txt +0 -3
  371. package/build/CMakeFiles/cmake.check_cache +0 -1
  372. package/build/CMakeFiles/generate.stamp +0 -1
  373. package/build/CMakeFiles/generate.stamp.depend +0 -109
  374. package/build/CMakeFiles/generate.stamp.list +0 -1
  375. package/build/Release/koffi.exp +0 -0
  376. package/build/Release/koffi.lib +0 -0
  377. package/build/Release/koffi.node +0 -0
  378. package/build/ZERO_CHECK.vcxproj +0 -176
  379. package/build/ZERO_CHECK.vcxproj.filters +0 -13
  380. package/build/cmake_install.cmake +0 -44
  381. package/build/koffi.dir/Release/call_arm64.obj +0 -0
  382. package/build/koffi.dir/Release/call_x64_sysv.obj +0 -0
  383. package/build/koffi.dir/Release/call_x64_win.obj +0 -0
  384. package/build/koffi.dir/Release/call_x64_win_fwd.obj +0 -0
  385. package/build/koffi.dir/Release/call_x86.obj +0 -0
  386. package/build/koffi.dir/Release/ffi.obj +0 -0
  387. package/build/koffi.dir/Release/koffi.node.recipe +0 -14
  388. package/build/koffi.dir/Release/koffi.tlog/CL.command.1.tlog +0 -0
  389. package/build/koffi.dir/Release/koffi.tlog/CL.read.1.tlog +0 -0
  390. package/build/koffi.dir/Release/koffi.tlog/CL.write.1.tlog +0 -0
  391. package/build/koffi.dir/Release/koffi.tlog/CustomBuild.command.1.tlog +0 -10
  392. package/build/koffi.dir/Release/koffi.tlog/CustomBuild.read.1.tlog +0 -108
  393. package/build/koffi.dir/Release/koffi.tlog/CustomBuild.write.1.tlog +0 -2
  394. package/build/koffi.dir/Release/koffi.tlog/Masm.read.1u.tlog +0 -0
  395. package/build/koffi.dir/Release/koffi.tlog/Masm.write.1u.tlog +0 -0
  396. package/build/koffi.dir/Release/koffi.tlog/koffi.lastbuildstate +0 -2
  397. package/build/koffi.dir/Release/koffi.tlog/koffi.write.1u.tlog +0 -0
  398. package/build/koffi.dir/Release/koffi.tlog/link.command.1.tlog +0 -0
  399. package/build/koffi.dir/Release/koffi.tlog/link.read.1.tlog +0 -0
  400. package/build/koffi.dir/Release/koffi.tlog/link.write.1.tlog +0 -0
  401. package/build/koffi.dir/Release/libcc.obj +0 -0
  402. package/build/koffi.dir/Release/util.obj +0 -0
  403. package/build/koffi.dir/Release/win_delay_load_hook.obj +0 -0
  404. package/build/koffi.sln +0 -53
  405. package/build/koffi.vcxproj +0 -363
  406. package/build/koffi.vcxproj.filters +0 -40
  407. package/build/x64/Release/ALL_BUILD/ALL_BUILD.recipe +0 -17
  408. package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/ALL_BUILD.lastbuildstate +0 -2
  409. package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.command.1.tlog +0 -10
  410. package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.read.1.tlog +0 -108
  411. package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.write.1.tlog +0 -2
  412. package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.recipe +0 -11
  413. package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.command.1.tlog +0 -10
  414. package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.read.1.tlog +0 -109
  415. package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.write.1.tlog +0 -2
  416. package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/ZERO_CHECK.lastbuildstate +0 -2
@@ -1,509 +1,509 @@
1
- // Copyright 2018 Ulf Adams
2
- //
3
- // The contents of this file may be used under the terms of the Apache License,
4
- // Version 2.0.
5
- //
6
- // (See accompanying file LICENSE-Apache or copy at
7
- // http://www.apache.org/licenses/LICENSE-2.0)
8
- //
9
- // Alternatively, the contents of this file may be used under the terms of
10
- // the Boost Software License, Version 1.0.
11
- // (See accompanying file LICENSE-Boost or copy at
12
- // https://www.boost.org/LICENSE_1_0.txt)
13
- //
14
- // Unless required by applicable law or agreed to in writing, this software
15
- // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
- // KIND, either express or implied.
17
-
18
- // Runtime compiler options:
19
- // -DRYU_DEBUG Generate verbose debugging output to stdout.
20
- //
21
- // -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower,
22
- // depending on your compiler.
23
- //
24
- // -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every
25
- // required power of 5, only store every 26th entry, and compute
26
- // intermediate values with a multiplication. This reduces the lookup table
27
- // size by about 10x (only one case, and only double) at the cost of some
28
- // performance. Currently requires MSVC intrinsics.
29
-
30
- #include "ryu/ryu.h"
31
-
32
- #include <assert.h>
33
- #include <stdbool.h>
34
- #include <stdint.h>
35
- #include <stdlib.h>
36
- #include <string.h>
37
-
38
- #ifdef RYU_DEBUG
39
- #include <inttypes.h>
40
- #include <stdio.h>
41
- #endif
42
-
43
- #include "ryu/common.h"
44
- #include "ryu/digit_table.h"
45
- #include "ryu/d2s_intrinsics.h"
46
-
47
- // Include either the small or the full lookup tables depending on the mode.
48
- #if defined(RYU_OPTIMIZE_SIZE)
49
- #include "ryu/d2s_small_table.h"
50
- #else
51
- #include "ryu/d2s_full_table.h"
52
- #endif
53
-
54
- #define DOUBLE_MANTISSA_BITS 52
55
- #define DOUBLE_EXPONENT_BITS 11
56
- #define DOUBLE_BIAS 1023
57
-
58
- static inline uint32_t decimalLength17(const uint64_t v) {
59
- // This is slightly faster than a loop.
60
- // The average output length is 16.38 digits, so we check high-to-low.
61
- // Function precondition: v is not an 18, 19, or 20-digit number.
62
- // (17 digits are sufficient for round-tripping.)
63
- assert(v < 100000000000000000L);
64
- if (v >= 10000000000000000L) { return 17; }
65
- if (v >= 1000000000000000L) { return 16; }
66
- if (v >= 100000000000000L) { return 15; }
67
- if (v >= 10000000000000L) { return 14; }
68
- if (v >= 1000000000000L) { return 13; }
69
- if (v >= 100000000000L) { return 12; }
70
- if (v >= 10000000000L) { return 11; }
71
- if (v >= 1000000000L) { return 10; }
72
- if (v >= 100000000L) { return 9; }
73
- if (v >= 10000000L) { return 8; }
74
- if (v >= 1000000L) { return 7; }
75
- if (v >= 100000L) { return 6; }
76
- if (v >= 10000L) { return 5; }
77
- if (v >= 1000L) { return 4; }
78
- if (v >= 100L) { return 3; }
79
- if (v >= 10L) { return 2; }
80
- return 1;
81
- }
82
-
83
- // A floating decimal representing m * 10^e.
84
- typedef struct floating_decimal_64 {
85
- uint64_t mantissa;
86
- // Decimal exponent's range is -324 to 308
87
- // inclusive, and can fit in a short if needed.
88
- int32_t exponent;
89
- } floating_decimal_64;
90
-
91
- static inline floating_decimal_64 d2d(const uint64_t ieeeMantissa, const uint32_t ieeeExponent) {
92
- int32_t e2;
93
- uint64_t m2;
94
- if (ieeeExponent == 0) {
95
- // We subtract 2 so that the bounds computation has 2 additional bits.
96
- e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
97
- m2 = ieeeMantissa;
98
- } else {
99
- e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
100
- m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
101
- }
102
- const bool even = (m2 & 1) == 0;
103
- const bool acceptBounds = even;
104
-
105
- #ifdef RYU_DEBUG
106
- printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
107
- #endif
108
-
109
- // Step 2: Determine the interval of valid decimal representations.
110
- const uint64_t mv = 4 * m2;
111
- // Implicit bool -> int conversion. True is 1, false is 0.
112
- const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
113
- // We would compute mp and mm like this:
114
- // uint64_t mp = 4 * m2 + 2;
115
- // uint64_t mm = mv - 1 - mmShift;
116
-
117
- // Step 3: Convert to a decimal power base using 128-bit arithmetic.
118
- uint64_t vr, vp, vm;
119
- int32_t e10;
120
- bool vmIsTrailingZeros = false;
121
- bool vrIsTrailingZeros = false;
122
- if (e2 >= 0) {
123
- // I tried special-casing q == 0, but there was no effect on performance.
124
- // This expression is slightly faster than max(0, log10Pow2(e2) - 1).
125
- const uint32_t q = log10Pow2(e2) - (e2 > 3);
126
- e10 = (int32_t) q;
127
- const int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1;
128
- const int32_t i = -e2 + (int32_t) q + k;
129
- #if defined(RYU_OPTIMIZE_SIZE)
130
- uint64_t pow5[2];
131
- double_computeInvPow5(q, pow5);
132
- vr = mulShiftAll64(m2, pow5, i, &vp, &vm, mmShift);
133
- #else
134
- vr = mulShiftAll64(m2, DOUBLE_POW5_INV_SPLIT[q], i, &vp, &vm, mmShift);
135
- #endif
136
- #ifdef RYU_DEBUG
137
- printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
138
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
139
- #endif
140
- if (q <= 21) {
141
- // This should use q <= 22, but I think 21 is also safe. Smaller values
142
- // may still be safe, but it's more difficult to reason about them.
143
- // Only one of mp, mv, and mm can be a multiple of 5, if any.
144
- const uint32_t mvMod5 = ((uint32_t) mv) - 5 * ((uint32_t) div5(mv));
145
- if (mvMod5 == 0) {
146
- vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
147
- } else if (acceptBounds) {
148
- // Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
149
- // <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
150
- // <=> true && pow5Factor(mm) >= q, since e2 >= q.
151
- vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
152
- } else {
153
- // Same as min(e2 + 1, pow5Factor(mp)) >= q.
154
- vp -= multipleOfPowerOf5(mv + 2, q);
155
- }
156
- }
157
- } else {
158
- // This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
159
- const uint32_t q = log10Pow5(-e2) - (-e2 > 1);
160
- e10 = (int32_t) q + e2;
161
- const int32_t i = -e2 - (int32_t) q;
162
- const int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
163
- const int32_t j = (int32_t) q - k;
164
- #if defined(RYU_OPTIMIZE_SIZE)
165
- uint64_t pow5[2];
166
- double_computePow5(i, pow5);
167
- vr = mulShiftAll64(m2, pow5, j, &vp, &vm, mmShift);
168
- #else
169
- vr = mulShiftAll64(m2, DOUBLE_POW5_SPLIT[i], j, &vp, &vm, mmShift);
170
- #endif
171
- #ifdef RYU_DEBUG
172
- printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
173
- printf("%u %d %d %d\n", q, i, k, j);
174
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
175
- #endif
176
- if (q <= 1) {
177
- // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
178
- // mv = 4 * m2, so it always has at least two trailing 0 bits.
179
- vrIsTrailingZeros = true;
180
- if (acceptBounds) {
181
- // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
182
- vmIsTrailingZeros = mmShift == 1;
183
- } else {
184
- // mp = mv + 2, so it always has at least one trailing 0 bit.
185
- --vp;
186
- }
187
- } else if (q < 63) { // TODO(ulfjack): Use a tighter bound here.
188
- // We want to know if the full product has at least q trailing zeros.
189
- // We need to compute min(p2(mv), p5(mv) - e2) >= q
190
- // <=> p2(mv) >= q && p5(mv) - e2 >= q
191
- // <=> p2(mv) >= q (because -e2 >= q)
192
- vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
193
- #ifdef RYU_DEBUG
194
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
195
- #endif
196
- }
197
- }
198
- #ifdef RYU_DEBUG
199
- printf("e10=%d\n", e10);
200
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
201
- printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
202
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
203
- #endif
204
-
205
- // Step 4: Find the shortest decimal representation in the interval of valid representations.
206
- int32_t removed = 0;
207
- uint8_t lastRemovedDigit = 0;
208
- uint64_t output;
209
- // On average, we remove ~2 digits.
210
- if (vmIsTrailingZeros || vrIsTrailingZeros) {
211
- // General case, which happens rarely (~0.7%).
212
- for (;;) {
213
- const uint64_t vpDiv10 = div10(vp);
214
- const uint64_t vmDiv10 = div10(vm);
215
- if (vpDiv10 <= vmDiv10) {
216
- break;
217
- }
218
- const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
219
- const uint64_t vrDiv10 = div10(vr);
220
- const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
221
- vmIsTrailingZeros &= vmMod10 == 0;
222
- vrIsTrailingZeros &= lastRemovedDigit == 0;
223
- lastRemovedDigit = (uint8_t) vrMod10;
224
- vr = vrDiv10;
225
- vp = vpDiv10;
226
- vm = vmDiv10;
227
- ++removed;
228
- }
229
- #ifdef RYU_DEBUG
230
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
231
- printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
232
- #endif
233
- if (vmIsTrailingZeros) {
234
- for (;;) {
235
- const uint64_t vmDiv10 = div10(vm);
236
- const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
237
- if (vmMod10 != 0) {
238
- break;
239
- }
240
- const uint64_t vpDiv10 = div10(vp);
241
- const uint64_t vrDiv10 = div10(vr);
242
- const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
243
- vrIsTrailingZeros &= lastRemovedDigit == 0;
244
- lastRemovedDigit = (uint8_t) vrMod10;
245
- vr = vrDiv10;
246
- vp = vpDiv10;
247
- vm = vmDiv10;
248
- ++removed;
249
- }
250
- }
251
- #ifdef RYU_DEBUG
252
- printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
253
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
254
- #endif
255
- if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
256
- // Round even if the exact number is .....50..0.
257
- lastRemovedDigit = 4;
258
- }
259
- // We need to take vr + 1 if vr is outside bounds or we need to round up.
260
- output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
261
- } else {
262
- // Specialized for the common case (~99.3%). Percentages below are relative to this.
263
- bool roundUp = false;
264
- const uint64_t vpDiv100 = div100(vp);
265
- const uint64_t vmDiv100 = div100(vm);
266
- if (vpDiv100 > vmDiv100) { // Optimization: remove two digits at a time (~86.2%).
267
- const uint64_t vrDiv100 = div100(vr);
268
- const uint32_t vrMod100 = ((uint32_t) vr) - 100 * ((uint32_t) vrDiv100);
269
- roundUp = vrMod100 >= 50;
270
- vr = vrDiv100;
271
- vp = vpDiv100;
272
- vm = vmDiv100;
273
- removed += 2;
274
- }
275
- // Loop iterations below (approximately), without optimization above:
276
- // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
277
- // Loop iterations below (approximately), with optimization above:
278
- // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
279
- for (;;) {
280
- const uint64_t vpDiv10 = div10(vp);
281
- const uint64_t vmDiv10 = div10(vm);
282
- if (vpDiv10 <= vmDiv10) {
283
- break;
284
- }
285
- const uint64_t vrDiv10 = div10(vr);
286
- const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
287
- roundUp = vrMod10 >= 5;
288
- vr = vrDiv10;
289
- vp = vpDiv10;
290
- vm = vmDiv10;
291
- ++removed;
292
- }
293
- #ifdef RYU_DEBUG
294
- printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false");
295
- printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
296
- #endif
297
- // We need to take vr + 1 if vr is outside bounds or we need to round up.
298
- output = vr + (vr == vm || roundUp);
299
- }
300
- const int32_t exp = e10 + removed;
301
-
302
- #ifdef RYU_DEBUG
303
- printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
304
- printf("O=%" PRIu64 "\n", output);
305
- printf("EXP=%d\n", exp);
306
- #endif
307
-
308
- floating_decimal_64 fd;
309
- fd.exponent = exp;
310
- fd.mantissa = output;
311
- return fd;
312
- }
313
-
314
- static inline int to_chars(const floating_decimal_64 v, const bool sign, char* const result) {
315
- // Step 5: Print the decimal representation.
316
- int index = 0;
317
- if (sign) {
318
- result[index++] = '-';
319
- }
320
-
321
- uint64_t output = v.mantissa;
322
- const uint32_t olength = decimalLength17(output);
323
-
324
- #ifdef RYU_DEBUG
325
- printf("DIGITS=%" PRIu64 "\n", v.mantissa);
326
- printf("OLEN=%u\n", olength);
327
- printf("EXP=%u\n", v.exponent + olength);
328
- #endif
329
-
330
- // Print the decimal digits.
331
- // The following code is equivalent to:
332
- // for (uint32_t i = 0; i < olength - 1; ++i) {
333
- // const uint32_t c = output % 10; output /= 10;
334
- // result[index + olength - i] = (char) ('0' + c);
335
- // }
336
- // result[index] = '0' + output % 10;
337
-
338
- uint32_t i = 0;
339
- // We prefer 32-bit operations, even on 64-bit platforms.
340
- // We have at most 17 digits, and uint32_t can store 9 digits.
341
- // If output doesn't fit into uint32_t, we cut off 8 digits,
342
- // so the rest will fit into uint32_t.
343
- if ((output >> 32) != 0) {
344
- // Expensive 64-bit division.
345
- const uint64_t q = div1e8(output);
346
- uint32_t output2 = ((uint32_t) output) - 100000000 * ((uint32_t) q);
347
- output = q;
348
-
349
- const uint32_t c = output2 % 10000;
350
- output2 /= 10000;
351
- const uint32_t d = output2 % 10000;
352
- const uint32_t c0 = (c % 100) << 1;
353
- const uint32_t c1 = (c / 100) << 1;
354
- const uint32_t d0 = (d % 100) << 1;
355
- const uint32_t d1 = (d / 100) << 1;
356
- memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
357
- memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
358
- memcpy(result + index + olength - i - 5, DIGIT_TABLE + d0, 2);
359
- memcpy(result + index + olength - i - 7, DIGIT_TABLE + d1, 2);
360
- i += 8;
361
- }
362
- uint32_t output2 = (uint32_t) output;
363
- while (output2 >= 10000) {
364
- #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217
365
- const uint32_t c = output2 - 10000 * (output2 / 10000);
366
- #else
367
- const uint32_t c = output2 % 10000;
368
- #endif
369
- output2 /= 10000;
370
- const uint32_t c0 = (c % 100) << 1;
371
- const uint32_t c1 = (c / 100) << 1;
372
- memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
373
- memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
374
- i += 4;
375
- }
376
- if (output2 >= 100) {
377
- const uint32_t c = (output2 % 100) << 1;
378
- output2 /= 100;
379
- memcpy(result + index + olength - i - 1, DIGIT_TABLE + c, 2);
380
- i += 2;
381
- }
382
- if (output2 >= 10) {
383
- const uint32_t c = output2 << 1;
384
- // We can't use memcpy here: the decimal dot goes between these two digits.
385
- result[index + olength - i] = DIGIT_TABLE[c + 1];
386
- result[index] = DIGIT_TABLE[c];
387
- } else {
388
- result[index] = (char) ('0' + output2);
389
- }
390
-
391
- // Print decimal point if needed.
392
- if (olength > 1) {
393
- result[index + 1] = '.';
394
- index += olength + 1;
395
- } else {
396
- ++index;
397
- }
398
-
399
- // Print the exponent.
400
- result[index++] = 'E';
401
- int32_t exp = v.exponent + (int32_t) olength - 1;
402
- if (exp < 0) {
403
- result[index++] = '-';
404
- exp = -exp;
405
- }
406
-
407
- if (exp >= 100) {
408
- const int32_t c = exp % 10;
409
- memcpy(result + index, DIGIT_TABLE + 2 * (exp / 10), 2);
410
- result[index + 2] = (char) ('0' + c);
411
- index += 3;
412
- } else if (exp >= 10) {
413
- memcpy(result + index, DIGIT_TABLE + 2 * exp, 2);
414
- index += 2;
415
- } else {
416
- result[index++] = (char) ('0' + exp);
417
- }
418
-
419
- return index;
420
- }
421
-
422
- static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
423
- floating_decimal_64* const v) {
424
- const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
425
- const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
426
-
427
- if (e2 > 0) {
428
- // f = m2 * 2^e2 >= 2^53 is an integer.
429
- // Ignore this case for now.
430
- return false;
431
- }
432
-
433
- if (e2 < -52) {
434
- // f < 1.
435
- return false;
436
- }
437
-
438
- // Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53.
439
- // Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0.
440
- const uint64_t mask = (1ull << -e2) - 1;
441
- const uint64_t fraction = m2 & mask;
442
- if (fraction != 0) {
443
- return false;
444
- }
445
-
446
- // f is an integer in the range [1, 2^53).
447
- // Note: mantissa might contain trailing (decimal) 0's.
448
- // Note: since 2^53 < 10^16, there is no need to adjust decimalLength17().
449
- v->mantissa = m2 >> -e2;
450
- v->exponent = 0;
451
- return true;
452
- }
453
-
454
- int d2s_buffered_n(double f, char* result) {
455
- // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
456
- const uint64_t bits = double_to_bits(f);
457
-
458
- #ifdef RYU_DEBUG
459
- printf("IN=");
460
- for (int32_t bit = 63; bit >= 0; --bit) {
461
- printf("%d", (int) ((bits >> bit) & 1));
462
- }
463
- printf("\n");
464
- #endif
465
-
466
- // Decode bits into sign, mantissa, and exponent.
467
- const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
468
- const uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
469
- const uint32_t ieeeExponent = (uint32_t) ((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
470
- // Case distinction; exit early for the easy cases.
471
- if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) {
472
- return copy_special_str(result, ieeeSign, ieeeExponent, ieeeMantissa);
473
- }
474
-
475
- floating_decimal_64 v;
476
- const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
477
- if (isSmallInt) {
478
- // For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros.
479
- // For scientific notation we need to move these zeros into the exponent.
480
- // (This is not needed for fixed-point notation, so it might be beneficial to trim
481
- // trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.)
482
- for (;;) {
483
- const uint64_t q = div10(v.mantissa);
484
- const uint32_t r = ((uint32_t) v.mantissa) - 10 * ((uint32_t) q);
485
- if (r != 0) {
486
- break;
487
- }
488
- v.mantissa = q;
489
- ++v.exponent;
490
- }
491
- } else {
492
- v = d2d(ieeeMantissa, ieeeExponent);
493
- }
494
-
495
- return to_chars(v, ieeeSign, result);
496
- }
497
-
498
- void d2s_buffered(double f, char* result) {
499
- const int index = d2s_buffered_n(f, result);
500
-
501
- // Terminate the string.
502
- result[index] = '\0';
503
- }
504
-
505
- char* d2s(double f) {
506
- char* const result = (char*) malloc(25);
507
- d2s_buffered(f, result);
508
- return result;
509
- }
1
+ // Copyright 2018 Ulf Adams
2
+ //
3
+ // The contents of this file may be used under the terms of the Apache License,
4
+ // Version 2.0.
5
+ //
6
+ // (See accompanying file LICENSE-Apache or copy at
7
+ // http://www.apache.org/licenses/LICENSE-2.0)
8
+ //
9
+ // Alternatively, the contents of this file may be used under the terms of
10
+ // the Boost Software License, Version 1.0.
11
+ // (See accompanying file LICENSE-Boost or copy at
12
+ // https://www.boost.org/LICENSE_1_0.txt)
13
+ //
14
+ // Unless required by applicable law or agreed to in writing, this software
15
+ // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16
+ // KIND, either express or implied.
17
+
18
+ // Runtime compiler options:
19
+ // -DRYU_DEBUG Generate verbose debugging output to stdout.
20
+ //
21
+ // -DRYU_ONLY_64_BIT_OPS Avoid using uint128_t or 64-bit intrinsics. Slower,
22
+ // depending on your compiler.
23
+ //
24
+ // -DRYU_OPTIMIZE_SIZE Use smaller lookup tables. Instead of storing every
25
+ // required power of 5, only store every 26th entry, and compute
26
+ // intermediate values with a multiplication. This reduces the lookup table
27
+ // size by about 10x (only one case, and only double) at the cost of some
28
+ // performance. Currently requires MSVC intrinsics.
29
+
30
+ #include "ryu/ryu.h"
31
+
32
+ #include <assert.h>
33
+ #include <stdbool.h>
34
+ #include <stdint.h>
35
+ #include <stdlib.h>
36
+ #include <string.h>
37
+
38
+ #ifdef RYU_DEBUG
39
+ #include <inttypes.h>
40
+ #include <stdio.h>
41
+ #endif
42
+
43
+ #include "ryu/common.h"
44
+ #include "ryu/digit_table.h"
45
+ #include "ryu/d2s_intrinsics.h"
46
+
47
+ // Include either the small or the full lookup tables depending on the mode.
48
+ #if defined(RYU_OPTIMIZE_SIZE)
49
+ #include "ryu/d2s_small_table.h"
50
+ #else
51
+ #include "ryu/d2s_full_table.h"
52
+ #endif
53
+
54
+ #define DOUBLE_MANTISSA_BITS 52
55
+ #define DOUBLE_EXPONENT_BITS 11
56
+ #define DOUBLE_BIAS 1023
57
+
58
+ static inline uint32_t decimalLength17(const uint64_t v) {
59
+ // This is slightly faster than a loop.
60
+ // The average output length is 16.38 digits, so we check high-to-low.
61
+ // Function precondition: v is not an 18, 19, or 20-digit number.
62
+ // (17 digits are sufficient for round-tripping.)
63
+ assert(v < 100000000000000000L);
64
+ if (v >= 10000000000000000L) { return 17; }
65
+ if (v >= 1000000000000000L) { return 16; }
66
+ if (v >= 100000000000000L) { return 15; }
67
+ if (v >= 10000000000000L) { return 14; }
68
+ if (v >= 1000000000000L) { return 13; }
69
+ if (v >= 100000000000L) { return 12; }
70
+ if (v >= 10000000000L) { return 11; }
71
+ if (v >= 1000000000L) { return 10; }
72
+ if (v >= 100000000L) { return 9; }
73
+ if (v >= 10000000L) { return 8; }
74
+ if (v >= 1000000L) { return 7; }
75
+ if (v >= 100000L) { return 6; }
76
+ if (v >= 10000L) { return 5; }
77
+ if (v >= 1000L) { return 4; }
78
+ if (v >= 100L) { return 3; }
79
+ if (v >= 10L) { return 2; }
80
+ return 1;
81
+ }
82
+
83
+ // A floating decimal representing m * 10^e.
84
+ typedef struct floating_decimal_64 {
85
+ uint64_t mantissa;
86
+ // Decimal exponent's range is -324 to 308
87
+ // inclusive, and can fit in a short if needed.
88
+ int32_t exponent;
89
+ } floating_decimal_64;
90
+
91
+ static inline floating_decimal_64 d2d(const uint64_t ieeeMantissa, const uint32_t ieeeExponent) {
92
+ int32_t e2;
93
+ uint64_t m2;
94
+ if (ieeeExponent == 0) {
95
+ // We subtract 2 so that the bounds computation has 2 additional bits.
96
+ e2 = 1 - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
97
+ m2 = ieeeMantissa;
98
+ } else {
99
+ e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS - 2;
100
+ m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
101
+ }
102
+ const bool even = (m2 & 1) == 0;
103
+ const bool acceptBounds = even;
104
+
105
+ #ifdef RYU_DEBUG
106
+ printf("-> %" PRIu64 " * 2^%d\n", m2, e2 + 2);
107
+ #endif
108
+
109
+ // Step 2: Determine the interval of valid decimal representations.
110
+ const uint64_t mv = 4 * m2;
111
+ // Implicit bool -> int conversion. True is 1, false is 0.
112
+ const uint32_t mmShift = ieeeMantissa != 0 || ieeeExponent <= 1;
113
+ // We would compute mp and mm like this:
114
+ // uint64_t mp = 4 * m2 + 2;
115
+ // uint64_t mm = mv - 1 - mmShift;
116
+
117
+ // Step 3: Convert to a decimal power base using 128-bit arithmetic.
118
+ uint64_t vr, vp, vm;
119
+ int32_t e10;
120
+ bool vmIsTrailingZeros = false;
121
+ bool vrIsTrailingZeros = false;
122
+ if (e2 >= 0) {
123
+ // I tried special-casing q == 0, but there was no effect on performance.
124
+ // This expression is slightly faster than max(0, log10Pow2(e2) - 1).
125
+ const uint32_t q = log10Pow2(e2) - (e2 > 3);
126
+ e10 = (int32_t) q;
127
+ const int32_t k = DOUBLE_POW5_INV_BITCOUNT + pow5bits((int32_t) q) - 1;
128
+ const int32_t i = -e2 + (int32_t) q + k;
129
+ #if defined(RYU_OPTIMIZE_SIZE)
130
+ uint64_t pow5[2];
131
+ double_computeInvPow5(q, pow5);
132
+ vr = mulShiftAll64(m2, pow5, i, &vp, &vm, mmShift);
133
+ #else
134
+ vr = mulShiftAll64(m2, DOUBLE_POW5_INV_SPLIT[q], i, &vp, &vm, mmShift);
135
+ #endif
136
+ #ifdef RYU_DEBUG
137
+ printf("%" PRIu64 " * 2^%d / 10^%u\n", mv, e2, q);
138
+ printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
139
+ #endif
140
+ if (q <= 21) {
141
+ // This should use q <= 22, but I think 21 is also safe. Smaller values
142
+ // may still be safe, but it's more difficult to reason about them.
143
+ // Only one of mp, mv, and mm can be a multiple of 5, if any.
144
+ const uint32_t mvMod5 = ((uint32_t) mv) - 5 * ((uint32_t) div5(mv));
145
+ if (mvMod5 == 0) {
146
+ vrIsTrailingZeros = multipleOfPowerOf5(mv, q);
147
+ } else if (acceptBounds) {
148
+ // Same as min(e2 + (~mm & 1), pow5Factor(mm)) >= q
149
+ // <=> e2 + (~mm & 1) >= q && pow5Factor(mm) >= q
150
+ // <=> true && pow5Factor(mm) >= q, since e2 >= q.
151
+ vmIsTrailingZeros = multipleOfPowerOf5(mv - 1 - mmShift, q);
152
+ } else {
153
+ // Same as min(e2 + 1, pow5Factor(mp)) >= q.
154
+ vp -= multipleOfPowerOf5(mv + 2, q);
155
+ }
156
+ }
157
+ } else {
158
+ // This expression is slightly faster than max(0, log10Pow5(-e2) - 1).
159
+ const uint32_t q = log10Pow5(-e2) - (-e2 > 1);
160
+ e10 = (int32_t) q + e2;
161
+ const int32_t i = -e2 - (int32_t) q;
162
+ const int32_t k = pow5bits(i) - DOUBLE_POW5_BITCOUNT;
163
+ const int32_t j = (int32_t) q - k;
164
+ #if defined(RYU_OPTIMIZE_SIZE)
165
+ uint64_t pow5[2];
166
+ double_computePow5(i, pow5);
167
+ vr = mulShiftAll64(m2, pow5, j, &vp, &vm, mmShift);
168
+ #else
169
+ vr = mulShiftAll64(m2, DOUBLE_POW5_SPLIT[i], j, &vp, &vm, mmShift);
170
+ #endif
171
+ #ifdef RYU_DEBUG
172
+ printf("%" PRIu64 " * 5^%d / 10^%u\n", mv, -e2, q);
173
+ printf("%u %d %d %d\n", q, i, k, j);
174
+ printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
175
+ #endif
176
+ if (q <= 1) {
177
+ // {vr,vp,vm} is trailing zeros if {mv,mp,mm} has at least q trailing 0 bits.
178
+ // mv = 4 * m2, so it always has at least two trailing 0 bits.
179
+ vrIsTrailingZeros = true;
180
+ if (acceptBounds) {
181
+ // mm = mv - 1 - mmShift, so it has 1 trailing 0 bit iff mmShift == 1.
182
+ vmIsTrailingZeros = mmShift == 1;
183
+ } else {
184
+ // mp = mv + 2, so it always has at least one trailing 0 bit.
185
+ --vp;
186
+ }
187
+ } else if (q < 63) { // TODO(ulfjack): Use a tighter bound here.
188
+ // We want to know if the full product has at least q trailing zeros.
189
+ // We need to compute min(p2(mv), p5(mv) - e2) >= q
190
+ // <=> p2(mv) >= q && p5(mv) - e2 >= q
191
+ // <=> p2(mv) >= q (because -e2 >= q)
192
+ vrIsTrailingZeros = multipleOfPowerOf2(mv, q);
193
+ #ifdef RYU_DEBUG
194
+ printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
195
+ #endif
196
+ }
197
+ }
198
+ #ifdef RYU_DEBUG
199
+ printf("e10=%d\n", e10);
200
+ printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
201
+ printf("vm is trailing zeros=%s\n", vmIsTrailingZeros ? "true" : "false");
202
+ printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
203
+ #endif
204
+
205
+ // Step 4: Find the shortest decimal representation in the interval of valid representations.
206
+ int32_t removed = 0;
207
+ uint8_t lastRemovedDigit = 0;
208
+ uint64_t output;
209
+ // On average, we remove ~2 digits.
210
+ if (vmIsTrailingZeros || vrIsTrailingZeros) {
211
+ // General case, which happens rarely (~0.7%).
212
+ for (;;) {
213
+ const uint64_t vpDiv10 = div10(vp);
214
+ const uint64_t vmDiv10 = div10(vm);
215
+ if (vpDiv10 <= vmDiv10) {
216
+ break;
217
+ }
218
+ const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
219
+ const uint64_t vrDiv10 = div10(vr);
220
+ const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
221
+ vmIsTrailingZeros &= vmMod10 == 0;
222
+ vrIsTrailingZeros &= lastRemovedDigit == 0;
223
+ lastRemovedDigit = (uint8_t) vrMod10;
224
+ vr = vrDiv10;
225
+ vp = vpDiv10;
226
+ vm = vmDiv10;
227
+ ++removed;
228
+ }
229
+ #ifdef RYU_DEBUG
230
+ printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
231
+ printf("d-10=%s\n", vmIsTrailingZeros ? "true" : "false");
232
+ #endif
233
+ if (vmIsTrailingZeros) {
234
+ for (;;) {
235
+ const uint64_t vmDiv10 = div10(vm);
236
+ const uint32_t vmMod10 = ((uint32_t) vm) - 10 * ((uint32_t) vmDiv10);
237
+ if (vmMod10 != 0) {
238
+ break;
239
+ }
240
+ const uint64_t vpDiv10 = div10(vp);
241
+ const uint64_t vrDiv10 = div10(vr);
242
+ const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
243
+ vrIsTrailingZeros &= lastRemovedDigit == 0;
244
+ lastRemovedDigit = (uint8_t) vrMod10;
245
+ vr = vrDiv10;
246
+ vp = vpDiv10;
247
+ vm = vmDiv10;
248
+ ++removed;
249
+ }
250
+ }
251
+ #ifdef RYU_DEBUG
252
+ printf("%" PRIu64 " %d\n", vr, lastRemovedDigit);
253
+ printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
254
+ #endif
255
+ if (vrIsTrailingZeros && lastRemovedDigit == 5 && vr % 2 == 0) {
256
+ // Round even if the exact number is .....50..0.
257
+ lastRemovedDigit = 4;
258
+ }
259
+ // We need to take vr + 1 if vr is outside bounds or we need to round up.
260
+ output = vr + ((vr == vm && (!acceptBounds || !vmIsTrailingZeros)) || lastRemovedDigit >= 5);
261
+ } else {
262
+ // Specialized for the common case (~99.3%). Percentages below are relative to this.
263
+ bool roundUp = false;
264
+ const uint64_t vpDiv100 = div100(vp);
265
+ const uint64_t vmDiv100 = div100(vm);
266
+ if (vpDiv100 > vmDiv100) { // Optimization: remove two digits at a time (~86.2%).
267
+ const uint64_t vrDiv100 = div100(vr);
268
+ const uint32_t vrMod100 = ((uint32_t) vr) - 100 * ((uint32_t) vrDiv100);
269
+ roundUp = vrMod100 >= 50;
270
+ vr = vrDiv100;
271
+ vp = vpDiv100;
272
+ vm = vmDiv100;
273
+ removed += 2;
274
+ }
275
+ // Loop iterations below (approximately), without optimization above:
276
+ // 0: 0.03%, 1: 13.8%, 2: 70.6%, 3: 14.0%, 4: 1.40%, 5: 0.14%, 6+: 0.02%
277
+ // Loop iterations below (approximately), with optimization above:
278
+ // 0: 70.6%, 1: 27.8%, 2: 1.40%, 3: 0.14%, 4+: 0.02%
279
+ for (;;) {
280
+ const uint64_t vpDiv10 = div10(vp);
281
+ const uint64_t vmDiv10 = div10(vm);
282
+ if (vpDiv10 <= vmDiv10) {
283
+ break;
284
+ }
285
+ const uint64_t vrDiv10 = div10(vr);
286
+ const uint32_t vrMod10 = ((uint32_t) vr) - 10 * ((uint32_t) vrDiv10);
287
+ roundUp = vrMod10 >= 5;
288
+ vr = vrDiv10;
289
+ vp = vpDiv10;
290
+ vm = vmDiv10;
291
+ ++removed;
292
+ }
293
+ #ifdef RYU_DEBUG
294
+ printf("%" PRIu64 " roundUp=%s\n", vr, roundUp ? "true" : "false");
295
+ printf("vr is trailing zeros=%s\n", vrIsTrailingZeros ? "true" : "false");
296
+ #endif
297
+ // We need to take vr + 1 if vr is outside bounds or we need to round up.
298
+ output = vr + (vr == vm || roundUp);
299
+ }
300
+ const int32_t exp = e10 + removed;
301
+
302
+ #ifdef RYU_DEBUG
303
+ printf("V+=%" PRIu64 "\nV =%" PRIu64 "\nV-=%" PRIu64 "\n", vp, vr, vm);
304
+ printf("O=%" PRIu64 "\n", output);
305
+ printf("EXP=%d\n", exp);
306
+ #endif
307
+
308
+ floating_decimal_64 fd;
309
+ fd.exponent = exp;
310
+ fd.mantissa = output;
311
+ return fd;
312
+ }
313
+
314
+ static inline int to_chars(const floating_decimal_64 v, const bool sign, char* const result) {
315
+ // Step 5: Print the decimal representation.
316
+ int index = 0;
317
+ if (sign) {
318
+ result[index++] = '-';
319
+ }
320
+
321
+ uint64_t output = v.mantissa;
322
+ const uint32_t olength = decimalLength17(output);
323
+
324
+ #ifdef RYU_DEBUG
325
+ printf("DIGITS=%" PRIu64 "\n", v.mantissa);
326
+ printf("OLEN=%u\n", olength);
327
+ printf("EXP=%u\n", v.exponent + olength);
328
+ #endif
329
+
330
+ // Print the decimal digits.
331
+ // The following code is equivalent to:
332
+ // for (uint32_t i = 0; i < olength - 1; ++i) {
333
+ // const uint32_t c = output % 10; output /= 10;
334
+ // result[index + olength - i] = (char) ('0' + c);
335
+ // }
336
+ // result[index] = '0' + output % 10;
337
+
338
+ uint32_t i = 0;
339
+ // We prefer 32-bit operations, even on 64-bit platforms.
340
+ // We have at most 17 digits, and uint32_t can store 9 digits.
341
+ // If output doesn't fit into uint32_t, we cut off 8 digits,
342
+ // so the rest will fit into uint32_t.
343
+ if ((output >> 32) != 0) {
344
+ // Expensive 64-bit division.
345
+ const uint64_t q = div1e8(output);
346
+ uint32_t output2 = ((uint32_t) output) - 100000000 * ((uint32_t) q);
347
+ output = q;
348
+
349
+ const uint32_t c = output2 % 10000;
350
+ output2 /= 10000;
351
+ const uint32_t d = output2 % 10000;
352
+ const uint32_t c0 = (c % 100) << 1;
353
+ const uint32_t c1 = (c / 100) << 1;
354
+ const uint32_t d0 = (d % 100) << 1;
355
+ const uint32_t d1 = (d / 100) << 1;
356
+ memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
357
+ memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
358
+ memcpy(result + index + olength - i - 5, DIGIT_TABLE + d0, 2);
359
+ memcpy(result + index + olength - i - 7, DIGIT_TABLE + d1, 2);
360
+ i += 8;
361
+ }
362
+ uint32_t output2 = (uint32_t) output;
363
+ while (output2 >= 10000) {
364
+ #ifdef __clang__ // https://bugs.llvm.org/show_bug.cgi?id=38217
365
+ const uint32_t c = output2 - 10000 * (output2 / 10000);
366
+ #else
367
+ const uint32_t c = output2 % 10000;
368
+ #endif
369
+ output2 /= 10000;
370
+ const uint32_t c0 = (c % 100) << 1;
371
+ const uint32_t c1 = (c / 100) << 1;
372
+ memcpy(result + index + olength - i - 1, DIGIT_TABLE + c0, 2);
373
+ memcpy(result + index + olength - i - 3, DIGIT_TABLE + c1, 2);
374
+ i += 4;
375
+ }
376
+ if (output2 >= 100) {
377
+ const uint32_t c = (output2 % 100) << 1;
378
+ output2 /= 100;
379
+ memcpy(result + index + olength - i - 1, DIGIT_TABLE + c, 2);
380
+ i += 2;
381
+ }
382
+ if (output2 >= 10) {
383
+ const uint32_t c = output2 << 1;
384
+ // We can't use memcpy here: the decimal dot goes between these two digits.
385
+ result[index + olength - i] = DIGIT_TABLE[c + 1];
386
+ result[index] = DIGIT_TABLE[c];
387
+ } else {
388
+ result[index] = (char) ('0' + output2);
389
+ }
390
+
391
+ // Print decimal point if needed.
392
+ if (olength > 1) {
393
+ result[index + 1] = '.';
394
+ index += olength + 1;
395
+ } else {
396
+ ++index;
397
+ }
398
+
399
+ // Print the exponent.
400
+ result[index++] = 'E';
401
+ int32_t exp = v.exponent + (int32_t) olength - 1;
402
+ if (exp < 0) {
403
+ result[index++] = '-';
404
+ exp = -exp;
405
+ }
406
+
407
+ if (exp >= 100) {
408
+ const int32_t c = exp % 10;
409
+ memcpy(result + index, DIGIT_TABLE + 2 * (exp / 10), 2);
410
+ result[index + 2] = (char) ('0' + c);
411
+ index += 3;
412
+ } else if (exp >= 10) {
413
+ memcpy(result + index, DIGIT_TABLE + 2 * exp, 2);
414
+ index += 2;
415
+ } else {
416
+ result[index++] = (char) ('0' + exp);
417
+ }
418
+
419
+ return index;
420
+ }
421
+
422
+ static inline bool d2d_small_int(const uint64_t ieeeMantissa, const uint32_t ieeeExponent,
423
+ floating_decimal_64* const v) {
424
+ const uint64_t m2 = (1ull << DOUBLE_MANTISSA_BITS) | ieeeMantissa;
425
+ const int32_t e2 = (int32_t) ieeeExponent - DOUBLE_BIAS - DOUBLE_MANTISSA_BITS;
426
+
427
+ if (e2 > 0) {
428
+ // f = m2 * 2^e2 >= 2^53 is an integer.
429
+ // Ignore this case for now.
430
+ return false;
431
+ }
432
+
433
+ if (e2 < -52) {
434
+ // f < 1.
435
+ return false;
436
+ }
437
+
438
+ // Since 2^52 <= m2 < 2^53 and 0 <= -e2 <= 52: 1 <= f = m2 / 2^-e2 < 2^53.
439
+ // Test if the lower -e2 bits of the significand are 0, i.e. whether the fraction is 0.
440
+ const uint64_t mask = (1ull << -e2) - 1;
441
+ const uint64_t fraction = m2 & mask;
442
+ if (fraction != 0) {
443
+ return false;
444
+ }
445
+
446
+ // f is an integer in the range [1, 2^53).
447
+ // Note: mantissa might contain trailing (decimal) 0's.
448
+ // Note: since 2^53 < 10^16, there is no need to adjust decimalLength17().
449
+ v->mantissa = m2 >> -e2;
450
+ v->exponent = 0;
451
+ return true;
452
+ }
453
+
454
+ int d2s_buffered_n(double f, char* result) {
455
+ // Step 1: Decode the floating-point number, and unify normalized and subnormal cases.
456
+ const uint64_t bits = double_to_bits(f);
457
+
458
+ #ifdef RYU_DEBUG
459
+ printf("IN=");
460
+ for (int32_t bit = 63; bit >= 0; --bit) {
461
+ printf("%d", (int) ((bits >> bit) & 1));
462
+ }
463
+ printf("\n");
464
+ #endif
465
+
466
+ // Decode bits into sign, mantissa, and exponent.
467
+ const bool ieeeSign = ((bits >> (DOUBLE_MANTISSA_BITS + DOUBLE_EXPONENT_BITS)) & 1) != 0;
468
+ const uint64_t ieeeMantissa = bits & ((1ull << DOUBLE_MANTISSA_BITS) - 1);
469
+ const uint32_t ieeeExponent = (uint32_t) ((bits >> DOUBLE_MANTISSA_BITS) & ((1u << DOUBLE_EXPONENT_BITS) - 1));
470
+ // Case distinction; exit early for the easy cases.
471
+ if (ieeeExponent == ((1u << DOUBLE_EXPONENT_BITS) - 1u) || (ieeeExponent == 0 && ieeeMantissa == 0)) {
472
+ return copy_special_str(result, ieeeSign, ieeeExponent, ieeeMantissa);
473
+ }
474
+
475
+ floating_decimal_64 v;
476
+ const bool isSmallInt = d2d_small_int(ieeeMantissa, ieeeExponent, &v);
477
+ if (isSmallInt) {
478
+ // For small integers in the range [1, 2^53), v.mantissa might contain trailing (decimal) zeros.
479
+ // For scientific notation we need to move these zeros into the exponent.
480
+ // (This is not needed for fixed-point notation, so it might be beneficial to trim
481
+ // trailing zeros in to_chars only if needed - once fixed-point notation output is implemented.)
482
+ for (;;) {
483
+ const uint64_t q = div10(v.mantissa);
484
+ const uint32_t r = ((uint32_t) v.mantissa) - 10 * ((uint32_t) q);
485
+ if (r != 0) {
486
+ break;
487
+ }
488
+ v.mantissa = q;
489
+ ++v.exponent;
490
+ }
491
+ } else {
492
+ v = d2d(ieeeMantissa, ieeeExponent);
493
+ }
494
+
495
+ return to_chars(v, ieeeSign, result);
496
+ }
497
+
498
+ void d2s_buffered(double f, char* result) {
499
+ const int index = d2s_buffered_n(f, result);
500
+
501
+ // Terminate the string.
502
+ result[index] = '\0';
503
+ }
504
+
505
+ char* d2s(double f) {
506
+ char* const result = (char*) malloc(25);
507
+ d2s_buffered(f, result);
508
+ return result;
509
+ }