Skip to content

Commit 977adc3

Browse files
committed
Using printing and parsing library from QuickJS.
- Number.prototype.toString(radix) Improved accuracy for edge cases Reimplemented using njs_dtoa2() with JS_DTOA_FORMAT_FREE | JS_DTOA_EXP_DISABLED - Number.prototype.toFixed(frac) Reimplemented using njs_dtoa2() with JS_DTOA_FORMAT_FIXED Removed old njs_fixed_dtoa() implementation - Number.prototype.toPrecision(prec) Reimplemented using njs_dtoa2() with precision format Removed old njs_dtoa_precision() implementation - Number.prototype.toExponential(frac) Reimplemented using njs_dtoa2() with exponential format Removed old njs_dtoa_exponential() implementation - parseInt() Simplified parsing implementation Removed custom njs_number_radix_parse() helper - parseFloat() Simplified parsing implementation Removed custom njs_number_bin_parse(), njs_number_oct_parse(), njs_number_dec_parse() and njs_strtod.c module Better handling of large numbers and denormal floats and invalid inputs.
1 parent d4cedc0 commit 977adc3

File tree

18 files changed

+1997
-2163
lines changed

18 files changed

+1997
-2163
lines changed

auto/clang

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,3 @@ njs_feature_test="#include <sanitizer/msan_interface.h>
203203
return 0;
204204
}"
205205
. auto/feature
206-
207-
208-
njs_feature="_mm_setcsr()"
209-
njs_feature_name=NJS_HAVE_DENORMALS_CONTROL
210-
njs_feature_run=no
211-
njs_feature_incs=
212-
njs_feature_libs=
213-
njs_feature_test="#include <xmmintrin.h>
214-
int main(void) {
215-
_mm_setcsr(_mm_getcsr());
216-
return 0;
217-
}"
218-
. auto/feature

auto/sources

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
NJS_LIB_SRCS=" \
22
src/njs_diyfp.c \
33
src/njs_dtoa.c \
4-
src/njs_dtoa_fixed.c \
54
src/njs_str.c \
6-
src/njs_strtod.c \
75
src/njs_murmur_hash.c \
86
src/njs_djb_hash.c \
97
src/njs_utf8.c \

external/njs_shell.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,6 @@ njs_main(njs_opts_t *opts)
412412
njs_int_t ret;
413413
njs_engine_t *engine;
414414

415-
njs_mm_denormals(opts->denormals);
416-
417415
if (opts->file == NULL) {
418416
if (opts->command.length != 0) {
419417
opts->file = (char *) "string";
@@ -637,12 +635,6 @@ njs_options_parse(njs_opts_t *opts, int argc, char **argv)
637635
return NJS_ERROR;
638636

639637
case 'f':
640-
641-
#if !(NJS_HAVE_DENORMALS_CONTROL)
642-
njs_stderror("option \"-f\" is not supported\n");
643-
return NJS_ERROR;
644-
#endif
645-
646638
opts->denormals = 0;
647639
break;
648640

src/njs_clang.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,6 @@ njs_unsafe_cast_double_to_int64(double num)
209209
}
210210

211211

212-
#if (NJS_HAVE_DENORMALS_CONTROL)
213-
#include <xmmintrin.h>
214-
215-
/*
216-
* 0x8000 Flush to zero
217-
* 0x0040 Denormals are zeros
218-
*/
219-
220-
#define NJS_MM_DENORMALS_MASK 0x8040
221-
222-
#define njs_mm_denormals(on) \
223-
_mm_setcsr((_mm_getcsr() & ~NJS_MM_DENORMALS_MASK) | (!(on) ? 0x8040: 0x0))
224-
#else
225-
226-
#define njs_mm_denormals(on)
227-
#endif
228-
229-
230212
#ifndef NJS_MAX_ALIGNMENT
231213

232214
#if (NJS_SOLARIS)

src/njs_cutils.h

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Selected C utilities adapted from QuickJS cutils.h
3+
*
4+
* Copyright (c) 2024 Fabrice Bellard
5+
*/
6+
7+
#ifndef _NJS_CUTILS_H_INCLUDED_
8+
#define _NJS_CUTILS_H_INCLUDED_
9+
10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
#ifndef likely
14+
#define likely(x) __builtin_expect(!!(x), 1)
15+
#endif
16+
17+
#ifndef unlikely
18+
#define unlikely(x) __builtin_expect(!!(x), 0)
19+
#endif
20+
21+
#ifndef no_inline
22+
#define no_inline __attribute__((noinline))
23+
#endif
24+
25+
/* compatibility attribute used in dtoa implementation */
26+
#ifndef __maybe_unused
27+
#define __maybe_unused __attribute__((unused))
28+
#endif
29+
30+
typedef int BOOL;
31+
32+
#ifndef FALSE
33+
#define FALSE 0
34+
#endif
35+
36+
#ifndef TRUE
37+
#define TRUE 1
38+
#endif
39+
40+
static inline int
41+
max_int(int a, int b)
42+
{
43+
return (a > b) ? a : b;
44+
}
45+
46+
static inline int
47+
min_int(int a, int b)
48+
{
49+
return (a < b) ? a : b;
50+
}
51+
52+
static inline uint32_t
53+
max_uint32(uint32_t a, uint32_t b)
54+
{
55+
return (a > b) ? a : b;
56+
}
57+
58+
static inline uint32_t
59+
min_uint32(uint32_t a, uint32_t b)
60+
{
61+
return (a < b) ? a : b;
62+
}
63+
64+
static inline int64_t
65+
max_int64(int64_t a, int64_t b)
66+
{
67+
return (a > b) ? a : b;
68+
}
69+
70+
static inline int64_t
71+
min_int64(int64_t a, int64_t b)
72+
{
73+
return (a < b) ? a : b;
74+
}
75+
76+
/* WARNING: undefined if a = 0 */
77+
static inline int
78+
clz32(unsigned int a)
79+
{
80+
return __builtin_clz(a);
81+
}
82+
83+
/* WARNING: undefined if a = 0 */
84+
static inline int
85+
clz64(uint64_t a)
86+
{
87+
return __builtin_clzll(a);
88+
}
89+
90+
/* WARNING: undefined if a = 0 */
91+
static inline int
92+
ctz32(unsigned int a)
93+
{
94+
return __builtin_ctz(a);
95+
}
96+
97+
/* WARNING: undefined if a = 0 */
98+
static inline int
99+
ctz64(uint64_t a)
100+
{
101+
return __builtin_ctzll(a);
102+
}
103+
104+
static inline uint64_t
105+
float64_as_uint64(double d)
106+
{
107+
union {
108+
double d;
109+
uint64_t u64;
110+
} u;
111+
112+
u.d = d;
113+
return u.u64;
114+
}
115+
116+
static inline double
117+
uint64_as_float64(uint64_t u64)
118+
{
119+
union {
120+
double d;
121+
uint64_t u64;
122+
} u;
123+
124+
u.u64 = u64;
125+
return u.d;
126+
}
127+
128+
static inline int
129+
strstart(const char *str, const char *val, const char **ptr)
130+
{
131+
const char *p = str;
132+
const char *q = val;
133+
134+
while (*q != '\0') {
135+
if (*p != *q) {
136+
return 0;
137+
}
138+
p++;
139+
q++;
140+
}
141+
142+
if (ptr != NULL) {
143+
*ptr = p;
144+
}
145+
146+
return 1;
147+
}
148+
149+
#endif /* _NJS_CUTILS_H_INCLUDED_ */

0 commit comments

Comments
 (0)