All files main.js

98.65% Statements 147/149
92.3% Branches 12/13
100% Functions 1/1
98.65% Lines 147/149

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 1501x 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 3504x 3504x 3504x 3504x 3504x 3504x 3504x 3504x 3504x 3504x 3504x 3504x 1x 1x 3504x 1x 1x 3502x 3502x 3502x 3504x 501x 501x     501x 501x 501x 501x 501x 501x 3504x 1x 1x 3501x 3504x 1x 1x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3500x 3504x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2025 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 NINF = require( '@stdlib/constants/float32/ninf' );
var kernelLog1pf = require( '@stdlib/math/base/special/kernel-log1pf' );
 
 
// VARIABLES //
 
var TWO25 = f32( 3.3554432000e+07 );   // 0x4c000000
var IVLN2HI = f32( 1.4428710938e+00 ); /* 0x3fb8b000 */
var IVLN2LO = f32( -1.7605285393e-04 ); // 0x3de705fc, 0x2eefa200
var HIGH_MAX_NORMAL_EXP = 0x7f800000|0; // asm type annotation
var HIGH_MIN_NORMAL_EXP = 0x00800000|0; // asm type annotation
var HIGH_BIASED_EXP_0 = 0x3f800000|0; // asm type annotation
 
 
// MAIN //
 
/**
* Evaluates the binary logarithm (base two) for single-precision floating point values.
*
* @param {NonNegativeNumber} 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;
	}
	if ( x === 0.0 ) {
		return NINF;
	}
	x = f32( x );
	hx = toWordf( x ) | 0;
	k = 0|0; // asm type annotation
	if ( hx < HIGH_MIN_NORMAL_EXP ) {
		// 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;
	}
	if ( hx >= HIGH_MAX_NORMAL_EXP ) {
		return f32( x + x );
	}
	// Case: log(1) = +0
	if ( hx === HIGH_BIASED_EXP_0 ) {
		return 0.0;
	}
	k = f32( k + f32( ( ( hx >> 23 ) - FLOAT32_EXPONENT_BIAS ) | 0 ) );
	hx &= FLOAT32_SIGNIFICAND_MASK;
	i = f32( ( ( hx+0x4afb0d ) & HIGH_MIN_NORMAL_EXP )|0 ); // asm type annotation
 
	// Normalize x or x/2...
	x = fromWordf( hx | ( i ^ HIGH_BIASED_EXP_0) );
	k = f32( k + ( ( i >> 23 ) | 0 ) );
	y = f32( k );
	f = f32( x - 1.0 );
	hfsq = f32( 0.5 * f32( f * f ) );
	R = kernelLog1pf( f );
	hi = f32( f - hfsq );
	hx = toWordf( hi ) | 0;
	hi = fromWordf( hx & 0xfffff000);
	lo = f32( f32( f - hi ) - f32( hfsq ) + f32( R ) );
	return f32( f32( f32( lo + hi ) * IVLN2LO ) + f32( lo * IVLN2HI ) + f32( hi * IVLN2HI ) + f32( y ) ); // eslint-disable-line max-len
}
 
 
// EXPORTS //
 
module.exports = log2f;