All files / math/base/special/powf/lib pow2f.js

100% Statements 140/140
100% Branches 6/6
100% Functions 1/1
100% Lines 140/140

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 1411x 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 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 20795x 20795x 20795x 20795x 20795x 20795x 8782x 8782x 20795x 20795x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 21824x 2927x 21824x 18897x 18897x 21824x 21824x 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_powf.c}. The implementation follows the original, but has been modified for JavaScript.
*
* ```text
* Copyright (C) 2004 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 fromWordf = require( '@stdlib/number/float32/base/from-word' );
var toWordf = require( '@stdlib/number/float32/base/to-word' );
var f32 = require( '@stdlib/number/float64/base/to-float32' );
var ldexpf = require( '@stdlib/math/base/special/ldexpf' );
var LN2 = require( '@stdlib/constants/float32/ln-two' );
var BIAS = require( '@stdlib/constants/float32/exponent-bias' );
var ABS_MASK = require( '@stdlib/constants/float32/abs-mask' );
var SIGNIFICAND_MASK = require( '@stdlib/constants/float32/significand-mask' );
var NUM_SIGNIFICAND_BITS = require( '@stdlib/constants/float32/num-significand-bits' );
var polyvalP = require( './polyval_p.js' );
 
 
// VARIABLES //
 
// 1.1754943508222875e-38 => 0 00000001 00000000000000000000000 => 0x00800000 = 8388608
var MIN_NORM_WORD = 0x00800000|0; // asm type annotation
 
// 0.5 => 0 01111110 00000000000000000000000 => 0x3f000000 = 1056964608
var HALF_WORD = 0x3f000000|0; // asm type annotation
 
// Mask to clear lower 15 significand bits:
var TRUNC_MASK_15 = 0xffff8000|0; // asm type annotation
 
// High: LN2
var LN2_HI = f32( 6.93145752e-01 ); // 0x3f317200
 
// Low: LN2
var LN2_LO = f32( 1.42860654e-06 ); // 0x35bfbe8c
 
var ONE = f32( 1.0 );
var TWO = f32( 2.0 );
 
 
// MAIN //
 
/**
* Computes \\(2^{\mathrm{hp} + \mathrm{lp}\\).
*
* @private
* @param {number} j - word of `hp + lp`
* @param {number} hp - first power summand
* @param {number} lp - second power summand
* @returns {number} function value
*
* @example
* var z = pow2f( 1065961648, -0.3398475646972656, -0.000002438187359100815 );
* // returns ~0.79
*/
function pow2f( j, hp, lp ) {
	var tmp;
	var t1;
	var t;
	var r;
	var u;
	var v;
	var w;
	var z;
	var n;
	var i;
	var k;
 
	i = ( j & ABS_MASK ) |0; // asm type annotation
	k = ((i>>NUM_SIGNIFICAND_BITS) - BIAS)|0; // asm type annotation
	n = 0;
 
	// `|z| > 0.5`, set `n = z+0.5`
	if ( i > HALF_WORD ) {
		n = (j + (MIN_NORM_WORD>>(k+1)))|0; // asm type annotation
		k = (((n & ABS_MASK)>>NUM_SIGNIFICAND_BITS) - BIAS)|0; // new k for n
		tmp = ((n & ~(SIGNIFICAND_MASK >> k)))|0; // asm type annotation
		t = fromWordf( tmp );
		n = (((n & SIGNIFICAND_MASK)|MIN_NORM_WORD) >> (NUM_SIGNIFICAND_BITS-k))|0; // eslint-disable-line max-len
		if ( j < 0 ) {
			n = -n;
		}
		hp = f32( hp - t );
	}
	t = f32( lp + hp );
	tmp = toWordf( t )|0; // asm type annotation
	t = fromWordf( tmp & TRUNC_MASK_15 );
	u = f32( t * LN2_HI );
	v = f32( f32( f32( lp - f32(t-hp) )*LN2 ) + f32( t*LN2_LO ) );
	z = f32( u + v );
	w = f32( v - f32( z - u ) );
	t = f32( z * z );
	t1 = f32( z - f32( t*polyvalP( t ) ) );
	r = f32( f32( f32(z*t1) / f32(t1-TWO) ) - f32( w + f32(z*w) ) );
	z = f32( ONE - f32(r - z) );
	j = toWordf( z ) |0; // asm type annotation
	j += ( (n>>>0) << NUM_SIGNIFICAND_BITS ) |0; // asm type annotation
 
	// Check for subnormal output...
	if ( (j>>NUM_SIGNIFICAND_BITS) <= 0 ) {
		z = ldexpf( z, n );
	} else {
		z = fromWordf( j );
	}
	return z;
}
 
 
// EXPORTS //
 
module.exports = pow2f;