All files / math/base/special/rempio2f/lib main.js

100% Statements 214/214
100% Branches 35/35
100% Functions 1/1
100% Lines 214/214

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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 2151x 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 1x 1x 1x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 20823x 404x 404x 404x 20419x 20823x 18x 18x 4x 4x 14x 18x 12x 7x 7x 7x 7x 5x 5x 5x 5x 18x 1x 1x 1x 1x 1x 1x 1x 1x 20401x 20823x 30x 30x 16x 2x 2x 16x 8x 8x 8x 8x 6x 6x 6x 6x 30x 2x 2x 30x 8x 8x 8x 8x 4x 4x 4x 4x 20371x 20823x 368x 368x 20003x 20003x 20823x 3x 3x 3x 20000x 20000x 20000x 20823x 40000x 40000x 40000x 20000x 20823x 81x 81x 20000x 20823x 4000x 4000x 4000x 16000x 16000x 20823x 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
*
* Adapted from the original implementation available as part of [FreeBSD]{@link https://svnweb.freebsd.org/base/release/9.3.0/lib/msun/src/e_rem_pio2.c}.
*
* ```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.
*
* Optimized by Bruce D. Evans.
* ```
*/
 
'use strict';
 
// MODULES //
 
var ABS_MASK = require( '@stdlib/constants/float32/abs-mask' ); // 0x7fffffff
var EXPONENT_MASK = require( '@stdlib/constants/float32/exponent-mask' ); // 0x7f800000
var SIGNIFICAND_MASK = require( '@stdlib/constants/float32/significand-mask' ); // 0x007fffff
var getWord = require( '@stdlib/number/float32/base/to-word' ); // Single word for 32-bit float
var fromWord = require( '@stdlib/number/float32/base/from-word' );
var rempio2fKernel = require( './kernel_rempio2f.js' ); // Adjusted kernel for float
var rempio2fMedium = require( './rempio2f_medium.js' ); // Adjusted medium range function
 
 
// VARIABLES //
 
var ZERO = 0.0; // 0x00000000
var TWO8 = 256.0; // 0x43800000 (2^8 for 32-bit scaling)
 
// 24 bits of π/2 (single precision):
var PIO2_1 = 1.57079632679; // 0x3fc90fdb
 
// PIO2_1T = π/2 - PIO2_1 (less precision):
var PIO2_1T = 4.960468e-8; // 0x33a22169
var TWO_PIO2_1T = 2.0 * PIO2_1T;
var THREE_PIO2_1T = 3.0 * PIO2_1T;
var FOUR_PIO2_1T = 4.0 * PIO2_1T;
 
// High word significand for π and π/2: 0x490fdb => 0010010010000111111011011
var PI_HIGH_WORD_SIGNIFICAND = 0x490fdb|0;
 
// High word constants (32-bit float representation):
var PIO4_HIGH_WORD = 0x3f490fdb|0;        // π/4: 0.785398163
var THREE_PIO4_HIGH_WORD = 0x4016cbe4|0;  // 3π/4: 2.35619449
var FIVE_PIO4_HIGH_WORD = 0x405497ce|0;   // 5π/4: 3.926990817
var THREE_PIO2_HIGH_WORD = 0x4096cbe4|0;  // 3π/2: 4.71238898
var SEVEN_PIO4_HIGH_WORD = 0x40b78909|0;  // 7π/4: 5.497787144
var TWO_PI_HIGH_WORD = 0x40c90fdb|0;      // 2π: 6.283185307
var NINE_PIO4_HIGH_WORD = 0x40fb53d1|0;   // 9π/4: 7.068583471
 
// Medium threshold: 2^8 * π/2 ≈ 402.123859659
var MEDIUM = 0x43c90fdb|0;
 
// Temporary arrays (smaller for single precision):
var TX = [ 0.0, 0.0 ];
var TY = [ 0.0 ];
 
 
// MAIN //
 
/**
* Computes `x - nπ/2 = r` for single-precision floating-point values.
*
* ## Notes
*
* -   Returns `n` and stores the remainder `r` in `y[0]`.
*
* @param {number} x - input value (single precision)
* @param {(Array|TypedArray|Object)} y - remainder element
* @returns {integer} factor of `π/2`
*
* @example
* var y = [ 0.0 ];
* var n = rempio2f( 128.0, y );
* // returns ~81
*
* var y0 = y[ 0 ];
* // returns ~0.765
*
* @example
* var y = [ 0.0 ];
* var n = rempio2f( NaN, y );
* // returns 0
*
* var y0 = y[ 0 ];
* // returns NaN
*/
function rempio2f( x, y ) {
	var e0;
	var ix;
	var nx;
	var w;
	var i;
	var n;
	var z;
 
	w = getWord( x );
	ix = (w & ABS_MASK)|0;
 
	// Case: |x| ~<= π/4
	if ( ix <= PIO4_HIGH_WORD ) {
		y[ 0 ] = x;
		return 0;
	}
	// Case: |x| ~<= 5π/4
	if ( ix <= FIVE_PIO4_HIGH_WORD ) {
		// Case: |x| ~= π/2 or π
		if ( (ix & SIGNIFICAND_MASK) === PI_HIGH_WORD_SIGNIFICAND ) {
			return rempio2fMedium( x, ix, y );
		}
		// Case: |x| ~<= 3π/4
		if ( ix <= THREE_PIO4_HIGH_WORD ) {
			if ( x > 0.0 ) {
				z = x - PIO2_1;
				y[ 0 ] = z - PIO2_1T;
				return 1;
			}
			z = x + PIO2_1;
			y[ 0 ] = z + PIO2_1T;
			return -1;
		}
		if ( x > 0.0 ) {
			z = x - ( 2.0 * PIO2_1 );
			y[ 0 ] = z - TWO_PIO2_1T;
			return 2;
		}
		z = x + ( 2.0 * PIO2_1 );
		y[ 0 ] = z + TWO_PIO2_1T;
		return -2;
	}
	// Case: |x| ~<= 9π/4
	if ( ix <= NINE_PIO4_HIGH_WORD ) {
		// Case: |x| ~<= 7π/4
		if ( ix <= SEVEN_PIO4_HIGH_WORD ) {
			if ( ix === THREE_PIO2_HIGH_WORD ) {
				return rempio2fMedium( x, ix, y );
			}
			if ( x > 0.0 ) {
				z = x - ( 3.0 * PIO2_1 );
				y[ 0 ] = z - THREE_PIO2_1T;
				return 3;
			}
			z = x + ( 3.0 * PIO2_1 );
			y[ 0 ] = z + THREE_PIO2_1T;
			return -3;
		}
		if ( ix === TWO_PI_HIGH_WORD ) {
			return rempio2fMedium( x, ix, y );
		}
		if ( x > 0.0 ) {
			z = x - ( 4.0 * PIO2_1 );
			y[ 0 ] = z - FOUR_PIO2_1T;
			return 4;
		}
		z = x + ( 4.0 * PIO2_1 );
		y[ 0 ] = z + FOUR_PIO2_1T;
		return -4;
	}
	// Case: |x| ~< 2^8 * π/2 (medium size)
	if ( ix < MEDIUM ) {
		return rempio2fMedium( x, ix, y );
	}
	// console.log("Inside kernel for x", x);
	// Case: x is NaN or infinity
	if ( ix >= EXPONENT_MASK ) {
		y[ 0 ] = NaN;
		return 0;
	}
	// Set z = scalbn(|x|, ilogb(x) - 7) for 8-bit chunks...
	e0 = (ix >> 23) - 134; // ilogb(x) - 7 (127 bias + 7 for 24-bit significand shift)
	z = fromWord( ix - ((e0 << 23)|0) );
	for ( i = 0; i < 2; i++ ) {
		TX[ i ] = z|0;
		z = (z - TX[i]) * TWO8;
	}
	nx = 2;
	while ( TX[ nx-1 ] === ZERO ) {
		nx -= 1;
	}
	n = rempio2fKernel( TX, TY, e0, nx );
	if ( x < 0.0 ) {
		y[ 0 ] = -TY[ 0 ];
		return -n;
	}
	y[ 0 ] = TY[ 0 ];
	return n;
}
 
 
// EXPORTS //
 
module.exports = rempio2f;