fpu_hyperb.c revision 1.9 1 /* $NetBSD: fpu_hyperb.c,v 1.9 2013/04/19 13:31:11 isaki Exp $ */
2
3 /*
4 * Copyright (c) 1995 Ken Nakata
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following 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 * 3. Neither the name of the author nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)fpu_hyperb.c 10/24/95
32 */
33
34 /*
35 * Copyright (c) 2011 Tetsuya Isaki. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
47 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
48 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
49 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
50 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
51 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
52 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
53 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
54 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 */
58
59 #include <sys/cdefs.h>
60 __KERNEL_RCSID(0, "$NetBSD: fpu_hyperb.c,v 1.9 2013/04/19 13:31:11 isaki Exp $");
61
62 #include "fpu_emulate.h"
63
64 /*
65 * fpu_hyperb.c: defines the following functions
66 *
67 * fpu_atanh(), fpu_cosh(), fpu_sinh(), and fpu_tanh()
68 */
69
70 struct fpn *
71 fpu_atanh(struct fpemu *fe)
72 {
73 /* stub */
74 return &fe->fe_f2;
75 }
76
77 /*
78 * taylor expansion used by sinh(), cosh().
79 */
80 static struct fpn *
81 __fpu_sinhcosh_taylor(struct fpemu *fe, struct fpn *s0, uint32_t f)
82 {
83 struct fpn res;
84 struct fpn x2;
85 struct fpn *s1;
86 struct fpn *r;
87 int sign;
88 uint32_t k;
89
90 /* x2 := x * x */
91 CPYFPN(&fe->fe_f1, &fe->fe_f2);
92 r = fpu_mul(fe);
93 CPYFPN(&x2, r);
94
95 /* res := s0 */
96 CPYFPN(&res, s0);
97
98 sign = 1; /* sign := (-1)^n */
99
100 for (;;) {
101 /* (f1 :=) s0 * x^2 */
102 CPYFPN(&fe->fe_f1, s0);
103 CPYFPN(&fe->fe_f2, &x2);
104 r = fpu_mul(fe);
105 CPYFPN(&fe->fe_f1, r);
106
107 /*
108 * for sin(), s1 := s0 * x^2 / (2n+1)2n
109 * for cos(), s1 := s0 * x^2 / 2n(2n-1)
110 */
111 k = f * (f + 1);
112 fpu_explode(fe, &fe->fe_f2, FTYPE_LNG, &k);
113 s1 = fpu_div(fe);
114
115 /* break if s1 is enough small */
116 if (ISZERO(s1))
117 break;
118 if (res.fp_exp - s1->fp_exp >= FP_NMANT)
119 break;
120
121 /* s0 := s1 for next loop */
122 CPYFPN(s0, s1);
123
124 /* res += s1 */
125 CPYFPN(&fe->fe_f2, s1);
126 CPYFPN(&fe->fe_f1, &res);
127 r = fpu_add(fe);
128 CPYFPN(&res, r);
129
130 f += 2;
131 sign ^= 1;
132 }
133
134 CPYFPN(&fe->fe_f2, &res);
135 return &fe->fe_f2;
136 }
137
138 struct fpn *
139 fpu_cosh(struct fpemu *fe)
140 {
141 struct fpn s0;
142 struct fpn *r;
143
144 if (ISNAN(&fe->fe_f2))
145 return &fe->fe_f2;
146
147 if (ISINF(&fe->fe_f2)) {
148 fe->fe_f2.fp_sign = 0;
149 return &fe->fe_f2;
150 }
151
152 fpu_const(&s0, FPU_CONST_1);
153 r = __fpu_sinhcosh_taylor(fe, &s0, 1);
154 CPYFPN(&fe->fe_f2, r);
155
156 return &fe->fe_f2;
157 }
158
159 struct fpn *
160 fpu_sinh(struct fpemu *fe)
161 {
162 struct fpn s0;
163 struct fpn *r;
164
165 if (ISNAN(&fe->fe_f2))
166 return &fe->fe_f2;
167 if (ISINF(&fe->fe_f2))
168 return &fe->fe_f2;
169
170 CPYFPN(&s0, &fe->fe_f2);
171 r = __fpu_sinhcosh_taylor(fe, &s0, 2);
172 CPYFPN(&fe->fe_f2, r);
173
174 return &fe->fe_f2;
175 }
176
177 struct fpn *
178 fpu_tanh(struct fpemu *fe)
179 {
180 struct fpn x;
181 struct fpn s;
182 struct fpn *r;
183 int sign;
184
185 if (ISNAN(&fe->fe_f2))
186 return &fe->fe_f2;
187
188 if (ISINF(&fe->fe_f2)) {
189 sign = fe->fe_f2.fp_sign;
190 fpu_const(&fe->fe_f2, FPU_CONST_1);
191 fe->fe_f2.fp_sign = sign;
192 return &fe->fe_f2;
193 }
194
195 CPYFPN(&x, &fe->fe_f2);
196
197 /* sinh(x) */
198 CPYFPN(&fe->fe_f2, &x);
199 r = fpu_sinh(fe);
200 CPYFPN(&s, r);
201
202 /* cosh(x) */
203 CPYFPN(&fe->fe_f2, &x);
204 r = fpu_cosh(fe);
205 CPYFPN(&fe->fe_f2, r);
206
207 CPYFPN(&fe->fe_f1, &s);
208 r = fpu_div(fe);
209
210 CPYFPN(&fe->fe_f2, r);
211
212 return &fe->fe_f2;
213 }
214