1 #ifndef FMATH_H
2 #define FMATH_H
3 
4 /* FMath.h (C) 1989, Mark C. Peterson, CompuServe [70441,3353]
5      All rights reserved.
6 
7    Code may be used in any program provided the author is credited
8      either during program execution or in the documentation.  Source
9      code may be distributed only in combination with public domain or
10      shareware source code.  Source code may be modified provided the
11      copyright notice and this message is left unchanged and all
12      modifications are clearly documented.
13 
14      I would appreciate a copy of any work which incorporates this code,
15      however this is optional.
16 
17      Mark C. Peterson
18      128 Hamden Ave., F
19      Waterbury, CT 06704
20      (203) 754-1162
21 
22      Notes below document changes to Mark's original file:
23 
24      Date       Change                                    Changer
25      ============================================================
26      07-16-89 - Added sqrt define per Mark's suggestion   TIW
27      07-26-89 - Added documentation and complex support   MCP
28 */
29 
30 /*****************
31  * Documentation *
32  *****************
33 
34    #include "fmath.h"
35    float x, y, z;
36    int Pot, Fudge;
37 
38                    23-bit accuracy (limit of type float)
39      Regular Implementation               Fast Math Implementation
40  --------------------------------------------------------------------
41          z = x + y;                          fAdd(x, y, z);
42          z = x * y;                          fMul(x, y, z);
43          z = x * x;                          fSqr(x, z);
44          z = x / y;                          fDiv(x, y, z);
45          z = x * 2;                          fShift(x, 1, z);
46          z = x * 16;                         fShift(x, 4, z);
47          z = x / 32;                         fShift(x, -5, z);
48          z = x / (pow(2.0, (double)Pot));    fShift(x, -Pot, z);
49          z = (float)Pot * (1L << Fudge);     Fg2Float(Pot, Fudge, z);
50          Pot = (int)(z / (1L << Fudge));     Pot = Float2Fg(z, Fudge);
51 
52                   Complex numbers using fComplex structures
53          z = x**2                            fSqrZ(&x, &z);      mod updated
54          z.mod = (z.x*z.x)+(z.y*z.y)         fModZ(&z);          mod updated
55          z = 1 / x                           fInvZ(&x, &z);      mod updated
56          z = x * y                           fMulZ(&x, &y, &z);  mod updated
57          z = x / y                           fDivZ(&x, &y, &z);  mod updated
58 
59                             16-bit accuracy
60      Regular Implementation               Fast Math Implementation
61  --------------------------------------------------------------------
62          z = x * y;                          fMul16(x, y, z);
63          z = x * x;                          fSqr16(x, z);
64 
65                             14-bit accuracy
66      Regular Implementation               Fast Math Implementation
67  --------------------------------------------------------------------
68          z = log(x);                         fLog14(x, z);
69          z = exp(x);                         fExp14(x, z);
70          z = pow(x, y);                      fPow14(x, y, z);
71 
72                             12-bit accuracy
73      Regular Implementation               Fast Math Implementation
74  --------------------------------------------------------------------
75          z = sin(x);                         fSin12(x, z);
76          z = cos(x);                         fCos12(x, z);
77          z = sinh(x);                        fSinh12(x, z);
78          z = cosh(x);                        fCosh12(x, z);
79 
80                   Complex numbers using fComplex structures
81          z = sin(x)                          fSinZ(&x, &z);
82          z = cos(x)                          fCosZ(&x, &z);
83          z = tan(x)                          fTagZ(&x, &z);
84          z = sinh(x)                         fSinhZ(&x, &z);
85          z = cosh(x)                         fCoshZ(&x, &z);
86          z = tanh(x)                         fCoshZ(&x, &z);
87 
88 Just be sure to declare x, y, and z as type floats instead of type double.
89 */
90 
91 long
92 #ifndef XFRACT
93    far RegFg2Float(long x, char FudgeFact),
94    far RegSftFloat(long x, char Shift),
95 #else
96    far RegFg2Float(long x, int FudgeFact),
97    far RegSftFloat(long x, int Shift),
98 #endif
99    far RegFloat2Fg(long x, int Fudge),
100    far RegAddFloat(long x, long y),
101    far RegDivFloat(long x, long y),
102    far RegMulFloat(long x, long y),
103    far RegSqrFloat(long x),
104    far RegSubFloat(long x, long y);
105 long
106    far r16Mul(long x, long y),
107    far r16Sqr(long x);
108 int
109         far sin13(long x),
110         far cos13(long x),
111         far FastCosine(int x),
112         far FastSine(int x);
113 long
114         far FastHypCosine(int x),
115         far FastHypSine(int x),
116    far sinh13(long x),
117    far cosh13(long x);
118 long far LogFudged(unsigned long x, int Fudge);
119 long far LogFloat14(unsigned long x);
120 unsigned long far ExpFudged(long x, int Fudge);
121 long far ExpFloat14(long x);
122 
123 #define fAdd(x, y, z) (void)((*(long*)&z) = RegAddFloat(*(long*)&x, *(long*)&y))
124 #define fMul(x, y, z) (void)((*(long*)&z) = RegMulFloat(*(long*)&x, *(long*)&y))
125 #define fDiv(x, y, z) (void)((*(long*)&z) = RegDivFloat(*(long*)&x, *(long*)&y))
126 #define fSub(x, y, z) (void)((*(long*)&z) = RegSubFloat(*(long*)&x, *(long*)&y))
127 #define fMul16(x, y, z) (void)((*(long*)&z) = r16Mul(*(long*)&x, *(long*)&y))
128 #define fSqr16(x, z) (void)((*(long*)&z) = r16Sqr(*(long*)&x))
129 #define fSqr(x, z) (void)((*(long*)&z) = RegSqrFloat(*(long*)&x))
130 #define fShift(x, Shift, z) (void)((*(long*)&z) = \
131    RegSftFloat(*(long*)&x, Shift))
132 #define Fg2Float(x, f, z) (void)((*(long*)&z) = RegFg2Float(x, f))
133 #define Float2Fg(x, f) RegFloat2Fg(*(long*)&x, f)
134 #define fSin12(x, z) (void)((*(long*)&z) = \
135    RegFg2Float((long)sin13(Float2Fg(x, 13)), 13))
136 #define fCos12(x, z) (void)((*(long*)&z) = \
137    RegFg2Float((long)cos13(Float2Fg(x, 13)), 13))
138 #define fSinh12(x, z) (void)((*(long*)&z) = \
139    RegFg2Float(sinh13(Float2Fg(x, 13)), 13))
140 #define fCosh12(x, z) (void)((*(long*)&z) = \
141    RegFg2Float(cosh13(Float2Fg(x, 13)), 13))
142 #define fLog14(x, z) (void)((*(long*)&z) = \
143         RegFg2Float(LogFloat14(*(long*)&x), 16))
144 #define fExp14(x, z) (void)((*(long*)&z) = ExpFloat14(*(long*)&x));
145 #define fPow14(x, y, z) fLog14(x, z); fMul16(z, y, z); fExp14(z, z)
146 #define fSqrt14(x, z) fLog14(x, z); fShift(z, -1, z); fExp14(z, z)
147 
148 struct fComplex {
149    float x, y, mod;
150 };
151 
152 void
153    fSqrZ(struct fComplex *x, struct fComplex *z),
154    fMod(struct fComplex *x),
155    fInvZ(struct fComplex *x, struct fComplex *z),
156    fMulZ(struct fComplex *x, struct fComplex *y, struct fComplex *z),
157    fDivZ(struct fComplex *x, struct fComplex *y, struct fComplex *z),
158    fSinZ(struct fComplex *x, struct fComplex *z),
159    fCosZ(struct fComplex *x, struct fComplex *z),
160    fTanZ(struct fComplex *x, struct fComplex *z),
161    fSinhZ(struct fComplex *x, struct fComplex *z),
162    fCoshZ(struct fComplex *x, struct fComplex *z),
163    fTanhZ(struct fComplex *x, struct fComplex *z);
164 
165 #endif
166