home *** CD-ROM | disk | FTP | other *** search
- /* File fixit.c Copyright (C) 1996 by John R. Montbriand. All Rights Reserved. */
-
- #include "fixit.h"
- #include <ToolUtils.h>
-
- /* File fixit.c
-
- Copyright (C) 1996 by John Montbriand. All Rights Reserved.
-
- Distribute freely in areas where the laws of copyright apply.
-
- Use at your own risk.
-
- Do not distribute modified copies.
-
- These various string list libraries are for free!
-
- See the file fixit.txt for details.
-
- */
-
-
- /* the method for calculating the square root used here is
- similar to newton's method, but originates from
- the pythagorean era and involves fewer variables
- and calculations. */
- Fixed FixSqrt(Fixed x) {
- Fixed Xn, lastXn;
- long i;
-
- /* special cases */
- if (x < 0)
- return 0x80000000;
- else if (x == 0)
- return 0;
-
- /* iterate for stability, max out at 16 */
- Xn = x;
- lastXn = 0;
- for (i = 0; i < 16; i++) {
- lastXn = Xn;
- Xn = FixDiv(Xn + FixDiv(x, Xn), 0x00020000);
- if (lastXn == Xn) break;
- }
-
- /* if the result is negative, we failed.. */
- if (Xn < 0) return 0x80000000;
-
- /* done */
- return Xn;
- }
-
- /* anybody have any ideas about how to make this faster,
- It'd be great to hear from you. */
- Fixed FixExp(Fixed x, Fixed a) {
- Fixed y, xx, root = x, mask = 0x00008000;
-
- /* reorganize parameters */
- if (a < 0) {
- xx = FixDiv(0x00010000, x);
- a = - a;
- } else xx = x;
-
- /* calculate integral exponent part */
- for (y = 0x00010000; a >= 0x00010000; a -= 0x00010000)
- y = FixMul(y, xx);
-
- /* calculate fractional exponent part */
- root = xx;
- mask = 0x00008000;
- while (a != 0) {
- root = FixSqrt(root);
- if ((a & mask) != 0) y = FixMul(y, root);
- a &= (~mask);
- mask >>= 1;
- }
-
- /* done */
- return y;
- }
-
- Fixed FixSquare(Fixed x) {
- return FixMul(x, x);
- }
-
- Fixed FixCos(Fixed x) {
- return Frac2Fix(FracCos(x));
- }
-
- Fixed FixSin(Fixed x) {
- return Frac2Fix(FracSin(x));
- }
-
- Fixed FixTan(Fixed x) {
- return FixDiv(FracSin(x), FracCos(x));
- }
-
- /* end of file fixit.c */
-