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
13 THIS SOFTWARE IS PROVIDED "AS IS", WITH ABSOLUTELY NO WARRANTY, REAL
20 static const struct fp_ext fp_one
=
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
);
29 fp_fsqrt(struct fp_ext
*dest
, struct fp_ext
*src
)
31 struct fp_ext tmp
, src2
;
34 dprint(PINSTR
, "fsqrt\n");
36 fp_monadic_check(dest
, src
);
49 * sqrt(m) * 2^(p) , if e = 2*p
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
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.
62 if (!(exp
& 1)) /* lowest bit of exponent is set */
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)
73 fp_fadd(dest
, &fp_one
);
74 dest
->exp
--; /* * 1/2 */
77 * We now apply the newton rule to the function
79 * which has a null point on x = sqrt(r).
82 * x' := x - f(x)/f'(x)
83 * = x - (x^2 -r)/(2*x)
88 for (i
= 0; i
< 9; i
++) {
89 fp_copy_ext(&tmp
, &src2
);
96 dest
->exp
+= (exp
- 0x3FFF) / 2;
102 fp_fetoxm1(struct fp_ext
*dest
, struct fp_ext
*src
)
106 fp_monadic_check(dest
, src
);
112 fp_fetox(struct fp_ext
*dest
, struct fp_ext
*src
)
116 fp_monadic_check(dest
, src
);
122 fp_ftwotox(struct fp_ext
*dest
, struct fp_ext
*src
)
126 fp_monadic_check(dest
, src
);
132 fp_ftentox(struct fp_ext
*dest
, struct fp_ext
*src
)
136 fp_monadic_check(dest
, src
);
142 fp_flogn(struct fp_ext
*dest
, struct fp_ext
*src
)
146 fp_monadic_check(dest
, src
);
152 fp_flognp1(struct fp_ext
*dest
, struct fp_ext
*src
)
156 fp_monadic_check(dest
, src
);
162 fp_flog10(struct fp_ext
*dest
, struct fp_ext
*src
)
166 fp_monadic_check(dest
, src
);
172 fp_flog2(struct fp_ext
*dest
, struct fp_ext
*src
)
176 fp_monadic_check(dest
, src
);
182 fp_fgetexp(struct fp_ext
*dest
, struct fp_ext
*src
)
184 dprint(PINSTR
, "fgetexp\n");
186 fp_monadic_check(dest
, src
);
195 fp_conv_long2ext(dest
, (int)dest
->exp
- 0x3FFF);
197 fp_normalize_ext(dest
);
203 fp_fgetman(struct fp_ext
*dest
, struct fp_ext
*src
)
205 dprint(PINSTR
, "fgetman\n");
207 fp_monadic_check(dest
, src
);