core-math-sys 1.0.3

Generate bindings to CORE-MATH
Documentation
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* Check sinh on random inputs.

Copyright (c) 2022-2025 Paul Zimmermann, Inria.

This file is part of the CORE-MATH project
(https://core-math.gitlabpages.inria.fr/).

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fenv.h>
#include <math.h>
#include <sys/types.h>
#include <unistd.h>
#include <inttypes.h>
#include <assert.h>
#include <mpfr.h>
#include "function_under_test.h"
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#include <omp.h>
#endif

int ref_init (void);
int ref_fesetround (int);

double cr_sinh (double);
double ref_sinh (double);

int rnd1[] = { FE_TONEAREST, FE_TOWARDZERO, FE_UPWARD, FE_DOWNWARD };

int rnd = 0;
int verbose = 0;
unsigned long tested = 0;

#define MAX_THREADS 192

static unsigned int Seed[MAX_THREADS];

static inline double tfun(double x){
  return cr_function_under_test(x);
}

static inline uint64_t
asuint64 (double f)
{
  union
  {
    double f;
    uint64_t i;
  } u = {f};
  return u.i;
}

typedef union {double f; uint64_t u;} b64u64_u;

static double
get_random (int tid)
{
  b64u64_u v;
  v.u = rand_r (Seed + tid);
  v.u |= (uint64_t) rand_r (Seed + tid) << 31;
  v.u |= (uint64_t) rand_r (Seed + tid) << 62;
  return v.f;
}

static void
check (double x)
{
  int bug;
#pragma omp atomic update
  tested ++;
  double y1 = ref_sinh (x);
  fesetround (rnd1[rnd]);
  double y2 = cr_sinh (x);
  if (isnan (y1))
    bug = !isnan (y2);
  else if (isnan (y2))
    bug = !isnan (y1);
  else
    bug = asuint64 (y1) != asuint64 (y2);
  if (bug)
  {
    printf ("FAIL x=%la ref=%la z=%la\n", x, y1, y2);
    fflush (stdout);
#ifndef DO_NOT_ABORT
    exit (1);
#endif
  }
}

static inline double
asfloat64 (uint64_t i)
{
  b64u64_u u = {.u = i};
  return u.f;
}

/* define our own is_nan function to avoid depending from math.h */
static inline int
is_nan (double x)
{
  uint64_t u = asuint64 (x);
  uint64_t e = u >> 52;
  return (e == 0x7ff || e == 0xfff) && (u << 12) != 0;
}

// When x is a NaN, returns 1 if x is an sNaN and 0 if it is a qNaN
static inline int is_signaling(double x) {
  b64u64_u _x = {.f = x};

  return !(_x.u & (1ull << 51));
}

static inline int
is_inf (double x)
{
  uint64_t u = asuint64 (x);
  uint64_t e = u >> 52;
  return (e == 0x7ff || e == 0xfff) && (u << 12) == 0;
}

static void
check_invalid (void)
{
  double plusInf = asfloat64(0x7ff0000000000000ull);
  double minInf = asfloat64(0xfff0000000000000ull);
  double qnan = asfloat64(0x7ff8000000000000ull);

  // Check +Inf
  feclearexcept (FE_INVALID);
  double y = cr_sinh(plusInf);

  /* In accordance with IEEE-754 sinh(+Inf) is +Inf and sinh(-Inf) is -Inf  */
  if (is_nan (y))
  {
    fprintf (stderr, "Error, foo(+Inf) should be +Inf, got %la=%"PRIx64"\n",
             y, asuint64 (y));
#ifndef DO_NOT_ABORT
    exit (1);
#endif
  }
  // check the invalid exception was not set
  int flag = fetestexcept (FE_INVALID);
  if (flag)
  {
    printf ("Spurious invalid exception for x=+Inf\n");
#ifndef DO_NOT_ABORT
    exit (1);
#endif
  }
  // Check that y is Inf
  if (!is_inf(y) || y < 0)
  {
    fprintf (stderr, "Error, foo(+Inf) should be +Inf, got %la=%"PRIx64"\n",
      y, asuint64 (y));
#ifndef DO_NOT_ABORT
    exit (1);
#endif
  }

  // Check -Inf
  feclearexcept (FE_INVALID);
  y = cr_sinh(minInf);
  if (is_nan (y))
  {
    fprintf (stderr, "Error, foo(-Inf) should be -Inf, got %la=%"PRIx64"\n",
             y, asuint64 (y));
#ifndef DO_NOT_ABORT
      exit (1);
#endif
  }
  // check the invalid exception was set
  flag = fetestexcept (FE_INVALID);
  if (flag)
  {
    printf ("Spurious invalid exception for x=-Inf\n");
#ifndef DO_NOT_ABORT
    exit (1);
#endif
  }
  // Check that y is Inf
  if (!is_inf(y) || y > 0)
  {
    fprintf (stderr, "Error, foo(-Inf) should be -Inf, got %la=%"PRIx64"\n",
      y, asuint64 (y));
#ifndef DO_NOT_ABORT
    exit (1);
#endif
  }

  // Check qnan
  feclearexcept (FE_INVALID);
  y = cr_sinh(qnan);
  // Check that sinh(qNaN) = qNaN
  if (!is_nan (y) || is_signaling(y))
  {
  fprintf (stderr, "Error, foo(qNaN) should be qNaN, got %la=%"PRIx64"\n",
               y, asuint64 (y));
#ifndef DO_NOT_ABORT
        exit (1);
#endif
  }
  // check the invalid exception was set
  flag = fetestexcept (FE_INVALID);
  if (flag)
  {
    printf ("Spurious invalid exception for x=-Inf\n");
#ifndef DO_NOT_ABORT
    exit (1);
#endif
  }

  // Check 
  double T[] = {asfloat64(0x7ff0000000000000ull), // +Inf
    0x1.fffffffffffffp+1023, // DBL_MAX
    0x1.633ce8fb9f87cp+9,
    0x1.633ce8fb9f87dp+9,
    0x1.633ce8fb9f87ep+9,
    0x1.633ce8fb9f87fp+9};

  for (unsigned long i = 0; i < sizeof(T)/sizeof(T[0]); i++) {
    feclearexcept (FE_INVALID);
    double x = T[i];
     y = cr_sinh (x);
    if (x >= 0x1.633ce8fb9f87ep+9 && !is_inf (y))
    {
      fprintf (stderr, "Error, foo(%la) should be +Inf, got %la=%"PRIx64"\n",
               x, y, asuint64 (y));
#ifndef DO_NOT_ABORT
      exit (1);
#endif
    }
    // check the invalid exception is not set
    flag = fetestexcept (FE_INVALID);
    if (flag)
    {
      printf ("Spurious invalid exception for x=%la\n", x);
#ifndef DO_NOT_ABORT
      exit (1);
#endif
    }

    // Check -x
    feclearexcept (FE_INVALID);
    y = cr_sinh (-x);
    if (x >= 0x1.633ce8fb9f87ep+9 && !is_inf (y))
    {
      fprintf (stderr, "Error, foo(%la) should be -Inf, got %la=%"PRIx64"\n",
               x, y, asuint64 (y));
#ifndef DO_NOT_ABORT
      exit (1);
#endif
    }
    // check the invalid exception is not set
    flag = fetestexcept (FE_INVALID);
    if (flag)
    {
      printf ("Spurious invalid exception for x=%la\n", x);
#ifndef DO_NOT_ABORT
      exit (1);
#endif
    }  
  }
}

// put in h+l a double-double approximation of sinh(x)
static void
dd_sinh (double *h, double *l, double x)
{
  mpfr_t t;
  mpfr_init2 (t, 107);
  mpfr_set_d (t, x, MPFR_RNDN);
  mpfr_sinh (t, t, MPFR_RNDN);
  *h = mpfr_get_d (t, MPFR_RNDN);
  mpfr_sub_d (t, t, *h, MPFR_RNDN);
  *l = mpfr_get_d (t, MPFR_RNDN);
  mpfr_clear (t);
}

// test n values starting from x
static void scan_consecutive_aux(int64_t n, double x){
  ref_init();
  ref_fesetround(rnd);
  fesetround(rnd1[rnd]);
  while (n) {
    double h, l, d, dd;
    dd_sinh (&h, &l, x);
    dd = fabs (h); // absolute value of 2nd derivative
    // derivative cosh(x) = sqrt(1+sinh(x)^2)
    d = dd > 0x1p53 ? h : sqrt (1.0 + h * h);
    int e;
    frexp (x, &e);
    /* 2^(e-1) <= |x| < 2^e thus ulp(x) = 2^(e-53) */
    d = ldexp (d, e - 53); // multiply d by ulp(x)
    dd = ldexp (dd, 2 * (e - 53)); // multiply dd by ulp(x)^2
    /* we want j^2*dd < 2^-11 ulp(h) so that the 2nd-order term
       produces an error bounded by 2^-11 ulp(h), to that MPFR
       will be called with probability about 2^-11.
       Thus approximately j^2*dd < 2^-64 h,
       or j < 2^-32 sqrt(h/dd) */
    int64_t jmax = 0x1p-32 * sqrt (fabs (h) / dd);
    if (jmax > n) jmax = n; // cap to n
    if (jmax == 0) jmax = 1; // ensure progress
    for(int64_t j=0;j<jmax;j++){
      b64u64_u v = {.f = x};
      v.u += j;
      double t = tfun (v.f);
      // sinh(x+j*u) is approximated by h + l + j*d
      double w = h + __builtin_fma (j, d, l);
      if (t != w) // expensive test
        check(v.f);
    }
    n -= jmax;
    x += jmax * ldexp (1.0, e - 53);
  }
}

static void scan_consecutive (int64_t n, double x){
  int nthreads = 1;
  if (n < 0) {
    n = -n;
    x = asfloat64 (asuint64 (x) - n);
  }
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel
  nthreads = omp_get_num_threads ();
#endif
  int64_t h = (n - 1) / nthreads + 1; // ceil(n/nthreads)
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for schedule(static,1)
#endif
  for (int i = 0; i < nthreads; i++) {
    int64_t ni = i * h;
    double xi = asfloat64 (asuint64 (x) + ni);
    int64_t hi = (ni + h > n) ? n - ni : h;
    scan_consecutive_aux (hi, xi);
  }
  printf ("checked %lu values, expensive checks %lu\n",
          (unsigned long) n, tested);
}

int
main (int argc, char *argv[])
{
  int conseq = 0;      // scan consecutive values
  double a = 0;        // starting value for scan_consecutive
  unsigned long C = 0; // length for scan_consecutive
  while (argc >= 2)
    {
      if (strcmp (argv[1], "--rndn") == 0)
        {
          rnd = 0;
          argc --;
          argv ++;
        }
      else if (strcmp (argv[1], "--rndz") == 0)
        {
          rnd = 1;
          argc --;
          argv ++;
        }
      else if (strcmp (argv[1], "--rndu") == 0)
        {
          rnd = 2;
          argc --;
          argv ++;
        }
      else if (strcmp (argv[1], "--rndd") == 0)
        {
          rnd = 3;
          argc --;
          argv ++;
        }
      else if (strcmp (argv[1], "-C") == 0)
        {
          conseq = 1;
          C = strtoul (argv[2], NULL, 0);
          argc -= 2;
          argv += 2;
        }
      else if (strcmp (argv[1], "-a") == 0)
        {
          conseq = 1;
          a = strtod (argv[2], NULL);
          argc -= 2;
          argv += 2;
        }
      else if (strcmp (argv[1], "--verbose") == 0)
        {
          verbose = 1;
          argc --;
          argv ++;
        }
      else
        {
          fprintf (stderr, "Error, unknown option %s\n", argv[1]);
          exit (1);
        }
    }
  ref_init ();
  ref_fesetround (rnd);

  if (conseq) {
    scan_consecutive (C, a);
    return 0;
  }

  check_invalid ();

#ifndef CORE_MATH_TESTS
#define CORE_MATH_TESTS 1000000000UL /* total number of tests */
#endif

  unsigned int seed = getpid ();
  for (int i = 0; i < MAX_THREADS; i++)
    Seed[i] = seed + i;

#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
#pragma omp parallel for
#endif
  for (uint64_t n = 0; n < CORE_MATH_TESTS; n++)
  {
    ref_init ();
    ref_fesetround (rnd);
    int tid;
#if (defined(_OPENMP) && !defined(CORE_MATH_NO_OPENMP))
    tid = omp_get_thread_num ();
#else
    tid = 0;
#endif
    double x;
    do x = get_random (tid); while (fabs (x) >= 0x1.633ce8fb9f87ep+9);
    check (x);
  }

  return 0;
}