Linux 5.6.13
[linux/fpc-iii.git] / arch / m68k / math-emu / fp_log.c
blob0663067870f2a9166dbdacb001cbeefdb97b37a7
1 /*
3 fp_trig.c: floating-point math routines for the Linux-m68k
4 floating point emulator.
6 Copyright (c) 1998-1999 David Huggins-Daines / Roman Zippel.
8 I hereby give permission, free of charge, to copy, modify, and
9 redistribute this software, in source or binary form, provided that
10 the above copyright notice and the following disclaimer are included
11 in all such copies.
13 THIS SOFTWARE IS PROVIDED "AS IS", WITH ABSOLUTELY NO WARRANTY, REAL
14 OR IMPLIED.
18 #include "fp_emu.h"
20 static const struct fp_ext fp_one =
22 .exp = 0x3fff,
25 extern struct fp_ext *fp_fadd(struct fp_ext *dest, const struct fp_ext *src);
26 extern struct fp_ext *fp_fdiv(struct fp_ext *dest, const struct fp_ext *src);
28 struct fp_ext *
29 fp_fsqrt(struct fp_ext *dest, struct fp_ext *src)
31 struct fp_ext tmp, src2;
32 int i, exp;
34 dprint(PINSTR, "fsqrt\n");
36 fp_monadic_check(dest, src);
38 if (IS_ZERO(dest))
39 return dest;
41 if (dest->sign) {
42 fp_set_nan(dest);
43 return dest;
45 if (IS_INF(dest))
46 return dest;
49 * sqrt(m) * 2^(p) , if e = 2*p
50 * sqrt(m*2^e) =
51 * sqrt(2*m) * 2^(p) , if e = 2*p + 1
53 * So we use the last bit of the exponent to decide whether to
54 * use the m or 2*m.
56 * Since only the fractional part of the mantissa is stored and
57 * the integer part is assumed to be one, we place a 1 or 2 into
58 * the fixed point representation.
60 exp = dest->exp;
61 dest->exp = 0x3FFF;
62 if (!(exp & 1)) /* lowest bit of exponent is set */
63 dest->exp++;
64 fp_copy_ext(&src2, dest);
67 * The taylor row around a for sqrt(x) is:
68 * sqrt(x) = sqrt(a) + 1/(2*sqrt(a))*(x-a) + R
69 * With a=1 this gives:
70 * sqrt(x) = 1 + 1/2*(x-1)
71 * = 1/2*(1+x)
73 fp_fadd(dest, &fp_one);
74 dest->exp--; /* * 1/2 */
77 * We now apply the newton rule to the function
78 * f(x) := x^2 - r
79 * which has a null point on x = sqrt(r).
81 * It gives:
82 * x' := x - f(x)/f'(x)
83 * = x - (x^2 -r)/(2*x)
84 * = x - (x - r/x)/2
85 * = (2*x - x + r/x)/2
86 * = (x + r/x)/2
88 for (i = 0; i < 9; i++) {
89 fp_copy_ext(&tmp, &src2);
91 fp_fdiv(&tmp, dest);
92 fp_fadd(dest, &tmp);
93 dest->exp--;
96 dest->exp += (exp - 0x3FFF) / 2;
98 return dest;
101 struct fp_ext *
102 fp_fetoxm1(struct fp_ext *dest, struct fp_ext *src)
104 uprint("fetoxm1\n");
106 fp_monadic_check(dest, src);
108 return dest;
111 struct fp_ext *
112 fp_fetox(struct fp_ext *dest, struct fp_ext *src)
114 uprint("fetox\n");
116 fp_monadic_check(dest, src);
118 return dest;
121 struct fp_ext *
122 fp_ftwotox(struct fp_ext *dest, struct fp_ext *src)
124 uprint("ftwotox\n");
126 fp_monadic_check(dest, src);
128 return dest;
131 struct fp_ext *
132 fp_ftentox(struct fp_ext *dest, struct fp_ext *src)
134 uprint("ftentox\n");
136 fp_monadic_check(dest, src);
138 return dest;
141 struct fp_ext *
142 fp_flogn(struct fp_ext *dest, struct fp_ext *src)
144 uprint("flogn\n");
146 fp_monadic_check(dest, src);
148 return dest;
151 struct fp_ext *
152 fp_flognp1(struct fp_ext *dest, struct fp_ext *src)
154 uprint("flognp1\n");
156 fp_monadic_check(dest, src);
158 return dest;
161 struct fp_ext *
162 fp_flog10(struct fp_ext *dest, struct fp_ext *src)
164 uprint("flog10\n");
166 fp_monadic_check(dest, src);
168 return dest;
171 struct fp_ext *
172 fp_flog2(struct fp_ext *dest, struct fp_ext *src)
174 uprint("flog2\n");
176 fp_monadic_check(dest, src);
178 return dest;
181 struct fp_ext *
182 fp_fgetexp(struct fp_ext *dest, struct fp_ext *src)
184 dprint(PINSTR, "fgetexp\n");
186 fp_monadic_check(dest, src);
188 if (IS_INF(dest)) {
189 fp_set_nan(dest);
190 return dest;
192 if (IS_ZERO(dest))
193 return dest;
195 fp_conv_long2ext(dest, (int)dest->exp - 0x3FFF);
197 fp_normalize_ext(dest);
199 return dest;
202 struct fp_ext *
203 fp_fgetman(struct fp_ext *dest, struct fp_ext *src)
205 dprint(PINSTR, "fgetman\n");
207 fp_monadic_check(dest, src);
209 if (IS_ZERO(dest))
210 return dest;
212 if (IS_INF(dest))
213 return dest;
215 dest->exp = 0x3FFF;
217 return dest;