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

95.02% Statements 229/241
84.84% Branches 28/33
100% Functions 2/2
95.02% Lines 229/241

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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 2421x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 137484x 137484x 137484x 137484x 137484x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 39371x 488x 488x 488x 39371x 39371x 39371x 38742x 38742x 19371x 19371x 38742x 19371x 19371x     19371x 19371x 19371x 19371x 38665x 38665x 19371x 19371x 19371x 19371x 19371x 19371x 19371x 19371x 38742x       39371x 629x 629x 629x           629x 629x 629x 629x 20000x 20000x 20000x 39371x 80000x 80000x 80000x 20000x 20000x 39371x 80000x 80000x 200000x 200000x 80000x 80000x 20000x 20000x 20000x 39371x 80000x 80000x 39371x 39371x 39371x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x 20000x     20000x 20000x 20000x 20000x 20000x 20000x 99919x 99919x 99919x 20000x 20000x 20000x 80000x 80000x 159676x 159676x 80000x 80000x 20000x 20000x 20000x 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/9.3.0/lib/msun/src/k_rem_pio2.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 floorf = require( '@stdlib/math/base/special/floorf' );
var ldexpf = require( '@stdlib/math/base/special/ldexpf' );
var zeros = require( '@stdlib/array/base/zeros' );
 
 
// VARIABLES //
 
// Table of constants for 2/π (first few terms for single precision):
var IPIO2 = [
	0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62, 0x95993C, 0xD8B3D3, 0x4A9D0D
];
 
// π/2 for single precision (24-bit precision):
var PIO2 = [
	1.570796251,       // 0x3fc90fda
	4.960468e-8,       // 0x33522168
	2.5893025e-8,      // 0x32d849ba
	1.9539925e-11      // 0x2f2e71d2
];
var TWO24 = 1.6777216e7;  // 0x4b800000
var TWON24 = 5.9604645e-8; // 0x33800000
 
// Temporary arrays (smaller for single precision):
var F = zeros( 10 );
var Q = zeros( 10 );
var FQ = zeros( 10 );
var IQ = zeros( 10 );
 
 
// FUNCTIONS //
 
