@stdlib/math-base-special-rempio2f 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js ADDED
@@ -0,0 +1,44 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ /**
22
+ * Compute `x - nπ/2 = r` (single precision).
23
+ *
24
+ * @module @stdlib/math-base-special-rempio2f
25
+ *
26
+ * @example
27
+ * var rempio2f = require( '@stdlib/math-base-special-rempio2f' );
28
+ *
29
+ * var y = [ 0.0 ];
30
+ * var n = rempio2f( 128.0, y );
31
+ * // returns 81
32
+ *
33
+ * var y1 = y[ 0 ];
34
+ * // returns ~0.765
35
+ */
36
+
37
+ // MODULES //
38
+
39
+ var rempio2f = require( './main.js' );
40
+
41
+
42
+ // EXPORTS //
43
+
44
+ module.exports = rempio2f;
@@ -0,0 +1,410 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ *
19
+ * ## Notice
20
+ *
21
+ * 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.
22
+ *
23
+ * ```text
24
+ * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
25
+ *
26
+ * Developed at SunPro, a Sun Microsystems, Inc. business.
27
+ * Permission to use, copy, modify, and distribute this
28
+ * software is freely granted, provided that this notice
29
+ * is preserved.
30
+ * ```
31
+ */
32
+
33
+ /* eslint-disable array-element-newline */
34
+
35
+ 'use strict';
36
+
37
+ // MODULES //
38
+
39
+ var floor = require( '@stdlib/math-base-special-floor' );
40
+ var ldexp = require( '@stdlib/math-base-special-ldexp' );
41
+ var zeros = require( '@stdlib/array-base-zeros' );
42
+
43
+
44
+ // VARIABLES //
45
+
46
+ /*
47
+ * Table of constants for `2/π` (`396` hex digits, `476` decimal).
48
+ *
49
+ * Integer array which contains the (`24*i`)-th to (`24*i+23`)-th bit of `2/π` after binary point. The corresponding floating value is
50
+ *
51
+ * ```tex
52
+ * \operatorname{ipio2}[i] \cdot 2^{-24(i+1)}
53
+ * ```
54
+ *
55
+ * This table must have at least `(e0-3)/24 + jk` terms. For single precision (`e0 <= 127`, `jk = 3`), this is `9`.
56
+ */
57
+ var IPIO2 = [
58
+ 0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62,
59
+ 0x95993C, 0x439041, 0xFE5163
60
+ ];
61
+
62
+ // Double precision array, obtained by cutting `π/2` into `24` bits chunks...
63
+ var PIO2 = [
64
+ 1.57079625129699707031e+00, // 0x3FF921FB, 0x40000000
65
+ 7.54978941586159635335e-08, // 0x3E74442D, 0x00000000
66
+ 5.39030252995776476554e-15, // 0x3CF84698, 0x80000000
67
+ 3.28200341580791294123e-22, // 0x3B78CC51, 0x60000000
68
+ 1.27065575308067607349e-29, // 0x39F01B83, 0x80000000
69
+ 1.22933308981111328932e-36, // 0x387A2520, 0x40000000
70
+ 2.73370053816464559624e-44, // 0x36E38222, 0x80000000
71
+ 2.16741683877804819444e-51 // 0x3569F31D, 0x00000000
72
+ ];
73
+ var TWO24 = 1.67772160000000000000e+07; // 0x41700000, 0x00000000
74
+ var TWON24 = 5.96046447753906250000e-08; // 0x3E700000, 0x00000000
75
+
76
+ // Arrays for storing temporary values (note that, in C, this is not thread safe):
77
+ var F = zeros( 20 );
78
+ var Q = zeros( 20 );
79
+ var FQ = zeros( 20 );
80
+ var IQ = zeros( 20 );
81
+
82
+
83
+ // FUNCTIONS //
84
+
85
+ /**
86
+ * Performs the computation for `kernelRempio2f()`.
87
+ *
88
+ * @private
89
+ * @param {PositiveNumber} x - input value
90
+ * @param {(Array|TypedArray|Object)} y - output object for storing double precision numbers
91
+ * @param {integer} jz - number of terms of `ipio2[]` used
92
+ * @param {Array<integer>} q - array with integral values, representing the 24-bits chunk of the product of `x` and `2/π`
93
+ * @param {integer} q0 - the corresponding exponent of `q[0]` (the exponent for `q[i]` would be `q0-24*i`)
94
+ * @param {integer} jk - `jk+1` is the initial number of terms of `IPIO2[]` needed in the computation
95
+ * @param {integer} jv - index for pointing to the suitable `ipio2[]` for the computation
96
+ * @param {integer} jx - `nx - 1`
97
+ * @param {Array<number>} f - `IPIO2[]` in floating point
98
+ * @returns {number} last three binary digits of `N`
99
+ */
100
+ function compute( x, y, jz, q, q0, jk, jv, jx, f ) {
101
+ var carry;
102
+ var fw;
103
+ var ih;
104
+ var jp;
105
+ var i;
106
+ var k;
107
+ var n;
108
+ var j;
109
+ var z;
110
+
111
+ // `jp+1` is the number of terms in `PIO2[]` needed:
112
+ jp = jk;
113
+
114
+ // Distill `q[]` into `IQ[]` in reverse order...
115
+ z = q[ jz ];
116
+ j = jz;
117
+ for ( i = 0; j > 0; i++ ) {
118
+ fw = ( TWON24 * z )|0;
119
+ IQ[ i ] = ( z - (TWO24*fw) )|0;
120
+ z = q[ j-1 ] + fw;
121
+ j -= 1;
122
+ }
123
+ // Compute `n`...
124
+ z = ldexp( z, q0 );
125
+ z -= 8.0 * floor( z*0.125 ); // Trim off integer >= 8
126
+ n = z|0;
127
+ z -= n;
128
+ ih = 0;
129
+ if ( q0 > 0 ) {
130
+ // Need `IQ[jz-1]` to determine `n`...
131
+ i = ( IQ[ jz-1 ] >> (24-q0) );
132
+ n += i;
133
+ IQ[ jz-1 ] -= ( i << (24-q0) );
134
+ ih = ( IQ[ jz-1 ] >> (23-q0) );
135
+ }
136
+ else if ( q0 === 0 ) {
137
+ ih = ( IQ[ jz-1 ] >> 23 );
138
+ }
139
+ else if ( z >= 0.5 ) {
140
+ ih = 2;
141
+ }
142
+ // Case: q > 0.5
143
+ if ( ih > 0 ) {
144
+ n += 1;
145
+ carry = 0;
146
+
147
+ // Compute `1-q`:
148
+ for ( i = 0; i < jz; i++ ) {
149
+ j = IQ[ i ];
150
+ if ( carry === 0 ) {
151
+ if ( j !== 0 ) {
152
+ carry = 1;
153
+ IQ[ i ] = 0x1000000 - j;
154
+ }
155
+ } else {
156
+ IQ[ i ] = 0xffffff - j;
157
+ }
158
+ }
159
+ if ( q0 > 0 ) {
160
+ // Rare case: chance is 1 in 12...
161
+ switch ( q0 ) { // eslint-disable-line default-case
162
+ case 1:
163
+ IQ[ jz-1 ] &= 0x7fffff;
164
+ break;
165
+ case 2:
166
+ IQ[ jz-1 ] &= 0x3fffff;
167
+ break;
168
+ }
169
+ }
170
+ if ( ih === 2 ) {
171
+ z = 1.0 - z;
172
+ if ( carry !== 0 ) {
173
+ z -= ldexp( 1.0, q0 );
174
+ }
175
+ }
176
+ }
177
+ // Check if re-computation is needed...
178
+ if ( z === 0.0 ) {
179
+ j = 0;
180
+ for ( i = jz-1; i >= jk; i-- ) {
181
+ j |= IQ[ i ];
182
+ }
183
+ if ( j === 0 ) {
184
+ // Need re-computation...
185
+ for ( k = 1; IQ[ jk-k ] === 0; k++ ) {
186
+ // `k` is the number of terms needed...
187
+ }
188
+ for ( i = jz+1; i <= jz+k; i++ ) {
189
+ // Add `q[jz+1]` to `q[jz+k]`...
190
+ f[ jx+i ] = IPIO2[ jv+i ];
191
+ fw = 0.0;
192
+ for ( j = 0; j <= jx; j++ ) {
193
+ fw += x[ j ] * f[ jx + (i-j) ];
194
+ }
195
+ q[ i ] = fw;
196
+ }
197
+ jz += k;
198
+ return compute( x, y, jz, q, q0, jk, jv, jx, f );
199
+ }
200
+ // Chop off zero terms...
201
+ jz -= 1;
202
+ q0 -= 24;
203
+ while ( IQ[ jz ] === 0 ) {
204
+ jz -= 1;
205
+ q0 -= 24;
206
+ }
207
+ } else {
208
+ // Break `z` into 24-bit if necessary...
209
+ z = ldexp( z, -q0 );
210
+ if ( z >= TWO24 ) {
211
+ fw = (TWON24*z)|0;
212
+ IQ[ jz ] = ( z - (TWO24*fw) )|0;
213
+ jz += 1;
214
+ q0 += 24;
215
+ IQ[ jz ] = fw;
216
+ } else {
217
+ IQ[ jz ] = z|0;
218
+ }
219
+ }
220
+ // Convert integer "bit" chunk to floating-point value...
221
+ fw = ldexp( 1.0, q0 );
222
+ for ( i = jz; i >= 0; i-- ) {
223
+ q[ i ] = fw * IQ[i];
224
+ fw *= TWON24;
225
+ }
226
+ // Compute `PIO2[0,...,jp]*q[jz,...,0]`...
227
+ for ( i = jz; i >= 0; i-- ) {
228
+ fw = 0.0;
229
+ for ( k = 0; k <= jp && k <= jz-i; k++ ) {
230
+ fw += PIO2[ k ] * q[ i+k ];
231
+ }
232
+ FQ[ jz-i ] = fw;
233
+ }
234
+ // Compress `FQ[]` into `y[]`...
235
+ fw = 0.0;
236
+ for ( i = jz; i >= 0; i-- ) {
237
+ fw += FQ[ i ];
238
+ }
239
+ if ( ih === 0 ) {
240
+ y[ 0 ] = fw;
241
+ } else {
242
+ y[ 0 ] = -fw;
243
+ }
244
+ return ( n & 7 );
245
+ }
246
+
247
+
248
+ // MAIN //
249
+
250
+ /**
251
+ * Returns the last three binary digits of `N` with `y = x - Nπ/2` so that `|y| < π/2` (single precision).
252
+ *
253
+ * ## Method
254
+ *
255
+ * - The method is to compute the integer (mod 8) and fraction parts of (2/π) * x without doing the full multiplication. In general, we skip the part of the product that are known to be a huge integer (more accurately, = 0 mod 8 ). Thus the number of operations are independent of the exponent of the input.
256
+ *
257
+ * - (2/π) is represented by an array of 24-bit integers in `ipio2[]`.
258
+ *
259
+ * - Input parameters:
260
+ *
261
+ * - `x[]` The input value (must be positive) is broken into `nx` pieces of 24-bit integers in double precision format. `x[i]` will be the i-th 24 bit of x. The scaled exponent of `x[0]` is given in input parameter `e0` (i.e., `x[0]*2^e0` match x's up to 24 bits).
262
+ *
263
+ * Example of breaking a double positive `z` into `x[0]+x[1]+x[2]`:
264
+ *
265
+ * ```tex
266
+ * e0 = \mathrm{ilogb}(z) - 23
267
+ * z = \mathrm{scalbn}(z, -e0)
268
+ * ```
269
+ *
270
+ * for `i = 0,1,2`
271
+ *
272
+ * ```tex
273
+ * x[i] = \lfloor z \rfloor
274
+ * z = (z - x[i]) \times 2^{24}
275
+ * ```
276
+ *
277
+ * - `y[]` output result in an array of double precision numbers.
278
+ *
279
+ * The dimension of `y[]` is:
280
+ * 24-bit precision 1
281
+ * 53-bit precision 2
282
+ * 64-bit precision 2
283
+ * 113-bit precision 3
284
+ *
285
+ * The actual value is the sum of them. Thus, for 113-bit precision, one may have to do something like:
286
+ *
287
+ * ```tex
288
+ * \mathrm{long\ double} \: t, w, r_{\text{head}}, r_{\text{tail}}; \\
289
+ * t &= (\mathrm{long\ double}) y[2] + (\mathrm{long\ double}) y[1]; \\
290
+ * w &= (\mathrm{long\ double}) y[0]; \\
291
+ * r_{\text{head}} &= t + w; \\
292
+ * r_{\text{tail}} &= w - (r_{\text{head}} - t);
293
+ * ```
294
+ *
295
+ * - `e0` The exponent of `x[0]`. Must be <= 16360 or you need to expand the `ipio2` table.
296
+ *
297
+ * - `nx` dimension of `x[]`
298
+ *
299
+ * - `prec` an integer indicating the precision:
300
+ * 0 24 bits (single)
301
+ * 1 53 bits (double)
302
+ * 2 64 bits (extended)
303
+ * 3 113 bits (quad)
304
+ *
305
+ * - External function:
306
+ *
307
+ * - double `scalbn()`, `floor()`;
308
+ *
309
+ * - Here is the description of some local variables:
310
+ *
311
+ * - `jk` `jk+1` is the initial number of terms of `ipio2[]` needed in the computation. The minimum and recommended value for `jk` is 3,4,4,6 for single, double, extended, and quad. `jk+1` must be 2 larger than you might expect so that our recomputation test works. (Up to 24 bits in the integer part (the 24 bits of it that we compute) and 23 bits in the fraction part may be lost to cancellation before we recompute.)
312
+ *
313
+ * - `jz` local integer variable indicating the number of terms of `ipio2[]` used.
314
+ *
315
+ * - `jx` `nx - 1`
316
+ *
317
+ * - `jv` index for pointing to the suitable `ipio2[]` for the computation. In general, we want
318
+ *
319
+ * ```tex
320
+ * \frac{{2^{e0} \cdot x[0] \cdot \mathrm{ipio2}[jv-1] \cdot 2^{-24jv}}}{{8}}
321
+ * ```
322
+ *
323
+ * to be an integer. Thus
324
+ *
325
+ * ```tex
326
+ * e0 - 3 - 24 \cdot jv \geq 0 \quad \text{or} \quad \frac{{e0 - 3}}{{24}} \geq jv
327
+ * ```
328
+ *
329
+ * Hence
330
+ *
331
+ * ```tex
332
+ * jv = \max(0, \frac{{e0 - 3}}{{24}})
333
+ * ```
334
+ *
335
+ * - `jp` `jp+1` is the number of terms in `PIo2[]` needed, `jp = jk`.
336
+ *
337
+ * - `q[]` double array with integral value, representing the 24-bits chunk of the product of `x` and `2/π`.
338
+ *
339
+ * - `q0` the corresponding exponent of `q[0]`. Note that the exponent for `q[i]` would be `q0-24*i`.
340
+ *
341
+ * - `PIo2[]` double precision array, obtained by cutting `π/2` into 24 bits chunks.
342
+ *
343
+ * - `f[]` `ipso2[]` in floating point
344
+ *
345
+ * - `iq[]` integer array by breaking up `q[]` in 24-bits chunk.
346
+ *
347
+ * - `fq[]` final product of `x*(2/π)` in `fq[0],..,fq[jk]`
348
+ *
349
+ * - `ih` integer. If >0 it indicates `q[]` is >= 0.5, hence it also indicates the _sign_ of the result.
350
+ *
351
+ * - Constants:
352
+ *
353
+ * - The hexadecimal values are the intended ones for the following constants. The decimal values may be used, provided that the compiler will convert from decimal to binary accurately enough to produce the hexadecimal values shown.
354
+ *
355
+ * @private
356
+ * @param {PositiveNumber} x - input value
357
+ * @param {(Array|TypedArray|Object)} y - remainder element
358
+ * @param {PositiveInteger} e0 - the exponent of `x[0]` (must be <= 127)
359
+ * @param {PositiveInteger} nx - dimension of `x[]`
360
+ * @returns {number} last three binary digits of `N`
361
+ */
362
+ function kernelRempio2f( x, y, e0, nx ) {
363
+ var fw;
364
+ var jk;
365
+ var jv;
366
+ var jx;
367
+ var jz;
368
+ var q0;
369
+ var i;
370
+ var j;
371
+ var m;
372
+
373
+ // Initialize `jk` for single-precision floating-point numbers:
374
+ jk = 3;
375
+
376
+ // Determine `jx`, `jv`, `q0` (note that `q0 < 3`):
377
+ jx = nx - 1;
378
+ jv = ( (e0 - 3) / 24 )|0;
379
+ if ( jv < 0 ) {
380
+ jv = 0;
381
+ }
382
+ q0 = e0 - (24 * (jv + 1));
383
+
384
+ // Set up `F[0]` to `F[jx+jk]` where `F[jx+jk] = IPIO2[jv+jk]`:
385
+ j = jv - jx;
386
+ m = jx + jk;
387
+ for ( i = 0; i <= m; i++ ) {
388
+ if ( j < 0 ) {
389
+ F[ i ] = 0.0;
390
+ } else {
391
+ F[ i ] = IPIO2[ j ];
392
+ }
393
+ j += 1;
394
+ }
395
+ // Compute `Q[0],Q[1],...,Q[jk]`:
396
+ for ( i = 0; i <= jk; i++ ) {
397
+ fw = 0.0;
398
+ for ( j = 0; j <= jx; j++ ) {
399
+ fw += x[ j ] * F[ jx + (i-j) ];
400
+ }
401
+ Q[ i ] = fw;
402
+ }
403
+ jz = jk;
404
+ return compute( x, y, jz, Q, q0, jk, jv, jx, F );
405
+ }
406
+
407
+
408
+ // EXPORTS //
409
+
410
+ module.exports = kernelRempio2f;
package/lib/main.js ADDED
@@ -0,0 +1,138 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ *
18
+ *
19
+ * ## Notice
20
+ *
21
+ * 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_rem_pio2f.c}. The implementation follows the original, but has been modified for JavaScript.
22
+ *
23
+ * ```text
24
+ * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
25
+ *
26
+ * Developed at SunPro, a Sun Microsystems, Inc. business.
27
+ * Permission to use, copy, modify, and distribute this
28
+ * software is freely granted, provided that this notice
29
+ * is preserved.
30
+ *
31
+ * Optimized by Bruce D. Evans.
32
+ * ```
33
+ */
34
+
35
+ 'use strict';
36
+
37
+ // MODULES //
38
+
39
+ var ABS_MASK = require( '@stdlib/constants-float32-abs-mask' );
40
+ var EXPONENT_MASK = require( '@stdlib/constants-float32-exponent-mask' );
41
+ var roundf = require( '@stdlib/math-base-special-roundf' );
42
+ var fromWordf = require( '@stdlib/number-float32-base-from-word' );
43
+ var toWordf = require( '@stdlib/number-float32-base-to-word' );
44
+ var float64ToFloat32 = require( '@stdlib/number-float64-base-to-float32' );
45
+ var rempio2Kernelf = require( './kernel_rempio2f.js' );
46
+
47
+
48
+ // VARIABLES //
49
+
50
+ // 53 bits of 2/π:
51
+ var INVPIO2 = 6.36619772367581382433e-01; // 0x3FE45F30, 0x6DC9C883
52
+
53
+ // First 25 bits of π/2:
54
+ var PIO2_1 = 1.57079631090164184570e+00; // 0x3FF921FB, 0x50000000
55
+
56
+ // PIO2_1T = π/2 - PIO2_1:
57
+ var PIO2_1T = 1.58932547735281966916e-08; // 0x3E5110b4, 0x611A6263
58
+
59
+ // 2^28*π/2 = 421657428.2663131 => 0100000110111001001000011111101101010100010001000010110100011000 => high word => 0x4dc90fdb = 1102651899 => 01000001101110010010000111111011
60
+ var MEDIUM = 0x4dc90fdb|0; // asm type annotation
61
+
62
+ // Arrays for storing temporary values:
63
+ var TX = [ 0.0 ];
64
+ var TY = [ 0.0 ];
65
+
66
+
67
+ // MAIN //
68
+
69
+ /**
70
+ * Computes `x - nπ/2 = r` (single precision).
71
+ *
72
+ * ## Notes
73
+ *
74
+ * - Returns `n` and stores the remainder `r` as `y[0]`.
75
+ *
76
+ * @param {number} x - input value
77
+ * @param {(Array|TypedArray|Object)} y - remainder element
78
+ * @returns {integer} factor of `π/2`
79
+ *
80
+ * @example
81
+ * var y = [ 0.0 ];
82
+ * var n = rempio2f( 128.0, y );
83
+ * // returns 81
84
+ *
85
+ * var y1 = y[ 0 ];
86
+ * // returns ~0.765
87
+ *
88
+ * @example
89
+ * var y = [ 0.0 ];
90
+ * var n = rempio2f( NaN, y );
91
+ * // returns 0
92
+ *
93
+ * var y1 = y[ 0 ];
94
+ * // returns NaN
95
+ */
96
+ function rempio2f( x, y ) {
97
+ var e0;
98
+ var hx;
99
+ var ix;
100
+ var n;
101
+ var r;
102
+ var w;
103
+ var z;
104
+
105
+ x = float64ToFloat32( x );
106
+ hx = toWordf( x ) | 0; // asm type annotation
107
+ ix = (hx & ABS_MASK)|0; // asm type annotation
108
+
109
+ // Case: |x| ~< 2^28*π/2 (medium size)
110
+ if ( ix < MEDIUM ) {
111
+ n = roundf( float64ToFloat32( x * INVPIO2 ) );
112
+ r = x - ( n * PIO2_1 );
113
+ w = n * PIO2_1T;
114
+ y[ 0 ] = r - w;
115
+ return n;
116
+ }
117
+ // Case: x is NaN or infinity
118
+ if ( ix >= EXPONENT_MASK ) {
119
+ y[ 0 ] = NaN;
120
+ return 0;
121
+ }
122
+ // Set z = scalbn(|x|, ilogb(|x|)-23)...
123
+ e0 = (ix >> 23) - 150; // `e0 = ilogb(|x|) - 23` => unbiased exponent minus 23
124
+ z = fromWordf( ix - ((e0 << 23)|0) );
125
+ TX[ 0 ] = z;
126
+ n = rempio2Kernelf( TX, TY, e0, 1 );
127
+ if ( hx < 0 ) {
128
+ y[ 0 ] = -TY[ 0 ];
129
+ return -n;
130
+ }
131
+ y[ 0 ] = TY[ 0 ];
132
+ return n;
133
+ }
134
+
135
+
136
+ // EXPORTS //
137
+
138
+ module.exports = rempio2f;
package/lib/native.js ADDED
@@ -0,0 +1,53 @@
1
+ /**
2
+ * @license Apache-2.0
3
+ *
4
+ * Copyright (c) 2025 The Stdlib Authors.
5
+ *
6
+ * Licensed under the Apache License, Version 2.0 (the "License");
7
+ * you may not use this file except in compliance with the License.
8
+ * You may obtain a copy of the License at
9
+ *
10
+ * http://www.apache.org/licenses/LICENSE-2.0
11
+ *
12
+ * Unless required by applicable law or agreed to in writing, software
13
+ * distributed under the License is distributed on an "AS IS" BASIS,
14
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ * See the License for the specific language governing permissions and
16
+ * limitations under the License.
17
+ */
18
+
19
+ 'use strict';
20
+
21
+ // MODULES //
22
+
23
+ var addon = require( './../src/addon.node' );
24
+
25
+
26
+ // MAIN //
27
+
28
+ /**
29
+ * Computes `x - nπ/2 = r` (single precision).
30
+ *
31
+ * @private
32
+ * @param {number} x - input value
33
+ * @param {Float64Array} y - remainder element
34
+ * @returns {integer} factor of `π/2`
35
+ *
36
+ * @example
37
+ * var Float64Array = require( '@stdlib/array-float64' );
38
+ *
39
+ * var y = new Float64Array( [ 0.0 ] );
40
+ * var n = rempio2f( 128.0, y );
41
+ * // returns 81
42
+ *
43
+ * var y1 = y[ 0 ];
44
+ * // returns ~0.765
45
+ */
46
+ function rempio2f( x, y ) {
47
+ return addon( x, y );
48
+ }
49
+
50
+
51
+ // EXPORTS //
52
+
53
+ module.exports = rempio2f;