diff options
Diffstat (limited to 'examples/redis-unstable/deps/fpconv/fpconv_powers.h')
| -rw-r--r-- | examples/redis-unstable/deps/fpconv/fpconv_powers.h | 133 |
1 files changed, 133 insertions, 0 deletions
diff --git a/examples/redis-unstable/deps/fpconv/fpconv_powers.h b/examples/redis-unstable/deps/fpconv/fpconv_powers.h new file mode 100644 index 0000000..bc488f6 --- /dev/null +++ b/examples/redis-unstable/deps/fpconv/fpconv_powers.h | |||
| @@ -0,0 +1,133 @@ | |||
| 1 | /* fpconv_powers.h -- floating point conversion utilities. | ||
| 2 | * | ||
| 3 | * Fast and accurate double to string conversion based on Florian Loitsch's | ||
| 4 | * Grisu-algorithm[1]. | ||
| 5 | * | ||
| 6 | * [1] https://www.cs.tufts.edu/~nr/cs257/archive/florian-loitsch/printf.pdf | ||
| 7 | * ---------------------------------------------------------------------------- | ||
| 8 | * | ||
| 9 | * Copyright (c) 2021, Redis Labs | ||
| 10 | * Copyright (c) 2013-2019, night-shift <as.smljk at gmail dot com> | ||
| 11 | * Copyright (c) 2009, Florian Loitsch < florian.loitsch at inria dot fr > | ||
| 12 | * All rights reserved. | ||
| 13 | * | ||
| 14 | * Boost Software License - Version 1.0 - August 17th, 2003 | ||
| 15 | * | ||
| 16 | * Permission is hereby granted, free of charge, to any person or organization | ||
| 17 | * obtaining a copy of the software and accompanying documentation covered by | ||
| 18 | * this license (the "Software") to use, reproduce, display, distribute, | ||
| 19 | * execute, and transmit the Software, and to prepare derivative works of the | ||
| 20 | * Software, and to permit third-parties to whom the Software is furnished to | ||
| 21 | * do so, all subject to the following: | ||
| 22 | * | ||
| 23 | * The copyright notices in the Software and this entire statement, including | ||
| 24 | * the above license grant, this restriction and the following disclaimer, | ||
| 25 | * must be included in all copies of the Software, in whole or in part, and | ||
| 26 | * all derivative works of the Software, unless such copies or derivative | ||
| 27 | * works are solely in the form of machine-executable object code generated by | ||
| 28 | * a source language processor. | ||
| 29 | * | ||
| 30 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 31 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 32 | * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT | ||
| 33 | * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE | ||
| 34 | * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, | ||
| 35 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
| 36 | * DEALINGS IN THE SOFTWARE. | ||
| 37 | */ | ||
| 38 | |||
| 39 | #include <stdint.h> | ||
| 40 | |||
| 41 | #define npowers 87 | ||
| 42 | #define steppowers 8 | ||
| 43 | #define firstpower -348 /* 10 ^ -348 */ | ||
| 44 | |||
| 45 | #define expmax -32 | ||
| 46 | #define expmin -60 | ||
| 47 | |||
| 48 | |||
| 49 | typedef struct Fp { | ||
| 50 | uint64_t frac; | ||
| 51 | int exp; | ||
| 52 | } Fp; | ||
| 53 | |||
| 54 | static Fp powers_ten[] = { | ||
| 55 | { 18054884314459144840U, -1220 }, { 13451937075301367670U, -1193 }, | ||
| 56 | { 10022474136428063862U, -1166 }, { 14934650266808366570U, -1140 }, | ||
| 57 | { 11127181549972568877U, -1113 }, { 16580792590934885855U, -1087 }, | ||
| 58 | { 12353653155963782858U, -1060 }, { 18408377700990114895U, -1034 }, | ||
| 59 | { 13715310171984221708U, -1007 }, { 10218702384817765436U, -980 }, | ||
| 60 | { 15227053142812498563U, -954 }, { 11345038669416679861U, -927 }, | ||
| 61 | { 16905424996341287883U, -901 }, { 12595523146049147757U, -874 }, | ||
| 62 | { 9384396036005875287U, -847 }, { 13983839803942852151U, -821 }, | ||
| 63 | { 10418772551374772303U, -794 }, { 15525180923007089351U, -768 }, | ||
| 64 | { 11567161174868858868U, -741 }, { 17236413322193710309U, -715 }, | ||
| 65 | { 12842128665889583758U, -688 }, { 9568131466127621947U, -661 }, | ||
| 66 | { 14257626930069360058U, -635 }, { 10622759856335341974U, -608 }, | ||
| 67 | { 15829145694278690180U, -582 }, { 11793632577567316726U, -555 }, | ||
| 68 | { 17573882009934360870U, -529 }, { 13093562431584567480U, -502 }, | ||
| 69 | { 9755464219737475723U, -475 }, { 14536774485912137811U, -449 }, | ||
| 70 | { 10830740992659433045U, -422 }, { 16139061738043178685U, -396 }, | ||
| 71 | { 12024538023802026127U, -369 }, { 17917957937422433684U, -343 }, | ||
| 72 | { 13349918974505688015U, -316 }, { 9946464728195732843U, -289 }, | ||
| 73 | { 14821387422376473014U, -263 }, { 11042794154864902060U, -236 }, | ||
| 74 | { 16455045573212060422U, -210 }, { 12259964326927110867U, -183 }, | ||
| 75 | { 18268770466636286478U, -157 }, { 13611294676837538539U, -130 }, | ||
| 76 | { 10141204801825835212U, -103 }, { 15111572745182864684U, -77 }, | ||
| 77 | { 11258999068426240000U, -50 }, { 16777216000000000000U, -24 }, | ||
| 78 | { 12500000000000000000U, 3 }, { 9313225746154785156U, 30 }, | ||
| 79 | { 13877787807814456755U, 56 }, { 10339757656912845936U, 83 }, | ||
| 80 | { 15407439555097886824U, 109 }, { 11479437019748901445U, 136 }, | ||
| 81 | { 17105694144590052135U, 162 }, { 12744735289059618216U, 189 }, | ||
| 82 | { 9495567745759798747U, 216 }, { 14149498560666738074U, 242 }, | ||
| 83 | { 10542197943230523224U, 269 }, { 15709099088952724970U, 295 }, | ||
| 84 | { 11704190886730495818U, 322 }, { 17440603504673385349U, 348 }, | ||
| 85 | { 12994262207056124023U, 375 }, { 9681479787123295682U, 402 }, | ||
| 86 | { 14426529090290212157U, 428 }, { 10748601772107342003U, 455 }, | ||
| 87 | { 16016664761464807395U, 481 }, { 11933345169920330789U, 508 }, | ||
| 88 | { 17782069995880619868U, 534 }, { 13248674568444952270U, 561 }, | ||
| 89 | { 9871031767461413346U, 588 }, { 14708983551653345445U, 614 }, | ||
| 90 | { 10959046745042015199U, 641 }, { 16330252207878254650U, 667 }, | ||
| 91 | { 12166986024289022870U, 694 }, { 18130221999122236476U, 720 }, | ||
| 92 | { 13508068024458167312U, 747 }, { 10064294952495520794U, 774 }, | ||
| 93 | { 14996968138956309548U, 800 }, { 11173611982879273257U, 827 }, | ||
| 94 | { 16649979327439178909U, 853 }, { 12405201291620119593U, 880 }, | ||
| 95 | { 9242595204427927429U, 907 }, { 13772540099066387757U, 933 }, | ||
| 96 | { 10261342003245940623U, 960 }, { 15290591125556738113U, 986 }, | ||
| 97 | { 11392378155556871081U, 1013 }, { 16975966327722178521U, 1039 }, | ||
| 98 | { 12648080533535911531U, 1066 } | ||
| 99 | }; | ||
| 100 | |||
| 101 | /** | ||
| 102 | * Grisu needs a cache of precomputed powers-of-ten. | ||
| 103 | * This function, given an exponent and an integer k | ||
| 104 | * return the normalized floating-point approximation of the power of 10. | ||
| 105 | * @param exp | ||
| 106 | * @param k | ||
| 107 | * @return | ||
| 108 | */ | ||
| 109 | static Fp find_cachedpow10(int exp, int* k) | ||
| 110 | { | ||
| 111 | const double one_log_ten = 0.30102999566398114; | ||
| 112 | |||
| 113 | const int approx = -(exp + npowers) * one_log_ten; | ||
| 114 | int idx = (approx - firstpower) / steppowers; | ||
| 115 | |||
| 116 | while(1) { | ||
| 117 | int current = exp + powers_ten[idx].exp + 64; | ||
| 118 | |||
| 119 | if(current < expmin) { | ||
| 120 | idx++; | ||
| 121 | continue; | ||
| 122 | } | ||
| 123 | |||
| 124 | if(current > expmax) { | ||
| 125 | idx--; | ||
| 126 | continue; | ||
| 127 | } | ||
| 128 | |||
| 129 | *k = (firstpower + idx * steppowers); | ||
| 130 | |||
| 131 | return powers_ten[idx]; | ||
| 132 | } | ||
| 133 | } | ||
