Skip to content

Commit dc4773d

Browse files
committed
Reorganize fpconv vendoring
Make it a single file and declare the dependency.
1 parent cce3d1f commit dc4773d

File tree

7 files changed

+139
-153
lines changed

7 files changed

+139
-153
lines changed

ext/json/ext/generator/depend

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
generator.o: generator.c $(srcdir)/../fbuffer/fbuffer.h
2+
generator.o: generator.c $(srcdir)/../vendor/fpconv.c

ext/json/ext/generator/generator.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "ruby.h"
22
#include "../fbuffer/fbuffer.h"
3+
#include "../vendor/fpconv.c"
34

45
#include <math.h>
56
#include <ctype.h>
@@ -1050,8 +1051,6 @@ static void generate_json_integer(FBuffer *buffer, struct generate_json_data *da
10501051
}
10511052
#endif
10521053

1053-
#include "../vendor/fpconv/src/fpconv.c"
1054-
10551054
static void generate_json_float(FBuffer *buffer, struct generate_json_data *data, JSON_Generator_State *state, VALUE obj)
10561055
{
10571056
double value = RFLOAT_VALUE(obj);

ext/json/ext/vendor/fpconv/src/fpconv.c renamed to ext/json/ext/vendor/fpconv.c

+137-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,118 @@
1+
// Boost Software License - Version 1.0 - August 17th, 2003
2+
//
3+
// Permission is hereby granted, free of charge, to any person or organization
4+
// obtaining a copy of the software and accompanying documentation covered by
5+
// this license (the "Software") to use, reproduce, display, distribute,
6+
// execute, and transmit the Software, and to prepare derivative works of the
7+
// Software, and to permit third-parties to whom the Software is furnished to
8+
// do so, all subject to the following:
9+
//
10+
// The copyright notices in the Software and this entire statement, including
11+
// the above license grant, this restriction and the following disclaimer,
12+
// must be included in all copies of the Software, in whole or in part, and
13+
// all derivative works of the Software, unless such copies or derivative
14+
// works are solely in the form of machine-executable object code generated by
15+
// a source language processor.
16+
//
17+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20+
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21+
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22+
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
// DEALINGS IN THE SOFTWARE.
24+
25+
// The contents of this file is extracted from https://github.com/night-shift/fpconv
26+
// It was slightly modified to append ".0" to plain floats, for use with the https://github.com/ruby/json package.
27+
128
#include <stdbool.h>
229
#include <string.h>
30+
#include <stdint.h>
31+
32+
#define npowers 87
33+
#define steppowers 8
34+
#define firstpower -348 /* 10 ^ -348 */
35+
36+
#define expmax -32
37+
#define expmin -60
38+
39+
typedef struct Fp {
40+
uint64_t frac;
41+
int exp;
42+
} Fp;
43+
44+
static Fp powers_ten[] = {
45+
{ 18054884314459144840U, -1220 }, { 13451937075301367670U, -1193 },
46+
{ 10022474136428063862U, -1166 }, { 14934650266808366570U, -1140 },
47+
{ 11127181549972568877U, -1113 }, { 16580792590934885855U, -1087 },
48+
{ 12353653155963782858U, -1060 }, { 18408377700990114895U, -1034 },
49+
{ 13715310171984221708U, -1007 }, { 10218702384817765436U, -980 },
50+
{ 15227053142812498563U, -954 }, { 11345038669416679861U, -927 },
51+
{ 16905424996341287883U, -901 }, { 12595523146049147757U, -874 },
52+
{ 9384396036005875287U, -847 }, { 13983839803942852151U, -821 },
53+
{ 10418772551374772303U, -794 }, { 15525180923007089351U, -768 },
54+
{ 11567161174868858868U, -741 }, { 17236413322193710309U, -715 },
55+
{ 12842128665889583758U, -688 }, { 9568131466127621947U, -661 },
56+
{ 14257626930069360058U, -635 }, { 10622759856335341974U, -608 },
57+
{ 15829145694278690180U, -582 }, { 11793632577567316726U, -555 },
58+
{ 17573882009934360870U, -529 }, { 13093562431584567480U, -502 },
59+
{ 9755464219737475723U, -475 }, { 14536774485912137811U, -449 },
60+
{ 10830740992659433045U, -422 }, { 16139061738043178685U, -396 },
61+
{ 12024538023802026127U, -369 }, { 17917957937422433684U, -343 },
62+
{ 13349918974505688015U, -316 }, { 9946464728195732843U, -289 },
63+
{ 14821387422376473014U, -263 }, { 11042794154864902060U, -236 },
64+
{ 16455045573212060422U, -210 }, { 12259964326927110867U, -183 },
65+
{ 18268770466636286478U, -157 }, { 13611294676837538539U, -130 },
66+
{ 10141204801825835212U, -103 }, { 15111572745182864684U, -77 },
67+
{ 11258999068426240000U, -50 }, { 16777216000000000000U, -24 },
68+
{ 12500000000000000000U, 3 }, { 9313225746154785156U, 30 },
69+
{ 13877787807814456755U, 56 }, { 10339757656912845936U, 83 },
70+
{ 15407439555097886824U, 109 }, { 11479437019748901445U, 136 },
71+
{ 17105694144590052135U, 162 }, { 12744735289059618216U, 189 },
72+
{ 9495567745759798747U, 216 }, { 14149498560666738074U, 242 },
73+
{ 10542197943230523224U, 269 }, { 15709099088952724970U, 295 },
74+
{ 11704190886730495818U, 322 }, { 17440603504673385349U, 348 },
75+
{ 12994262207056124023U, 375 }, { 9681479787123295682U, 402 },
76+
{ 14426529090290212157U, 428 }, { 10748601772107342003U, 455 },
77+
{ 16016664761464807395U, 481 }, { 11933345169920330789U, 508 },
78+
{ 17782069995880619868U, 534 }, { 13248674568444952270U, 561 },
79+
{ 9871031767461413346U, 588 }, { 14708983551653345445U, 614 },
80+
{ 10959046745042015199U, 641 }, { 16330252207878254650U, 667 },
81+
{ 12166986024289022870U, 694 }, { 18130221999122236476U, 720 },
82+
{ 13508068024458167312U, 747 }, { 10064294952495520794U, 774 },
83+
{ 14996968138956309548U, 800 }, { 11173611982879273257U, 827 },
84+
{ 16649979327439178909U, 853 }, { 12405201291620119593U, 880 },
85+
{ 9242595204427927429U, 907 }, { 13772540099066387757U, 933 },
86+
{ 10261342003245940623U, 960 }, { 15290591125556738113U, 986 },
87+
{ 11392378155556871081U, 1013 }, { 16975966327722178521U, 1039 },
88+
{ 12648080533535911531U, 1066 }
89+
};
90+
91+
static Fp find_cachedpow10(int exp, int* k)
92+
{
93+
const double one_log_ten = 0.30102999566398114;
394

4-
#include "fpconv.h"
5-
#include "powers.h"
95+
int approx = -(exp + npowers) * one_log_ten;
96+
int idx = (approx - firstpower) / steppowers;
97+
98+
while(1) {
99+
int current = exp + powers_ten[idx].exp + 64;
100+
101+
if(current < expmin) {
102+
idx++;
103+
continue;
104+
}
105+
106+
if(current > expmax) {
107+
idx--;
108+
continue;
109+
}
110+
111+
*k = (firstpower + idx * steppowers);
112+
113+
return powers_ten[idx];
114+
}
115+
}
6116

7117
#define fracmask 0x000FFFFFFFFFFFFFU
8118
#define expmask 0x7FF0000000000000U
@@ -317,7 +427,31 @@ static int filter_special(double fp, char* dest)
317427
return 3;
318428
}
319429

