log2.js (4205B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 * 18 * 19 * ## Notice 20 * 21 * The following copyright and license were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/e_log2.c}. The implementation follows the original, but has been modified for JavaScript. 22 * 23 * ```text 24 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 25 * 26 * Developed at SunPro, a Sun Microsystems, Inc. business. 27 * Permission to use, copy, modify, and distribute this 28 * software is freely granted, provided that this notice 29 * is preserved. 30 * ``` 31 */ 32 33 'use strict'; 34 35 // MODULES // 36 37 var getHighWord = require( '@stdlib/number/float64/base/get-high-word' ); 38 var setHighWord = require( '@stdlib/number/float64/base/set-high-word' ); 39 var setLowWord = require( '@stdlib/number/float64/base/set-low-word' ); 40 var toWords = require( '@stdlib/number/float64/base/to-words' ); 41 var isnan = require( './../../../../base/assert/is-nan' ); 42 var BIAS = require( '@stdlib/constants/float64/exponent-bias' ); 43 var NINF = require( '@stdlib/constants/float64/ninf' ); 44 var klog = require( './klog.js' ); 45 46 47 // VARIABLES // 48 49 var TWO54 = 1.80143985094819840000e+16; // 0x43500000, 0x00000000 50 var IVLN2HI = 1.44269504072144627571e+00; // 0x3ff71547, 0x65200000 51 var IVLN2LO = 1.67517131648865118353e-10; // 0x3de705fc, 0x2eefa200 52 53 // 0x000fffff = 1048575 => 0 00000000000 11111111111111111111 54 var HIGH_SIGNIFICAND_MASK = 0x000fffff|0; // asm type annotation 55 56 // 0x7ff00000 = 2146435072 => 0 11111111111 00000000000000000000 => biased exponent: 2047 = 1023+1023 => 2^1023 57 var HIGH_MAX_NORMAL_EXP = 0x7ff00000|0; // asm type annotation 58 59 // 0x00100000 = 1048576 => 0 00000000001 00000000000000000000 => biased exponent: 1 = -1022+1023 => 2^-1022 60 var HIGH_MIN_NORMAL_EXP = 0x00100000|0; // asm type annotation 61 62 // 0x3ff00000 = 1072693248 => 0 01111111111 00000000000000000000 => biased exponent: 1023 = 0+1023 => 2^0 = 1 63 var HIGH_BIASED_EXP_0 = 0x3ff00000|0; // asm type annotation 64 65 // 0x7fffffff = 2147483647 => 0 11111111111 11111111111111111111 66 var ABS_MASK = 0x7fffffff|0; // asm type annotation 67 68 // High/low words workspace: 69 var WORDS = [ 0|0, 0|0 ]; // WARNING: not thread safe 70 71 72 // MAIN // 73 74 /** 75 * Evaluates the binary logarithm (base two). 76 * 77 * @param {NonNegativeNumber} x - input value 78 * @returns {number} function value 79 * 80 * @example 81 * var v = log2( 4.0 ); 82 * // returns 2.0 83 * 84 * @example 85 * var v = log2( 8.0 ); 86 * // returns 3.0 87 * 88 * @example 89 * var v = log2( 0.0 ); 90 * // returns -Infinity 91 * 92 * @example 93 * var v = log2( Infinity ); 94 * // returns Infinity 95 * 96 * @example 97 * var v = log2( NaN ); 98 * // returns NaN 99 * 100 * @example 101 * var v = log2( -4.0 ); 102 * // returns NaN 103 */ 104 function log2( x ) { 105 var hi; 106 var lo; 107 var hx; 108 var lx; 109 var f; 110 var i; 111 var k; 112 113 if ( isnan( x ) || x < 0.0 ) { 114 return NaN; 115 } 116 toWords( WORDS, x ); 117 hx = WORDS[ 0 ]; 118 lx = WORDS[ 1 ]; 119 k = 0|0; // asm type annotation 120 if ( hx < HIGH_MIN_NORMAL_EXP ) { 121 // Case: x < 2**-1022 122 if ( ( (hx&ABS_MASK) | lx ) === 0 ) { 123 return NINF; 124 } 125 k -= 54|0; // asm type annotation 126 127 // Subnormal number, scale up x: 128 x *= TWO54; 129 hx = getHighWord( x ); 130 } 131 if ( hx >= HIGH_MAX_NORMAL_EXP ) { 132 return x + x; 133 } 134 k += ( (hx>>20) - BIAS )|0; // asm type annotation 135 hx &= HIGH_SIGNIFICAND_MASK; 136 i = ( ( hx+0x95f64 ) & 0x100000 )|0; // asm type annotation 137 138 // Normalize x or x/2... 139 x = setHighWord( x, hx|(i^HIGH_BIASED_EXP_0) ); 140 k += (i>>20)|0; // asm type annotation 141 f = klog( x ); 142 x -= 1; 143 hi = setLowWord( x, 0 ); 144 lo = x - hi; 145 return ( (x+f)*IVLN2LO ) + ( (lo+f)*IVLN2HI ) + ( hi*IVLN2HI ) + k; 146 } 147 148 149 // EXPORTS // 150 151 module.exports = log2;