1 /* $OpenBSD: s_sinl.c,v 1.1 2008/12/09 20:00:35 martynas Exp $ */
3 * Copyright (c) 2007 Steven G. Kargl
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
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.
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.
29 * Compute sin(x) for x where x is reduced to y = x - k * pi / 2.
32 #include <sys/types.h>
33 #include <machine/ieee.h>
37 #include "math_private.h"
39 #if LDBL_MANT_DIG == 64
42 #elif LDBL_MANT_DIG == 113
46 #error "Unsupported long double format"
49 static const long double two24
= 1.67772160000000000000e+07L;
59 double xd
[NX
], yd
[PREC
];
66 /* If x = +-0 or x is a subnormal number, then sin(x) = x */
67 if (z
.bits
.ext_exp
== 0)
70 /* If x = NaN or Inf, then sin(x) = NaN. */
71 if (z
.bits
.ext_exp
== 32767)
72 return ((x
- x
) / (x
- x
));
74 /* Optimize the case where x is already within range. */
76 hi
= __kernel_sinl(z
.e
, 0, 0);
77 return (s
? -hi
: hi
);
80 /* Split z.e into a 24-bit representation. */
81 e0
= ilogbl(z
.e
) - 23;
82 z
.e
= scalbnl(z
.e
, -e0
);
83 for (i
= 0; i
< NX
; i
++) {
84 xd
[i
] = (double)((int32_t)z
.e
);
85 z
.e
= (z
.e
- xd
[i
]) * two24
;
88 /* yd contains the pieces of xd rem pi/2 such that |yd| < pi/4. */
89 e0
= __kernel_rem_pio2(xd
, yd
, e0
, NX
, PREC
);
92 hi
= (long double)yd
[0] + yd
[1];
93 lo
= yd
[1] - (hi
- yd
[0]);
96 t
= (long double)yd
[2] + yd
[1];
98 lo
= yd
[0] - (hi
- t
);
103 hi
= __kernel_sinl(hi
, lo
, 1);
106 hi
= __kernel_cosl(hi
, lo
);
109 hi
= - __kernel_sinl(hi
, lo
, 1);
112 hi
= - __kernel_cosl(hi
, lo
);
116 return (s
? -hi
: hi
);