320-
int fpconv_dtoa(double d, char dest[24])
430+
/* Fast and accurate double to string conversion based on Florian Loitsch's
431+
* Grisu-algorithm[1].
432+
*
433+
* Input:
434+
* fp -> the double to convert, dest -> destination buffer.
435+
* The generated string will never be longer than 24 characters.
436+
* Make sure to pass a pointer to at least 24 bytes of memory.
437+
* The emitted string will not be null terminated.
438+
*
439+
* Output:
440+
* The number of written characters.
441+
*
442+
* Exemplary usage:
443+
*
444+
* void print(double d)
445+
* {
446+
* char buf[24 + 1] // plus null terminator
447+
* int str_len = fpconv_dtoa(d, buf);
448+
*
449+
* buf[str_len] = '\0';
450+
* printf("%s", buf);
451+
* }
452+
*
453+
*/
454+
static int fpconv_dtoa(double d, char dest[24])
321455
{
322456
char digits[18];
323457

ext/json/ext/vendor/fpconv/README.md

-5
This file was deleted.

ext/json/ext/vendor/fpconv/license

-23
This file was deleted.

ext/json/ext/vendor/fpconv/src/fpconv.h

-33
This file was deleted.

ext/json/ext/vendor/fpconv/src/powers.h

-87
This file was deleted.

0 commit comments

Comments
 (0)