Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3507x 3507x 3507x 3507x 3507x 3507x 3507x 3507x 3507x 3507x 3507x 3507x 3x 3x 3504x 3504x 3504x 3507x 503x 503x 2x 2x 501x 501x 501x 501x 501x 501x 3507x 1x 1x 3501x 3507x 1x 1x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3507x 1x 1x 1x 1x 1x | /**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* ## Notice
*
* The following copyright and license were part of the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/12.2.0/lib/msun/src/e_log2f.c}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
*
* Developed at SunPro, a Sun Microsystems, Inc. business.
* Permission to use, copy, modify, and distribute this
* software is freely granted, provided that this notice
* is preserved.
* ```
*/
'use strict';
// MODULES //
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var toWordf = require( '@stdlib/number/float32/base/to-word' );
var fromWordf = require( '@stdlib/number/float32/base/from-word' );
var ABS_MASK = require( '@stdlib/constants/float32/abs-mask' );
var FLOAT32_EXPONENT_BIAS = require( '@stdlib/constants/float32/exponent-bias' );
var FLOAT32_SIGNIFICAND_MASK = require( '@stdlib/constants/float32/significand-mask' );
var FLOAT32_EXPONENT_MASK = require( '@stdlib/constants/float32/exponent-mask' );
var FLOAT32_NUM_SIGNIFICAND_BITS = require( '@stdlib/constants/float32/num-significand-bits' );
var NINF = require( '@stdlib/constants/float32/ninf' );
var kernelLog1pf = require( '@stdlib/math/base/special/kernel-log1pf' );
// VARIABLES //
// 2^-126 => 0 00000001 00000000000000000000000 => 0x00800000 = 8388608
var MIN_NORMAL_WORD = 0x00800000|0; // asm type annotation
// 1 => 0 01111111 00000000000000000000000 => 0x3f800000 = 1065353216
var ONE_WORD = 0x3f800000|0; // asm type annotation
// Mask to clear lower 12 mantissa bits:
var TRUNC_MASK = 0xfffff000|0; // asm type annotation
// Normalization offset:
var NORM_OFFSET = 0x4afb0d|0; // asm type annotation
// 2^25 = 33554432 => 0 10011000 00000000000000000000000 => 0x4c000000 = 1275068416
var TWO25 = f32( 3.3554432000e+07 );
// 1/ln(2) high part = 1.4428710938 => 0 01111111 01110001011000000000000 => 0x3fb8b000 = 1068875776
var IVLN2HI = f32( 1.4428710938e+00 );
// 1/ln(2) low part = -1.7605285393e-04 => 1 01110011 00010001001101011010100 => 0xb9389ad4 = -1187821868
var IVLN2LO = f32( -1.7605285393e-04 );
// MAIN //
/**
* Evaluates the binary logarithm (base two) of a single-precision floating-point number.
*
* @param {number} x - input value
* @returns {number} function value
*
* @example
* var v = log2f( 4.0 );
* // returns 2.0
*
* @example
* var v = log2f( 8.0 );
* // returns 3.0
*
* @example
* var v = log2f( 0.0 );
* // returns -Infinity
*
* @example
* var v = log2f( Infinity );
* // returns Infinity
*
* @example
* var v = log2f( NaN );
* // returns NaN
*
* @example
* var v = log2f( -4.0 );
* // returns NaN
*/
function log2f( x ) {
var hfsq;
var hi;
var hx;
var lo;
var f;
var r;
var y;
var i;
var k;
if ( isnanf( x ) || x < 0.0 ) {
return NaN;
}
x = f32( x );
hx = toWordf( x ) | 0; // asm type annotation
k = 0|0; // asm type annotation
if ( hx < MIN_NORMAL_WORD ) {
// Case: x < 2**-126
if ( ( hx & ABS_MASK ) === 0 ) {
return NINF;
}
k -= 25|0; // asm type annotation
// Subnormal number, scale up x:
x = f32( x * TWO25 );
hx = toWordf( x ) | 0; // asm type annotation
}
if ( hx >= FLOAT32_EXPONENT_MASK ) {
return f32( x + x );
}
// Case: log(1) = +0
if ( hx === ONE_WORD ) {
return 0.0;
}
k += ( ( hx >> FLOAT32_NUM_SIGNIFICAND_BITS ) - FLOAT32_EXPONENT_BIAS ) | 0; // asm type annotation
hx &= FLOAT32_SIGNIFICAND_MASK;
i = ( ( hx + NORM_OFFSET ) & MIN_NORMAL_WORD ) | 0; // asm type annotation
// Normalize x or x/2...
x = fromWordf( hx | ( i ^ ONE_WORD ) );
k += ( i >> FLOAT32_NUM_SIGNIFICAND_BITS ) | 0; // asm type annotation
y = f32( k );
f = f32( x - f32( 1.0 ) );
hfsq = f32( f32( 0.5 ) * f32( f * f ) );
r = kernelLog1pf( f );
hi = f32( f - hfsq );
hx = toWordf( hi ) | 0; // asm type annotation
hi = fromWordf( hx & TRUNC_MASK );
lo = f32( f32( f32( f - hi ) - hfsq ) + r );
return f32( f32( f32( f32( f32( lo + hi ) * IVLN2LO ) + f32( lo * IVLN2HI ) ) + f32( hi * IVLN2HI ) ) + y ); // eslint-disable-line max-len
}
// EXPORTS //
module.exports = log2f;
|