sleef-sys 0.1.2

Rust FFI bindings to the SLEEF Vectorized Math Library
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
//          Copyright Naoki Shibata 2010 - 2017.
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

// Always use -ffp-contract=off option to compile SLEEF.

#include <stdio.h>

#include <assert.h>
#include <stdint.h>
#include <limits.h>

#include "misc.h"

#ifdef DORENAME
#include "rename.h"
#endif

#if (defined(_MSC_VER))
#pragma fp_contract (off)
#endif

static INLINE CONST long double mlal(long double x, long double y, long double z) { return x * y + z; }
static INLINE CONST long double xrintl(long double x) { return x < 0 ? (int)(x - 0.5) : (int)(x + 0.5); }
static INLINE CONST int64_t xceill(long double x) { return (int64_t)x + (x < 0 ? 0 : 1); }
static INLINE CONST long double xtruncl(long double x) { return (long double)(int)x; }

static INLINE CONST int xisnanl(long double x) { return x != x; }
static INLINE CONST int xisinfl(long double x) { return x == SLEEF_INFINITYl || x == -SLEEF_INFINITYl; }
static INLINE CONST int xisminfl(long double x) { return x == -SLEEF_INFINITYl; }
static INLINE CONST int xispinfl(long double x) { return x == SLEEF_INFINITYl; }

static INLINE CONST long double xfabsl(long double x) { return x >= 0 ? x : -x; }

//

#ifndef NDEBUG
static int checkfp(long double x) {
  if (xisinfl(x) || xisnanl(x)) return 1;
  return 0;
}
#endif

static INLINE CONST long double upperl(long double d) {
  union {
    long double ld;
    uint32_t u[4];
  } cnv;

  cnv.ld = d;
  cnv.u[0] = 0;
  return cnv.ld;
}

static INLINE CONST Sleef_longdouble2 dl(long double h, long double l) {
  Sleef_longdouble2 ret;
  ret.x = h; ret.y = l;
  return ret;
}

static INLINE CONST Sleef_longdouble2 dlnormalize_l2_l2(Sleef_longdouble2 t) {
  Sleef_longdouble2 s;

  s.x = t.x + t.y;
  s.y = t.x - s.x + t.y;

  return s;
}

static INLINE CONST Sleef_longdouble2 dlscale_l2_l2_l(Sleef_longdouble2 d, long double s) {
  Sleef_longdouble2 r;

  r.x = d.x * s;
  r.y = d.y * s;

  return r;
}

