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 wether 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);
115 fp_fetox(struct fp_ext *dest, struct fp_ext *src)
119 fp_monadic_check(dest, src);
125 fp_ftwotox(struct fp_ext *dest, struct fp_ext *src)
129 fp_monadic_check(dest, src);
135 fp_ftentox(struct fp_ext *dest, struct fp_ext *src)
139 fp_monadic_check(dest, src);
145 fp_flogn(struct fp_ext *dest, struct fp_ext *src)
149 fp_monadic_check(dest, src);
155 fp_flognp1(struct fp_ext *dest, struct fp_ext *src)
159 fp_monadic_check(dest, src);
165 fp_flog10(struct fp_ext *dest, struct fp_ext *src)
169 fp_monadic_check(dest, src);
175 fp_flog2(struct fp_ext *dest, struct fp_ext *src)
179 fp_monadic_check(dest, src);
185 fp_fgetexp(struct fp_ext *dest, struct fp_ext *src)
187 dprint(PINSTR, "fgetexp\n");
189 fp_monadic_check(dest, src);
198 fp_conv_long2ext(dest, (int)dest->exp - 0x3FFF);
200 fp_normalize_ext(dest);
206 fp_fgetman(struct fp_ext *dest, struct fp_ext *src)
208 dprint(PINSTR, "fgetman\n");
210 fp_monadic_check(dest, src);