]> git.kaiwu.me - njs.git/commitdiff
Optimized conversion of negative numbers to integer.
authorValentin Bartenev <vbart@nginx.com>
Sat, 19 Nov 2016 19:20:06 +0000 (22:20 +0300)
committerValentin Bartenev <vbart@nginx.com>
Sat, 19 Nov 2016 19:20:06 +0000 (22:20 +0300)
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.

njs/njs_number.c

index 1e01a84513b126744d6e3621fb18b226a8a9ee1f..45e816e258b20a7875ea55df5e44346506fe49ef 100644 (file)
@@ -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 {