[njs] Optimized conversion of negative numbers to integer.

Valentin Bartenev vbart at nginx.com
Sat Nov 19 19:18:10 UTC 2016


details:   http://hg.nginx.org/njs/rev/9f0f13857c46
branches:  
changeset: 265:9f0f13857c46
user:      Valentin Bartenev <vbart at nginx.com>
date:      Sat Nov 19 22:20:06 2016 +0300
description:
Optimized conversion of negative numbers to integer.

There is no reason to use fmod() for all negative numbers.
The numbers in range [-2^53, 2^53] can be converted directly
to int64_t.

diffstat:

 njs/njs_number.c |  15 ++++++++-------
 1 files changed, 8 insertions(+), 7 deletions(-)

diffs (27 lines):

diff -r 91dc07616b19 -r 9f0f13857c46 njs/njs_number.c
--- a/njs/njs_number.c	Sat Nov 19 22:20:06 2016 +0300
+++ b/njs/njs_number.c	Sat Nov 19 22:20:06 2016 +0300
@@ -749,15 +749,16 @@ njs_number_to_integer(double num)
     int64_t  i64;
 
     /*
-     * ECMAScript 5.1: integer must be modulo 2^32.
-     * 2^53 is the largest integer number which can be stored in the IEEE-754
-     * format and numbers less than 2^53 can be just converted to int64_t
-     * eliding more expensive fmod() operation.  Then the int64 integer is
-     * truncated to uint32_t.  The NaN can be converted to 0x8000000000000000
-     * and becomes 0 after truncation.  fmod() of the infinity returns NaN.
+     * ES5.1: integer must be modulo 2^32.
+     * 2^53 is the largest integer number which can be stored safely
+     * in the IEEE-754 format and numbers less than 2^53 can be just
+     * converted to int64_t eliding more expensive fmod() operation.
+     * Then the int64 integer is truncated to uint32_t.  The NaN is
+     * converted to 0x8000000000000000 and becomes 0 after truncation.
+     * fmod() of the Infinity returns NaN.
      */
 
-    if (num < 0 || num > 9007199254740992.0) {
+    if (fabs(num) > 9007199254740992.0) {
         i64 = fmod(num, 4294967296.0);
 
     } else {



More information about the nginx-devel mailing list