@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/manifest.json ADDED
@@ -0,0 +1,94 @@
1
+ {
2
+ "options": {
3
+ "task": "build"
4
+ },
5
+ "fields": [
6
+ {
7
+ "field": "src",
8
+ "resolve": true,
9
+ "relative": true
10
+ },
11
+ {
12
+ "field": "include",
13
+ "resolve": true,
14
+ "relative": true
15
+ },
16
+ {
17
+ "field": "libraries",
18
+ "resolve": false,
19
+ "relative": false
20
+ },
21
+ {
22
+ "field": "libpath",
23
+ "resolve": true,
24
+ "relative": false
25
+ }
26
+ ],
27
+ "confs": [
28
+ {
29
+ "task": "build",
30
+ "src": [
31
+ "./src/main.c"
32
+ ],
33
+ "include": [
34
+ "./include"
35
+ ],
36
+ "libraries": [],
37
+ "libpath": [],
38
+ "dependencies": [
39
+ "@stdlib/napi-argv",
40
+ "@stdlib/napi-argv-float",
41
+ "@stdlib/napi-argv-float64array",
42
+ "@stdlib/napi-create-double",
43
+ "@stdlib/napi-export",
44
+ "@stdlib/math-base-special-roundf",
45
+ "@stdlib/math-base-special-floor",
46
+ "@stdlib/math-base-special-ldexp",
47
+ "@stdlib/number-float32-base-from-word",
48
+ "@stdlib/number-float32-base-to-word",
49
+ "@stdlib/constants-float32-abs-mask",
50
+ "@stdlib/constants-float32-exponent-mask"
51
+ ]
52
+ },
53
+ {
54
+ "task": "benchmark",
55
+ "src": [
56
+ "./src/main.c"
57
+ ],
58
+ "include": [
59
+ "./include"
60
+ ],
61
+ "libraries": [],
62
+ "libpath": [],
63
+ "dependencies": [
64
+ "@stdlib/math-base-special-roundf",
65
+ "@stdlib/math-base-special-floor",
66
+ "@stdlib/math-base-special-ldexp",
67
+ "@stdlib/number-float32-base-from-word",
68
+ "@stdlib/number-float32-base-to-word",
69
+ "@stdlib/constants-float32-abs-mask",
70
+ "@stdlib/constants-float32-exponent-mask"
71
+ ]
72
+ },
73
+ {
74
+ "task": "examples",
75
+ "src": [
76
+ "./src/main.c"
77
+ ],
78
+ "include": [
79
+ "./include"
80
+ ],
81
+ "libraries": [],
82
+ "libpath": [],
83
+ "dependencies": [
84
+ "@stdlib/math-base-special-roundf",
85
+ "@stdlib/math-base-special-floor",
86
+ "@stdlib/math-base-special-ldexp",
87
+ "@stdlib/number-float32-base-from-word",
88
+ "@stdlib/number-float32-base-to-word",
89
+ "@stdlib/constants-float32-abs-mask",
90
+ "@stdlib/constants-float32-exponent-mask"
91
+ ]
92
+ }
93
+ ]
94
+ }
package/package.json ADDED
@@ -0,0 +1,84 @@
1
+ {
2
+ "name": "@stdlib/math-base-special-rempio2f",
3
+ "version": "0.1.0",
4
+ "description": "Compute `x - nπ/2 = r`.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "gypfile": false,
18
+ "directories": {
19
+ "doc": "./docs",
20
+ "include": "./include",
21
+ "lib": "./lib",
22
+ "src": "./src",
23
+ "dist": "./dist"
24
+ },
25
+ "types": "./docs/types",
26
+ "scripts": {},
27
+ "homepage": "https://stdlib.io",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git://github.com/stdlib-js/math-base-special-rempio2f.git"
31
+ },
32
+ "bugs": {
33
+ "url": "https://github.com/stdlib-js/stdlib/issues"
34
+ },
35
+ "dependencies": {
36
+ "@stdlib/array-base-zeros": "^0.2.2",
37
+ "@stdlib/constants-float32-abs-mask": "^0.2.2",
38
+ "@stdlib/constants-float32-exponent-mask": "^0.2.2",
39
+ "@stdlib/math-base-special-floor": "^0.2.3",
40
+ "@stdlib/math-base-special-ldexp": "^0.2.4",
41
+ "@stdlib/math-base-special-roundf": "^0.1.0",
42
+ "@stdlib/napi-argv": "^0.2.2",
43
+ "@stdlib/napi-argv-float": "^0.2.2",
44
+ "@stdlib/napi-argv-float64array": "^0.2.2",
45
+ "@stdlib/napi-create-double": "^0.0.2",
46
+ "@stdlib/napi-export": "^0.3.0",
47
+ "@stdlib/number-float32-base-from-word": "^0.2.2",
48
+ "@stdlib/number-float32-base-to-word": "^0.2.2",
49
+ "@stdlib/number-float64-base-to-float32": "^0.2.2",
50
+ "@stdlib/utils-library-manifest": "^0.2.3"
51
+ },
52
+ "devDependencies": {},
53
+ "engines": {
54
+ "node": ">=0.10.0",
55
+ "npm": ">2.7.0"
56
+ },
57
+ "os": [
58
+ "aix",
59
+ "darwin",
60
+ "freebsd",
61
+ "linux",
62
+ "macos",
63
+ "openbsd",
64
+ "sunos",
65
+ "win32",
66
+ "windows"
67
+ ],
68
+ "keywords": [
69
+ "stdlib",
70
+ "stdmath",
71
+ "mathematics",
72
+ "math",
73
+ "remainder",
74
+ "division",
75
+ "pi",
76
+ "pio2",
77
+ "pio2f",
78
+ "precision"
79
+ ],
80
+ "funding": {
81
+ "type": "opencollective",
82
+ "url": "https://opencollective.com/stdlib"
83
+ }
84
+ }
package/src/addon.c ADDED
@@ -0,0 +1,42 @@
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
+ #include "stdlib/math/base/special/rempio2f.h"
20
+ #include "stdlib/napi/argv.h"
21
+ #include "stdlib/napi/argv_float.h"
22
+ #include "stdlib/napi/argv_float64array.h"
23
+ #include "stdlib/napi/create_double.h"
24
+ #include "stdlib/napi/export.h"
25
+ #include <node_api.h>
26
+
27
+ /**
28
+ * Receives JavaScript callback invocation data.
29
+ *
30
+ * @param env environment under which the function is invoked
31
+ * @param info callback data
32
+ * @return Node-API value
33
+ */
34
+ static napi_value addon( napi_env env, napi_callback_info info ) {
35
+ STDLIB_NAPI_ARGV( env, info, argv, argc, 2 );
36
+ STDLIB_NAPI_ARGV_FLOAT( env, x, argv, 0 );
37
+ STDLIB_NAPI_ARGV_FLOAT64ARRAY( env, y, ylen, argv, 1 );
38
+ STDLIB_NAPI_CREATE_DOUBLE( env, stdlib_base_rempio2f( x, &y[0] ), n );
39
+ return n;
40
+ }
41
+
42
+ STDLIB_NAPI_MODULE_EXPORT_FCN( addon )
package/src/main.c ADDED
@@ -0,0 +1,438 @@
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, license, and long comment 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 according to project conventions.
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
+ #include "stdlib/math/base/special/rempio2f.h"
34
+ #include "stdlib/math/base/special/roundf.h"
35
+ #include "stdlib/math/base/special/floor.h"
36
+ #include "stdlib/math/base/special/ldexp.h"
37
+ #include "stdlib/number/float32/base/from_word.h"
38
+ #include "stdlib/number/float32/base/to_word.h"
39
+ #include "stdlib/constants/float32/abs_mask.h"
40
+ #include "stdlib/constants/float32/exponent_mask.h"
41
+ #include <stdint.h>
42
+
43
+ static const double ZERO = 0.00000000000000000000e+00; // 0x00000000, 0x00000000
44
+ static const double TWO24 = 1.67772160000000000000e+07; // 0x41700000, 0x00000000
45
+ static const double TWON24 = 5.96046447753906250000e-08; // 0x3E700000, 0x00000000
46
+ static const double INVPIO2 = 6.36619772367581382433e-01; // 0x3FE45F30, 0x6DC9C883
47
+ static const double PIO2_1 = 1.57079631090164184570e+00; // 0x3FF921FB, 0x50000000
48
+ static const double PIO2_1T = 1.58932547735281966916e-08; // 0x3E5110b4, 0x611A6263
49
+
50
+ // 2^28*π/2 = 421657428.2663131 => 0100000110111001001000011111101101010100010001000010110100011000 => high word => 0x4dc90fdb = 1102651899 => 01000001101110010010000111111011
51
+ static const int32_t MEDIUM = 0x4dc90fdb;
52
+
53
+
54
+ /*
55
+ * Table of constants for `2/π` (`396` hex digits, `476` decimal).
56
+ *
57
+ * Integer array which contains the (`24*i`)-th to (`24*i+23`)-th bit of `2/π` after binary point. The corresponding floating value is
58
+ *
59
+ * ```tex
60
+ * \operatorname{ipio2}[i] \cdot 2^{-24(i+1)}
61
+ * ```
62
+ *
63
+ * This table must have at least `(e0-3)/24 + jk` terms. For single precision (`e0 <= 127`, `jk = 3`), this is `9`.
64
+ */
65
+ static const int32_t IPIO2[] = {
66
+ 0xA2F983, 0x6E4E44, 0x1529FC, 0x2757D1, 0xF534DD, 0xC0DB62,
67
+ 0x95993C, 0x439041, 0xFE5163
68
+ };
69
+
70
+ // Double precision array, obtained by cutting `π/2` into `24` bits chunks...
71
+ static const double PIO2[] = {
72
+ 1.57079625129699707031e+00, // 0x3FF921FB, 0x40000000
73
+ 7.54978941586159635335e-08, // 0x3E74442D, 0x00000000
74
+ 5.39030252995776476554e-15, // 0x3CF84698, 0x80000000
75
+ 3.28200341580791294123e-22, // 0x3B78CC51, 0x60000000
76
+ 1.27065575308067607349e-29, // 0x39F01B83, 0x80000000
77
+ 1.22933308981111328932e-36, // 0x387A2520, 0x40000000
78
+ 2.73370053816464559624e-44, // 0x36E38222, 0x80000000
79
+ 2.16741683877804819444e-51 // 0x3569F31D, 0x00000000
80
+ };
81
+
82
+ /**
83
+ * Returns the last three binary digits of `N` with `y = x - Nπ/2` so that `|y| < π/2` (single-precision).
84
+ *
85
+ * ## Method
86
+ *
87
+ * - 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.
88
+ *
89
+ * - (2/π) is represented by an array of 24-bit integers in `ipio2[]`.
90
+ *
91
+ * - Input parameters:
92
+ *
93
+ * - `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).
94
+ *
95
+ * Example of breaking a double positive `z` into `x[0]+x[1]+x[2]`:
96
+ *
97
+ * ```tex
98
+ * e0 = \mathrm{ilogb}(z) - 23
99
+ * z = \mathrm{scalbn}(z, -e0)
100
+ * ```
101
+ *
102
+ * for `i = 0,1,2`
103
+ *
104
+ * ```tex
105
+ * x[i] = \lfloor z \rfloor
106
+ * z = (z - x[i]) \times 2^{24}
107
+ * ```
108
+ *
109
+ * - `y[]` output result in an array of double precision numbers.
110
+ *
111
+ * The dimension of `y[]` is:
112
+ * 24-bit precision 1
113
+ * 53-bit precision 2
114
+ * 64-bit precision 2
115
+ * 113-bit precision 3
116
+ *
117
+ * The actual value is the sum of them. Thus, for 113-bit precision, one may have to do something like:
118
+ *
119
+ * ```tex
120
+ * \mathrm{long\ double} \: t, w, r_{\text{head}}, r_{\text{tail}}; \\
121
+ * t &= (\mathrm{long\ double}) y[2] + (\mathrm{long\ double}) y[1]; \\
122
+ * w &= (\mathrm{long\ double}) y[0]; \\
123
+ * r_{\text{head}} &= t + w; \\
124
+ * r_{\text{tail}} &= w - (r_{\text{head}} - t);
125
+ * ```
126
+ *
127
+ * - `e0` The exponent of `x[0]`. Must be <= 16360 or you need to expand the `ipio2` table.
128
+ *
129
+ * - `nx` dimension of `x[]`
130
+ *
131
+ * - `prec` an integer indicating the precision:
132
+ * 0 24 bits (single)
133
+ * 1 53 bits (double)
134
+ * 2 64 bits (extended)
135
+ * 3 113 bits (quad)
136
+ *
137
+ * - External function:
138
+ *
139
+ * - double `scalbn()`, `floor()`;
140
+ *
141
+ * - Here is the description of some local variables:
142
+ *
143
+ * - `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.)
144
+ *
145
+ * - `jz` local integer variable indicating the number of terms of `ipio2[]` used.
146
+ *
147
+ * - `jx` `nx - 1`
148
+ *
149
+ * - `jv` index for pointing to the suitable `ipio2[]` for the computation. In general, we want
150
+ *
151
+ * ```tex
152
+ * \frac{{2^{e0} \cdot x[0] \cdot \mathrm{ipio2}[jv-1] \cdot 2^{-24jv}}}{{8}}
153
+ * ```
154
+ *
155
+ * to be an integer. Thus
156
+ *
157
+ * ```tex
158
+ * e0 - 3 - 24 \cdot jv \geq 0 \quad \text{or} \quad \frac{{e0 - 3}}{{24}} \geq jv
159
+ * ```
160
+ *
161
+ * Hence
162
+ *
163
+ * ```tex
164
+ * jv = \max(0, \frac{{e0 - 3}}{{24}})
165
+ * ```
166
+ *
167
+ * - `jp` `jp+1` is the number of terms in `PIo2[]` needed, `jp = jk`.
168
+ *
169
+ * - `q[]` double array with integral value, representing the 24-bits chunk of the product of `x` and `2/π`.
170
+ *
171
+ * - `q0` the corresponding exponent of `q[0]`. Note that the exponent for `q[i]` would be `q0-24*i`.
172
+ *
173
+ * - `PIo2[]` double precision array, obtained by cutting `π/2` into 24 bits chunks.
174
+ *
175
+ * - `f[]` `ipso2[]` in floating point
176
+ *
177
+ * - `iq[]` integer array by breaking up `q[]` in 24-bits chunk.
178
+ *
179
+ * - `fq[]` final product of `x*(2/π)` in `fq[0],..,fq[jk]`
180
+ *
181
+ * - `ih` integer. If >0 it indicates `q[]` is >= 0.5, hence it also indicates the _sign_ of the result.
182
+ *
183
+ * - Constants:
184
+ *
185
+ * - 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.
186
+ *
187
+ * @param x input value
188
+ * @param y output array to store the remainder
189
+ * @param e0 the exponent of `x[0]` (must be <= 127)
190
+ * @param nx dimension of `x[]`
191
+ * @return last 3 binary digits
192
+ */
193
+ int32_t rempio2fKernel( const double* x, double *y, int32_t e0, int32_t nx ) {
194
+ double F[ 20 ] = { 0.0 };
195
+ double Q[ 20 ] = { 0.0 };
196
+ int32_t IQ[ 20 ];
197
+ double FQ[ 20 ];
198
+ int32_t carry;
199
+ int32_t jz;
200
+ int32_t jx;
201
+ int32_t jv;
202
+ int32_t jp;
203
+ int32_t jk;
204
+ int32_t q0;
205
+ int32_t ih;
206
+ int32_t n;
207
+ int32_t i;
208
+ int32_t j;
209
+ int32_t k;
210
+ int32_t m;
211
+ double fw;
212
+ double z;
213
+
214
+ // Initialize `jk` for single-precision floating-point numbers:
215
+ jk = 3;
216
+ jp = jk;
217
+
218
+ // Determine `jx`, `jv`, `q0` (note that `q0 < 3`):
219
+ jx = nx - 1;
220
+ jv = ( e0 - 3 ) / 24;
221
+ if ( jv < 0 ) {
222
+ jv = 0;
223
+ }
224
+ q0 = e0 - ( 24 * ( jv + 1 ) );
225
+
226
+ // Set up `F[0]` to `F[jx+jk]` where `F[jx+jk] = IPIO2[jv+jk]`:
227
+ j = jv - jx;
228
+ m = jx + jk;
229
+ for ( i = 0; i <= m; i++ ) {
230
+ if ( j < 0 ) {
231
+ F[ i ] = ZERO;
232
+ } else {
233
+ F[ i ] = ( double )IPIO2[ j ];
234
+ }
235
+ j++;
236
+ }
237
+
238
+ // Compute `Q[0],Q[1],...,Q[jk]`:
239
+ for ( i = 0; i <= jk; i++ ) {
240
+ fw = 0.0;
241
+ for ( j = 0; j <= jx; j++ ) {
242
+ fw += x[ j ] * F[ jx + ( i - j ) ];
243
+ }
244
+ Q[ i ] = fw;
245
+ }
246
+
247
+ jz = jk;
248
+ recompute:
249
+ // Distill `q[]` into `IQ[]` in reverse order...
250
+ z = Q[ jz ];
251
+ j = jz;
252
+ for ( i = 0; j > 0; i++ ) {
253
+ fw = ( double )( ( int32_t )( TWON24 * z ) );
254
+ IQ[ i ] = (int32_t)( z - ( TWO24 * fw ) );
255
+ z = Q[ j - 1 ] + fw;
256
+ j--;
257
+ }
258
+ // Compute `n`...
259
+ z = stdlib_base_ldexp( z, q0 );
260
+ z -= 8.0 * stdlib_base_floor( z * 0.125 ); // Trim off integer >= 8
261
+ n = ( int32_t )z;
262
+ z -= ( double )n;
263
+ ih = 0;
264
+ if ( q0 > 0 ) {
265
+ // Need `IQ[jz-1]` to determine `n`...
266
+ i = ( IQ[ jz - 1 ] >> ( 24 - q0 ) );
267
+ n += i;
268
+ IQ[ jz - 1 ] -= ( i << ( 24 - q0 ) );
269
+ ih = ( IQ[ jz - 1 ] >> ( 23 - q0 ) );
270
+ } else if ( q0 == 0 ) {
271
+ ih = ( IQ[ jz - 1 ] >> 23 );
272
+ } else if ( z >= 0.5 ) {
273
+ ih = 2;
274
+ }
275
+
276
+ // Case: q > 0.5
277
+ if ( ih > 0 ) {
278
+ n += 1;
279
+ carry = 0;
280
+ // Compute `1-q`:
281
+ for ( i = 0; i < jz; i++ ) {
282
+ j = IQ[ i ];
283
+ if ( carry == 0 ) {
284
+ if ( j != 0 ) {
285
+ carry = 1;
286
+ IQ[ i ] = 0x1000000 - j;
287
+ }
288
+ } else {
289
+ IQ[ i ] = 0xffffff - j;
290
+ }
291
+ }
292
+ if ( q0 > 0 ) {
293
+ // Rare case: chance is 1 in 12...
294
+ switch ( q0 ) {
295
+ case 1:
296
+ IQ[ jz - 1 ] &= 0x7fffff;
297
+ break;
298
+ case 2:
299
+ IQ[ jz - 1 ] &= 0x3fffff;
300
+ break;
301
+ }
302
+ }
303
+ if ( ih == 2 ) {
304
+ z = 1.0 - z;
305
+ if ( carry != 0 ) {
306
+ z -= stdlib_base_ldexp( 1.0, q0 );
307
+ }
308
+ }
309
+ }
310
+
311
+ // Check if re-computation is needed...
312
+ if ( z == ZERO ) {
313
+ j = 0;
314
+ for ( i = jz - 1; i >= jk; i-- ) {
315
+ j |= IQ[ i ];
316
+ }
317
+ if ( j == 0 ) {
318
+ // Need re-computation...
319
+ for ( k = 1; IQ[ jk - k ] == 0; k++ ) {
320
+ // `k` is the number of terms needed...
321
+ }
322
+
323
+ for ( i = jz + 1; i <= jz + k; i++ ) {
324
+ // Add `q[jz+1]` to `q[jz+k]`...
325
+ F[ jx + i ] = ( double )IPIO2[ jv + i ];
326
+ fw = 0.0;
327
+ for ( j = 0; j <= jx; j++ ) {
328
+ fw += x[ j ] * F[ jx + ( i - j ) ];
329
+ }
330
+ Q[ i ] = fw;
331
+ }
332
+ jz += k;
333
+ goto recompute;
334
+ }
335
+ // Chop off zero terms...
336
+ jz -= 1;
337
+ q0 -= 24;
338
+ while ( IQ[ jz ] == 0 ) {
339
+ jz--;
340
+ q0 -= 24;
341
+ }
342
+ } else {
343
+ // Break `z` into 24-bit if necessary...
344
+ z = stdlib_base_ldexp( z, -q0 );
345
+ if ( z >= TWO24 ) {
346
+ fw = ( double )( ( int32_t )( TWON24 * z ) );
347
+ IQ[ jz ] = ( int32_t )( z - ( TWO24 * fw ) );
348
+ jz += 1;
349
+ q0 += 24;
350
+ IQ[ jz ] = ( int32_t )fw;
351
+ } else {
352
+ IQ[ jz ] = ( int32_t )z;
353
+ }
354
+ }
355
+
356
+ // Convert integer "bit" chunk to floating-point value...
357
+ fw = stdlib_base_ldexp( 1.0, q0 );
358
+ for ( i = jz; i >= 0; i-- ) {
359
+ Q[ i ] = fw * ( double )IQ[ i ];
360
+ fw *= TWON24;
361
+ }
362
+
363
+ // Compute `PIO2[0,...,jp]*q[jz,...,0]`...
364
+ for ( i = jz; i >= 0; i-- ) {
365
+ fw = 0.0;
366
+ for ( k = 0; k <= jp && k <= jz - i; k++ ) {
367
+ fw += PIO2[ k ] * Q[ i + k ];
368
+ }
369
+ FQ[ jz - i ] = fw;
370
+ }
371
+ // Compress `FQ[]` into `y[]`...
372
+ fw = 0.0;
373
+ for ( i = jz; i >= 0; i-- ) {
374
+ fw += FQ[ i ];
375
+ }
376
+ if ( ih == 0 ) {
377
+ y[ 0 ] = fw;
378
+ } else {
379
+ y[ 0 ] = -fw;
380
+ }
381
+ return n & 7;
382
+ }
383
+
384
+ /**
385
+ * Computes `x - nπ/2 = r` (single-precision).
386
+ *
387
+ * @param x input value
388
+ * @param rem remainder element
389
+ * @return factor of `π/2`
390
+ *
391
+ * @example
392
+ * #include <stdint.h>
393
+ *
394
+ * double rem;
395
+ * int32_t n = stdlib_base_rempio2f( 128.0f, &rem );
396
+ */
397
+ int32_t stdlib_base_rempio2f( const float x, double *rem ) {
398
+ double TX[ 1 ];
399
+ double TY[ 1 ];
400
+ uint32_t uhx;
401
+ int32_t ix;
402
+ int32_t hx;
403
+ int32_t e0;
404
+ int32_t n;
405
+ double w;
406
+ double r;
407
+ float z;
408
+
409
+ // high word of x
410
+ stdlib_base_float32_to_word( x, &uhx );
411
+ hx = ( int32_t )uhx;
412
+ ix = hx & STDLIB_CONSTANT_FLOAT32_ABS_MASK;
413
+
414
+ // Case: |x| ~< 2^28*π/2 (medium size)
415
+ if ( ix < MEDIUM ) {
416
+ n = stdlib_base_roundf( x * INVPIO2 );
417
+ r = x - ( n * PIO2_1 );
418
+ w = n * PIO2_1T;
419
+ *rem = r - w;
420
+ return n;
421
+ }
422
+ // Case: x is NaN or infinity
423
+ if( ix >= STDLIB_CONSTANT_FLOAT32_EXPONENT_MASK ) {
424
+ *rem = 0.0 / 0.0; // NaN
425
+ return 0;
426
+ }
427
+ // Set z = scalbn(|x|, ilogb(|x|)-23)...
428
+ e0 = ( ix >> 23 ) - 150; // `e0 = ilogb(|x|) - 23` => unbiased exponent minus 23
429
+ stdlib_base_float32_from_word( ix - ( int32_t ) ( e0 << 23 ), &z );
430
+ TX[ 0 ] = z;
431
+ n = rempio2fKernel( TX, TY, e0, 1 );
432
+ if ( hx < 0 ) {
433
+ *rem = -TY[ 0 ];
434
+ return -n;
435
+ }
436
+ *rem = TY[ 0 ];
437
+ return n;
438
+ }