/**
* Performs the computation for `kernelRempio2f()`.
*
* @private
* @param {Array<number>} x - input value array
* @param {(Array|TypedArray|Object)} y - output array for single remainder
* @param {integer} jz - number of terms of `ipio2[]` used
* @param {Array<number>} q - array with integral values, representing the 24-bit chunks of the product of `x` and `2/π`
* @param {integer} q0 - the corresponding exponent of `q[0]` (exponent for `q[i]` is `q0-24*i`)
* @param {integer} jk - `jk+1` is the initial number of terms of `IPIO2[]` needed
* @param {integer} jv - index for pointing to the suitable `ipio2[]`
* @param {integer} jx - `nx - 1`
* @param {Array<number>} f - `IPIO2[]` in floating point
* @returns {number} last three binary digits of `N`
*/
function compute( x, y, jz, q, q0, jk, jv, jx, f ) {
	var fw;
	var ih;
	var jp;
	var i;
	var k;
	var n;
	var j;
	var z;
 
	// `jp+1` is the number of terms in `PIO2[]` needed:
	jp = jk;
 
	// Distill `q[]` into `IQ[]` in reverse order...
	z = q[jz];
	j = jz;
	for ( i = 0; j > 0; i++ ) {
		fw = (TWON24 * z)|0;
		IQ[i] = (z - (TWO24 * fw))|0;
		z = q[j - 1] + fw;
		j -= 1;
	}
 
	// Compute `n`...
	z = ldexpf( z, q0 );
	z -= 8.0 * floorf( z * 0.125 ); // Trim off integer >= 8
	n = z|0;
	z -= n;
	ih = (z >= 0.5) ? 2 : 0;
 
	// Case: z > 0.5
	if ( ih > 0 ) {
		n += 1;
		z = 1.0 - z; // No carry needed for single precision
	}
 
	// Check if re-computation is needed...
	if ( z === 0.0 ) {
		j = 0;
		for ( i = jz - 1; i >= jk; i-- ) {
			j |= IQ[i];
		}
		if ( j === 0 ) {
			// Need re-computation...
			for ( k = 1; IQ[jk - k] === 0; k++ ) {
				// `k` is the number of terms needed...
			}
			for ( i = jz + 1; i <= jz + k; i++ ) {
				f[jx + i] = IPIO2[jv + i];
				fw = 0.0;
				for ( j = 0; j <= jx; j++ ) {
					fw += x[j] * f[jx + (i - j)];
				}
				q[i] = fw;
			}
			jz += k;
			return compute( x, y, jz, q, q0, jk, jv, jx, f );
		}
		// Chop off zero terms...
		jz -= 1;
		q0 -= 24;
		while ( IQ[jz] === 0 && jz >= 0 ) {
			jz -= 1;
			q0 -= 24;
		}
	} else {
		// Break `z` into 24-bit chunks if necessary...
		z = ldexpf( z, -q0 );
		if ( z >= TWO24 ) {
			fw = (TWON24 * z)|0;
			IQ[jz] = (z - (TWO24 * fw))|0;
			jz += 1;
			q0 += 24;
			IQ[jz] = fw;
		} else {
			IQ[jz] = z|0;
		}
	}
 
	// Convert integer chunks to floating-point...
	fw = ldexpf( 1.0, q0 );
	for ( i = jz; i >= 0; i-- ) {
		q[i] = fw * IQ[i];
		fw *= TWON24;
	}
 
	// Compute `PIO2[0,...,jp] * q[jz,...,0]`...
	for ( i = jz; i >= 0; i-- ) {
		fw = 0.0;
		for ( k = 0; k <= jp && k <= jz - i; k++ ) {
			fw += PIO2[k] * q[i + k];
		}
		FQ[jz - i] = fw;
	}
 
	// Compress `FQ[]` into `y[]` (single value for single precision)...
	fw = 0.0;
	for ( i = jz; i >= 0; i-- ) {
		fw += FQ[i];
	}
	y[0] = (ih === 0) ? fw : -fw;
	return (n & 7);
}
 
/**
* Returns the last three binary digits of `N` with `y = x - Nπ/2` so that `|y| < π/2` using single precision.
*
* @private
* @param {Array<number>} x - input value array
* @param {(Array|TypedArray|Object)} y - remainder array (single value)
* @param {PositiveInteger} e0 - the exponent of `x[0]` (must be <= 127)
* @param {PositiveInteger} nx - dimension of `x[]`
* @returns {number} last three binary digits of `N`
*/
function kernelRempio2f( x, y, e0, nx ) {
	var fw;
	var jk;
	var jv;
	var jx;
	var jz;
	var q0;
	var i;
	var j;
	var m;
 
	// Initialize `jk` for single-precision:
	jk = 3;
 
	// Determine `jx`, `jv`, `q0`:
	jx = nx - 1;
	jv = ((e0 - 3) / 24)|0;
	if ( jv < 0 ) {
		jv = 0;
	}
	q0 = e0 - (24 * (jv + 1));
 
	// Set up `F[0]` to `F[jx+jk]`:
	j = jv - jx;
	m = jx + jk;
	for ( i = 0; i <= m; i++ ) {
		F[i] = (j < 0) ? 0.0 : IPIO2[j];
		j += 1;
	}
 
	// Compute `Q[0],Q[1],...,Q[jk]`:
	for ( i = 0; i <= jk; i++ ) {
		fw = 0.0;
		for ( j = 0; j <= jx; j++ ) {
			fw += x[j] * F[jx + (i - j)];
		}
		Q[i] = fw;
	}
	jz = jk;
	return compute( x, y, jz, Q, q0, jk, jv, jx, F );
}
 
 
// EXPORTS //
 
module.exports = kernelRempio2f;