Skip to content

Commit 9cffe11

Browse files
committed
optimize double to int
1 parent 8d4a907 commit 9cffe11

File tree

10 files changed

+69
-27
lines changed

10 files changed

+69
-27
lines changed

xs/platforms/esp/xsHost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,10 @@ void selectionSort(void *base, size_t num, size_t width, int (*compare )(const v
449449
#define C_NAN NAN
450450
#define C_RAND_MAX INT32_MAX
451451

452+
#define C_FP_ILOGB0 FP_ILOGB0
453+
#define C_FP_ILOGBNAN FP_ILOGBNAN
454+
#define C_INT_MAX INT_MAX
455+
452456
#define c_acos acos
453457
#define c_acosh acosh
454458
#define c_asin asin
@@ -472,6 +476,7 @@ void selectionSort(void *base, size_t num, size_t width, int (*compare )(const v
472476
#endif
473477
#define c_fpclassify fpclassify
474478
#define c_hypot hypot
479+
#define c_ilogb ilogb
475480
#define c_isfinite isfinite
476481
#define c_isnormal isnormal
477482
#define c_isnan isnan

xs/platforms/gecko/xsHost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ typedef struct tm c_tm;
385385
#define C_NAN NAN
386386
#define C_RAND_MAX RAND_MAX
387387

388+
#define C_FP_ILOGB0 FP_ILOGB0
389+
#define C_FP_ILOGBNAN FP_ILOGBNAN
390+
#define C_INT_MAX INT_MAX
391+
388392
#define c_acos acos
389393
#define c_acosh acosh
390394
#define c_asin asin
@@ -403,6 +407,7 @@ typedef struct tm c_tm;
403407
#define c_fmod fmod
404408
#define c_fpclassify fpclassify
405409
#define c_hypot hypot
410+
#define c_ilogb ilogb
406411
#define c_isfinite isfinite
407412
#define c_isnormal isnormal
408413
#define c_isnan isnan

xs/platforms/nrf52/xsHost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,10 @@ extern void *pvPortRealloc(void *ptr, size_t size);
372372
#define C_NAN NAN
373373
#define C_RAND_MAX RAND_MAX
374374

375+
#define C_FP_ILOGB0 FP_ILOGB0
376+
#define C_FP_ILOGBNAN FP_ILOGBNAN
377+
#define C_INT_MAX INT_MAX
378+
375379
#define c_acos acos
376380
#define c_acosh acosh
377381
#define c_asin asin
@@ -390,6 +394,7 @@ extern void *pvPortRealloc(void *ptr, size_t size);
390394
#define c_fmod fmod
391395
#define c_fpclassify fpclassify
392396
#define c_hypot hypot
397+
#define c_ilogb ilogb
393398
#define c_isfinite isfinite
394399
#define c_isnormal isnormal
395400
#define c_isnan isnan

xs/platforms/openwrt/xsHost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,10 @@ typedef struct timeval c_timeval;
276276
#define C_NAN NAN
277277
#define C_RAND_MAX (0xFFFFFFFF)
278278

279+
#define C_FP_ILOGB0 FP_ILOGB0
280+
#define C_FP_ILOGBNAN FP_ILOGBNAN
281+
#define C_INT_MAX INT_MAX
282+
279283
#define c_acos acos
280284
#define c_acosh acosh
281285
#define c_asin asin
@@ -295,6 +299,7 @@ typedef struct timeval c_timeval;
295299
#define c_fmod fmod
296300
#define c_fpclassify fpclassify
297301
#define c_hypot hypot
302+
#define c_ilogb ilogb
298303
#define c_isfinite isfinite
299304
#define c_isnormal isnormal
300305
#define c_isnan isnan

xs/platforms/pico/xsHost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ extern void pico_reboot(uint32_t kind);
301301
#define C_NAN NAN
302302
#define C_RAND_MAX RAND_MAX
303303

304+
#define C_FP_ILOGB0 FP_ILOGB0
305+
#define C_FP_ILOGBNAN FP_ILOGBNAN
306+
#define C_INT_MAX INT_MAX
307+
304308
#define c_acos acos
305309
#define c_acosh acosh
306310
#define c_asin asin
@@ -319,6 +323,7 @@ extern void pico_reboot(uint32_t kind);
319323
#define c_fmod fmod
320324
#define c_fpclassify fpclassify
321325
#define c_hypot hypot
326+
#define c_ilogb ilogb
322327
#define c_isfinite isfinite
323328
#define c_isnormal isnormal
324329
#define c_isnan isnan

xs/platforms/qca4020/xsHost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,10 @@ extern void *my_malloc(size_t size);
338338
#define C_NAN NAN
339339
#define C_RAND_MAX RAND_MAX
340340

341+
#define C_FP_ILOGB0 FP_ILOGB0
342+
#define C_FP_ILOGBNAN FP_ILOGBNAN
343+
#define C_INT_MAX INT_MAX
344+
341345
#define c_acos acos
342346
#define c_acosh acosh
343347
#define c_asin asin
@@ -356,6 +360,7 @@ extern void *my_malloc(size_t size);
356360
#define c_fmod fmod
357361
#define c_fpclassify fpclassify
358362
#define c_hypot hypot
363+
#define c_ilogb ilogb
359364
#define c_isfinite isfinite
360365
#define c_isnormal isnormal
361366
#define c_isnan isnan

xs/platforms/xsPlatform.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,9 @@
434434
#ifndef c_isfinite
435435
#define c_isfinite isfinite
436436
#endif
437+
#ifndef c_ilogb
438+
#define c_ilogb ilogb
439+
#endif
437440
#ifndef c_isnormal
438441
#define c_isnormal isnormal
439442
#endif
@@ -492,6 +495,16 @@
492495
#define c_trunc trunc
493496
#endif
494497

498+
#ifndef C_FP_ILOGB0
499+
#define C_FP_ILOGB0 FP_ILOGB0
500+
#endif
501+
#ifndef C_FP_ILOGBNAN
502+
#define C_FP_ILOGBNAN FP_ILOGBNAN
503+
#endif
504+
#ifndef C_INT_MAX
505+
#define C_INT_MAX INT_MAX
506+
#endif
507+
495508
/* STRING */
496509

497510
#ifndef c_memcpy

xs/platforms/zephyr/xsHost.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,10 @@ c_tm *modLocalTime(const c_time_t *timep);
385385
#define C_NAN ((double)NAN)
386386
#define C_RAND_MAX RAND_MAX
387387

388+
#define C_FP_ILOGB0 FP_ILOGB0
389+
#define C_FP_ILOGBNAN FP_ILOGBNAN
390+
#define C_INT_MAX INT_MAX
391+
388392
#define c_acos acos
389393
#define c_acosh acosh
390394
#define c_asin asin
@@ -403,6 +407,7 @@ c_tm *modLocalTime(const c_time_t *timep);
403407
#define c_fmod fmod
404408
#define c_fpclassify fpclassify
405409
#define c_hypot hypot
410+
#define c_ilogb ilogb
406411
#define c_isfinite isfinite
407412
#define c_isnormal isnormal
408413
#define c_isnan isnan

xs/sources/xsAPI.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -166,22 +166,7 @@ txInteger fxToInteger(txMachine* the, txSlot* theSlot)
166166
break;
167167
case XS_NUMBER_KIND:
168168
theSlot->kind = XS_INTEGER_KIND;
169-
switch (c_fpclassify(theSlot->value.number)) {
170-
case C_FP_INFINITE:
171-
case C_FP_NAN:
172-
case C_FP_ZERO:
173-
theSlot->value.integer = 0;
174-
break;
175-
default: {
176-
#define MODULO 4294967296.0
177-
txNumber aNumber = c_fmod(c_trunc(theSlot->value.number), MODULO);
178-
if (aNumber >= MODULO / 2)
179-
aNumber -= MODULO;
180-
else if (aNumber < -MODULO / 2)
181-
aNumber += MODULO;
182-
theSlot->value.integer = (txInteger)aNumber;
183-
} break;
184-
}
169+
theSlot->value.integer = fxNumberToInteger(theSlot->value.number);
185170
mxFloatingPointOp("number to integer");
186171
break;
187172
case XS_STRING_KIND:

xs/sources/xsdtoa.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016-2017 Moddable Tech, Inc.
2+
* Copyright (c) 2016-2026 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*
@@ -6413,16 +6413,25 @@ txString fxIntegerToString(void* the, txInteger theValue, txString theBuffer, tx
64136413

64146414
txInteger fxNumberToInteger(txNumber theValue)
64156415
{
6416-
if (c_fpclassify(theValue) == C_FP_NORMAL) {
6417-
#define MODULO 4294967296.0
6418-
txNumber aNumber = c_fmod(c_trunc(theValue), MODULO);
6419-
if (aNumber >= MODULO / 2)
6420-
aNumber -= MODULO;
6421-
else if (aNumber < -MODULO / 2)
6422-
aNumber += MODULO;
6423-
return (txInteger)aNumber;
6424-
}
6425-
return 0;
6416+
int lb = c_ilogb(theValue);
6417+
if ((C_FP_ILOGB0 == lb) || (C_FP_ILOGBNAN == lb))
6418+
return 0;
6419+
6420+
if (lb < 31)
6421+
return (txInteger)theValue;
6422+
6423+
if (C_INT_MAX == lb)
6424+
return 0;
6425+
6426+
theValue = c_trunc(theValue);
6427+
#define MODULO 4294967296.0
6428+
theValue = c_fmod(theValue, MODULO);
6429+
if (theValue >= MODULO / 2)
6430+
theValue -= MODULO;
6431+
else if (theValue < -MODULO / 2)
6432+
theValue += MODULO;
6433+
6434+
return (txInteger)theValue;
64266435
}
64276436

64286437
txString fxNumberToString(void* the, txNumber theValue, txString theBuffer, txSize theSize, txByte theMode, txInteger thePrecision)

0 commit comments

Comments
 (0)