From: Valentin Bartenev Date: Sat, 19 Nov 2016 19:20:06 +0000 (+0300) Subject: Optimized conversion of negative numbers to integer. X-Git-Tag: 0.1.6~17 X-Git-Url: http://www.kaiwu.me/postgresql/commit/?a=commitdiff_plain;h=1696d6b801bb3861c6480148f9d6551c712c3828;p=njs.git 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. --- diff --git a/njs/njs_number.c b/njs/njs_number.c index 1e01a845..45e816e2 100644 --- a/njs/njs_number.c +++ b/njs/njs_number.c @@ -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 {