3 fp_log.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
22 static const struct fp_ext fp_one
= {
26 struct fp_ext
*fp_fsqrt(struct fp_ext
*dest
, struct fp_ext
*src
)
28 struct fp_ext tmp
, src2
;
31 dprint(PINSTR
, "fsqrt\n");
33 fp_monadic_check(dest
, src
);
46 * sqrt(m) * 2^(p) , if e = 2*p
48 * sqrt(2*m) * 2^(p) , if e = 2*p + 1
50 * So we use the last bit of the exponent to decide whether to
53 * Since only the fractional part of the mantissa is stored and
54 * the integer part is assumed to be one, we place a 1 or 2 into
55 * the fixed point representation.
59 if (!(exp
& 1)) /* lowest bit of exponent is set */
61 fp_copy_ext(&src2
, dest
);
64 * The taylor row around a for sqrt(x) is:
65 * sqrt(x) = sqrt(a) + 1/(2*sqrt(a))*(x-a) + R
66 * With a=1 this gives:
67 * sqrt(x) = 1 + 1/2*(x-1)
70 /* It is safe to cast away the constness, as fp_one is normalized */
71 fp_fadd(dest
, (struct fp_ext
*)&fp_one
);
72 dest
->exp
--; /* * 1/2 */
75 * We now apply the newton rule to the function
77 * which has a null point on x = sqrt(r).
80 * x' := x - f(x)/f'(x)
81 * = x - (x^2 -r)/(2*x)
86 for (i
= 0; i
< 9; i
++) {
87 fp_copy_ext(&tmp
, &src2
);
94 dest
->exp
+= (exp
- 0x3FFF) / 2;
99 struct fp_ext
*fp_fetoxm1(struct fp_ext
*dest
, struct fp_ext
*src
)
103 fp_monadic_check(dest
, src
);
108 struct fp_ext
*fp_fetox(struct fp_ext
*dest
, struct fp_ext
*src
)
112 fp_monadic_check(dest
, src
);
117 struct fp_ext
*fp_ftwotox(struct fp_ext
*dest
, struct fp_ext
*src
)
121 fp_monadic_check(dest
, src
);
126 struct fp_ext
*fp_ftentox(struct fp_ext
*dest
, struct fp_ext
*src
)
130 fp_monadic_check(dest
, src
);
135 struct fp_ext
*fp_flogn(struct fp_ext
*dest
, struct fp_ext
*src
)
139 fp_monadic_check(dest
, src
);
144 struct fp_ext
*fp_flognp1(struct fp_ext
*dest
, struct fp_ext
*src
)
148 fp_monadic_check(dest
, src
);
153 struct fp_ext
*fp_flog10(struct fp_ext
*dest
, struct fp_ext
*src
)
157 fp_monadic_check(dest
, src
);
162 struct fp_ext
*fp_flog2(struct fp_ext
*dest
, struct fp_ext
*src
)
166 fp_monadic_check(dest
, src
);
171 struct fp_ext
*fp_fgetexp(struct fp_ext
*dest
, struct fp_ext
*src
)
173 dprint(PINSTR
, "fgetexp\n");
175 fp_monadic_check(dest
, src
);
184 fp_conv_long2ext(dest
, (int)dest
->exp
- 0x3FFF);
186 fp_normalize_ext(dest
);
191 struct fp_ext
*fp_fgetman(struct fp_ext
*dest
, struct fp_ext
*src
)
193 dprint(PINSTR
, "fgetman\n");
195 fp_monadic_check(dest
, src
);