static INLINE CONST Sleef_longdouble2 dlneg_l2_l2(Sleef_longdouble2 d) {
  Sleef_longdouble2 r;

  r.x = -d.x;
  r.y = -d.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd_l2_l_l(long double x, long double y) {
  // |x| >= |y|

  Sleef_longdouble2 r;

#ifndef NDEBUG
  if (!(checkfp(x) || checkfp(y) || xfabsl(x) >= xfabsl(y))) {
    fprintf(stderr, "[dladd_l2_l_l : %Lg, %Lg]\n", x, y);
    fflush(stderr);
  }
#endif

  r.x = x + y;
  r.y = x - r.x + y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd2_l2_l_l(long double x, long double y) {
  Sleef_longdouble2 r;

  r.x = x + y;
  long double v = r.x - x;
  r.y = (x - (r.x - v)) + (y - v);

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd_l2_l2_l(Sleef_longdouble2 x, long double y) {
  // |x| >= |y|

  Sleef_longdouble2 r;

#ifndef NDEBUG
  if (!(checkfp(x.x) || checkfp(y) || xfabsl(x.x) >= xfabsl(y))) {
    fprintf(stderr, "[dladd_l2_l2_l : %Lg %Lg]\n", x.x, y);
    fflush(stderr);
  }
#endif

  r.x = x.x + y;
  r.y = x.x - r.x + y + x.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd2_l2_l2_l(Sleef_longdouble2 x, long double y) {
  // |x| >= |y|

  Sleef_longdouble2 r;

  r.x  = x.x + y;
  long double v = r.x - x.x;
  r.y = (x.x - (r.x - v)) + (y - v);
  r.y += x.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd_l2_l_l2(long double x, Sleef_longdouble2 y) {
  // |x| >= |y|

  Sleef_longdouble2 r;

#ifndef NDEBUG
  if (!(checkfp(x) || checkfp(y.x) || xfabsl(x) >= xfabsl(y.x))) {
    fprintf(stderr, "[dladd_l2_l_l2 : %Lg %Lg]\n", x, y.x);
    fflush(stderr);
  }
#endif

  r.x = x + y.x;
  r.y = x - r.x + y.x + y.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd2_l2_l_l2(long double x, Sleef_longdouble2 y) {
  Sleef_longdouble2 r;

  r.x  = x + y.x;
  long double v = r.x - x;
  r.y = (x - (r.x - v)) + (y.x - v) + y.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd_l2_l2_l2(Sleef_longdouble2 x, Sleef_longdouble2 y) {
  // |x| >= |y|

  Sleef_longdouble2 r;

#ifndef NDEBUG
  if (!(checkfp(x.x) || checkfp(y.x) || xfabsl(x.x) >= xfabsl(y.x))) {
    fprintf(stderr, "[dladd_l2_l2_l2 : %Lg %Lg]\n", x.x, y.x);
    fflush(stderr);
  }
#endif

  r.x = x.x + y.x;
  r.y = x.x - r.x + y.x + x.y + y.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dladd2_l2_l2_l2(Sleef_longdouble2 x, Sleef_longdouble2 y) {
  Sleef_longdouble2 r;

  r.x  = x.x + y.x;
  long double v = r.x - x.x;
  r.y = (x.x - (r.x - v)) + (y.x - v);
  r.y += x.y + y.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dlsub_l2_l2_l2(Sleef_longdouble2 x, Sleef_longdouble2 y) {
  // |x| >= |y|

  Sleef_longdouble2 r;

#ifndef NDEBUG
  if (!(checkfp(x.x) || checkfp(y.x) || xfabsl(x.x) >= xfabsl(y.x))) {
    fprintf(stderr, "[dlsub_l2_l2_l2 : %Lg %Lg]\n", x.x, y.x);
    fflush(stderr);
  }
#endif

  r.x = x.x - y.x;
  r.y = x.x - r.x - y.x + x.y - y.y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dldiv_l2_l2_l2(Sleef_longdouble2 n, Sleef_longdouble2 d) {
  long double t = 1.0 / d.x;
  long double dh  = upperl(d.x), dl  = d.x - dh;
  long double th  = upperl(t  ), tl  = t   - th;
  long double nhh = upperl(n.x), nhl = n.x - nhh;

  Sleef_longdouble2 q;

  q.x = n.x * t;

  long double u = -q.x + nhh * th + nhh * tl + nhl * th + nhl * tl +
    q.x * (1 - dh * th - dh * tl - dl * th - dl * tl);

  q.y = t * (n.y - q.x * d.y) + u;

  return q;
}

static INLINE CONST Sleef_longdouble2 dlmul_l2_l_l(long double x, long double y) {
  long double xh = upperl(x), xl = x - xh;
  long double yh = upperl(y), yl = y - yh;
  Sleef_longdouble2 r;

  r.x = x * y;
  r.y = xh * yh - r.x + xl * yh + xh * yl + xl * yl;

  return r;
}

static INLINE CONST Sleef_longdouble2 dlmul_l2_l2_l(Sleef_longdouble2 x, long double y) {
  long double xh = upperl(x.x), xl = x.x - xh;
  long double yh = upperl(y  ), yl = y   - yh;
  Sleef_longdouble2 r;

  r.x = x.x * y;
  r.y = xh * yh - r.x + xl * yh + xh * yl + xl * yl + x.y * y;

  return r;
}

static INLINE CONST Sleef_longdouble2 dlmul_l2_l2_l2(Sleef_longdouble2 x, Sleef_longdouble2 y) {
  long double xh = upperl(x.x), xl = x.x - xh;
  long double yh = upperl(y.x), yl = y.x - yh;
  Sleef_longdouble2 r;

  r.x = x.x * y.x;
  r.y = xh * yh - r.x + xl * yh + xh * yl + xl * yl + x.x * y.y + x.y * y.x;

  return r;
}

static INLINE CONST Sleef_longdouble2 dlsqu_l2_l2(Sleef_longdouble2 x) {
  long double xh = upperl(x.x), xl = x.x - xh;
  Sleef_longdouble2 r;

  r.x = x.x * x.x;
  r.y = xh * xh - r.x + (xh + xh) * xl + xl * xl + x.x * (x.y + x.y);

  return r;
}

static INLINE CONST Sleef_longdouble2 dlrec_l2_l(long double d) {
  long double t = 1.0 / d;
  long double dh = upperl(d), dl = d - dh;
  long double th = upperl(t), tl = t - th;
  Sleef_longdouble2 q;

  q.x = t;
  q.y = t * (1 - dh * th - dh * tl - dl * th - dl * tl);

  return q;
}

static INLINE CONST Sleef_longdouble2 dlrec_l2_l2(Sleef_longdouble2 d) {
  long double t = 1.0 / d.x;
  long double dh = upperl(d.x), dl = d.x - dh;
  long double th = upperl(t  ), tl = t   - th;
  Sleef_longdouble2 q;

  q.x = t;
  q.y = t * (1 - dh * th - dh * tl - dl * th - dl * tl - d.y * t);

  return q;
}

/*
static INLINE CONST Sleef_longdouble2 dlsqrt_l2_l2(Sleef_longdouble2 d) {
  long double t = sqrt(d.x + d.y);
  return dlscale_l2_l2_l(dlmul_l2_l2_l2(dladd2_l2_l2_l2(d, dlmul_l2_l_l(t, t)), dlrec_l2_l(t)), 0.5);
}
*/

//

EXPORT CONST Sleef_longdouble2 xsincospil_u05(long double d) {
  long double u, s, t;
  Sleef_longdouble2 r, x, s2;

  u = d * 4;
  int64_t q = xceill(u) & ~(int64_t)1;

  s = u - (long double)q;
  t = s;
  s = s * s;
  s2 = dlmul_l2_l_l(t, t);
  
  //

  u = 4.59265607313529833157632e-17L;
  u = mlal(u, s, -2.04096140520547829627419e-14L);
  u = mlal(u, s, 6.94845264320316515640316e-12L);
  u = mlal(u, s, -1.75724767308629210422023e-09L);
  u = mlal(u, s, 3.13361689037693212744991e-07L);
  u = mlal(u, s, -3.65762041821772284521155e-05L);
  u = mlal(u, s, 0.00249039457019272015784594L);
  x = dladd2_l2_l_l2(u * s, dl(-0.0807455121882807817044873L, -2.40179063154839769223037e-21L));
  x = dladd2_l2_l2_l2(dlmul_l2_l2_l2(s2, x), dl(0.785398163397448309628202L, -1.25420305812534448752181e-20L));
  
  x = dlmul_l2_l2_l(x, t);
  r.x = x.x + x.y;

  //

  u = -2.00423964577657539380734e-18L;
  u = mlal(u, s, 1.00185574457758689324113e-15L);
  u = mlal(u, s, -3.89807283423502620989528e-13L);
  u = mlal(u, s, 1.15011591257563133685341e-10L);
  u = mlal(u, s, -2.461136950493305818105e-08L);
  u = mlal(u, s, 3.59086044859150791782134e-06L);
  u = mlal(u, s, -0.00032599188692739001335938L);
  x = dladd2_l2_l_l2(u * s, dl(0.0158543442438155008529635L, -6.97556143018517384674258e-22L));
  x = dladd2_l2_l2_l2(dlmul_l2_l2_l2(s2, x), dl(-0.308425137534042456829379L, -9.19882299434302978226668e-21L));
  
  x = dladd2_l2_l2_l(dlmul_l2_l2_l2(x, s2), 1);
  r.y = x.x + x.y;

  //
  
  if ((q & 2) != 0) { s = r.y; r.y = r.x; r.x = s; }
  if ((q & 4) != 0) { r.x = -r.x; }
  if (((q+2) & 4) != 0) { r.y = -r.y; }

  if (xisinfl(d)) { r.x = r.y = SLEEF_NAN; }
  if (!xisinfl(d) && xfabsl(d) > TRIGRANGEMAX3) { r.x = r.y = 0; }

  return r;
}

EXPORT CONST Sleef_longdouble2 xsincospil_u35(long double d) {
  long double u, s, t;
  Sleef_longdouble2 r;

  u = d * 4;
  int64_t q = xceill(u) & ~(int64_t)1;

  s = u - (long double)q;
  t = s;
  s = s * s;
  
  //

  u = -0.2023275819380976135024e-13L;
  u = mlal(u, s, +0.6948176964255957574946e-11L);
  u = mlal(u, s, -0.1757247450021535880723e-8L);
  u = mlal(u, s, +0.3133616889379195970541e-6L);
  u = mlal(u, s, -0.3657620418215300856408e-4L);
  u = mlal(u, s, +0.2490394570192717262476e-2L);
  u = mlal(u, s, -0.8074551218828078160284e-1L);
  u = mlal(u, s, +0.7853981633974483096282e+0L);
  
  r.x = u * t;

  //

  u = +0.9933418221428971922705e-15L;
  u = mlal(u, s, -0.3897923064055824005357e-12L);
  u = mlal(u, s, +0.1150115771521792692066e-9L);
  u = mlal(u, s, -0.2461136949725905367314e-7L);
  u = mlal(u, s, +0.3590860448589084195081e-5L);
  u = mlal(u, s, -0.3259918869273895914840e-3L);
  u = mlal(u, s, +0.1585434424381550079706e-1L);
  u = mlal(u, s, -0.3084251375340424568294e+0L);
  u = mlal(u, s, 1.0L);

  r.y = u;

  //
  
  if ((q & 2) != 0) { s = r.y; r.y = r.x; r.x = s; }
  if ((q & 4) != 0) { r.x = -r.x; }
  if (((q+2) & 4) != 0) { r.y = -r.y; }

  if (xisinfl(d)) { r.x = r.y = SLEEF_NAN; }
  if (!xisinfl(d) && xfabsl(d) > TRIGRANGEMAX3) { r.x = r.y = 0; }

  return r;
}