1 |
|
|
/* $OpenBSD: s_sinl.c,v 1.2 2016/09/12 19:47:02 guenther Exp $ */ |
2 |
|
|
/*- |
3 |
|
|
* Copyright (c) 2007 Steven G. Kargl |
4 |
|
|
* All rights reserved. |
5 |
|
|
* |
6 |
|
|
* Redistribution and use in source and binary forms, with or without |
7 |
|
|
* modification, are permitted provided that the following conditions |
8 |
|
|
* are met: |
9 |
|
|
* 1. Redistributions of source code must retain the above copyright |
10 |
|
|
* notice unmodified, this list of conditions, and the following |
11 |
|
|
* disclaimer. |
12 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright |
13 |
|
|
* notice, this list of conditions and the following disclaimer in the |
14 |
|
|
* documentation and/or other materials provided with the distribution. |
15 |
|
|
* |
16 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 |
|
|
*/ |
27 |
|
|
|
28 |
|
|
/* |
29 |
|
|
* Compute sin(x) for x where x is reduced to y = x - k * pi / 2. |
30 |
|
|
*/ |
31 |
|
|
|
32 |
|
|
#include <sys/types.h> |
33 |
|
|
#include <machine/ieee.h> |
34 |
|
|
#include <float.h> |
35 |
|
|
#include <math.h> |
36 |
|
|
|
37 |
|
|
#include "math_private.h" |
38 |
|
|
|
39 |
|
|
#if LDBL_MANT_DIG == 64 |
40 |
|
|
#define NX 3 |
41 |
|
|
#define PREC 2 |
42 |
|
|
#elif LDBL_MANT_DIG == 113 |
43 |
|
|
#define NX 5 |
44 |
|
|
#define PREC 3 |
45 |
|
|
#else |
46 |
|
|
#error "Unsupported long double format" |
47 |
|
|
#endif |
48 |
|
|
|
49 |
|
|
static const long double two24 = 1.67772160000000000000e+07L; |
50 |
|
|
|
51 |
|
|
long double |
52 |
|
|
sinl(long double x) |
53 |
|
|
{ |
54 |
|
420 |
union { |
55 |
|
|
long double e; |
56 |
|
|
struct ieee_ext bits; |
57 |
|
|
} z; |
58 |
|
|
int i, e0, s; |
59 |
|
210 |
double xd[NX], yd[PREC]; |
60 |
|
|
long double hi, lo; |
61 |
|
|
|
62 |
|
210 |
z.e = x; |
63 |
|
210 |
s = z.bits.ext_sign; |
64 |
|
210 |
z.bits.ext_sign = 0; |
65 |
|
|
|
66 |
|
|
/* If x = +-0 or x is a subnormal number, then sin(x) = x */ |
67 |
✓✓ |
210 |
if (z.bits.ext_exp == 0) |
68 |
|
14 |
return (x); |
69 |
|
|
|
70 |
|
|
/* If x = NaN or Inf, then sin(x) = NaN. */ |
71 |
✗✓ |
196 |
if (z.bits.ext_exp == 32767) |
72 |
|
|
return ((x - x) / (x - x)); |
73 |
|
|
|
74 |
|
|
/* Optimize the case where x is already within range. */ |
75 |
✓✓ |
196 |
if (z.e < M_PI_4) { |
76 |
|
56 |
hi = __kernel_sinl(z.e, 0, 0); |
77 |
|
56 |
return (s ? -hi : hi); |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
/* Split z.e into a 24-bit representation. */ |
81 |
|
140 |
e0 = ilogbl(z.e) - 23; |
82 |
|
140 |
z.e = scalbnl(z.e, -e0); |
83 |
✓✓ |
1120 |
for (i = 0; i < NX; i++) { |
84 |
|
420 |
xd[i] = (double)((int32_t)z.e); |
85 |
|
420 |
z.e = (z.e - xd[i]) * two24; |
86 |
|
|
} |
87 |
|
|
|
88 |
|
|
/* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */ |
89 |
|
140 |
e0 = __kernel_rem_pio2(xd, yd, e0, NX, PREC); |
90 |
|
|
|
91 |
|
|
#if PREC == 2 |
92 |
|
140 |
hi = (long double)yd[0] + yd[1]; |
93 |
|
140 |
lo = yd[1] - (hi - yd[0]); |
94 |
|
|
#else /* PREC == 3 */ |
95 |
|
|
long double t; |
96 |
|
|
t = (long double)yd[2] + yd[1]; |
97 |
|
|
hi = t + yd[0]; |
98 |
|
|
lo = yd[0] - (hi - t); |
99 |
|
|
#endif |
100 |
|
|
|
101 |
✗✓✓✗ ✗ |
140 |
switch (e0 & 3) { |
102 |
|
|
case 0: |
103 |
|
|
hi = __kernel_sinl(hi, lo, 1); |
104 |
|
|
break; |
105 |
|
|
case 1: |
106 |
|
56 |
hi = __kernel_cosl(hi, lo); |
107 |
|
56 |
break; |
108 |
|
|
case 2: |
109 |
|
84 |
hi = - __kernel_sinl(hi, lo, 1); |
110 |
|
84 |
break; |
111 |
|
|
case 3: |
112 |
|
|
hi = - __kernel_cosl(hi, lo); |
113 |
|
|
break; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
140 |
return (s ? -hi : hi); |
117 |
|
210 |
} |
118 |
|
|
DEF_STD(sinl); |