| 1 |
/* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved. |
| 2 |
This software is licensed as OpenSource, under the Apache License, Version 2.0. |
| 3 |
This license is available at: http://opensource.org/licenses/Apache-2.0. */ |
| 4 |
|
| 5 |
/* |
| 6 |
fp.h -- Interface to Floating/Fixed package. |
| 7 |
*/ |
| 8 |
|
| 9 |
#ifndef FP_H |
| 10 |
#define FP_H |
| 11 |
|
| 12 |
#include "supportpublictypes.h" |
| 13 |
/* |
| 14 |
* Math Library Interface |
| 15 |
*/ |
| 16 |
|
| 17 |
/* This interface incorporates, by reference, the following procedures |
| 18 |
from the standard C math library, as documented in most C manuals; |
| 19 |
it may include others as well, but is not required to. |
| 20 |
|
| 21 |
acos, asin, atan, atan2, atod, atof, ceil, cos, ecvt, exp, fcvt, |
| 22 |
floor, frexp, ldexp, log, log10, modf, pow, sin, sqrt |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <math.h> |
| 26 |
|
| 27 |
#ifndef WINATM |
| 28 |
#define WINATM 0 |
| 29 |
#endif |
| 30 |
|
| 31 |
#if WINATM |
| 32 |
|
| 33 |
#include "atmtypes.h" |
| 34 |
|
| 35 |
#include "fpadjust.h" |
| 36 |
|
| 37 |
#else /* WINATM */ |
| 38 |
|
| 39 |
#ifndef FIXEDFUNC |
| 40 |
#define FIXEDFUNC |
| 41 |
#endif /* FIXEDFUNC */ |
| 42 |
|
| 43 |
#endif /* WINATM */ |
| 44 |
|
| 45 |
/* |
| 46 |
* Floating Point Interface |
| 47 |
*/ |
| 48 |
|
| 49 |
/* Exported Procedures */ |
| 50 |
|
| 51 |
#define os_ceil ceil |
| 52 |
#define os_floor floor |
| 53 |
#define os_fabs fabs |
| 54 |
#define os_log10 log10 |
| 55 |
#define os_sqrt sqrt |
| 56 |
#define os_atof atof |
| 57 |
|
| 58 |
/* Inline Procedures */ |
| 59 |
|
| 60 |
/* The following macros implement comparisons of real (float) values |
| 61 |
with zero. If IEEESOFT is true (defined in environment.h), these |
| 62 |
comparisons are done more efficiently than the general floating |
| 63 |
point compare subroutines would typically do them. |
| 64 |
Note: the argument r must be a simple variable (not an expression) |
| 65 |
of type real (float); it must not be a formal parameter |
| 66 |
of a procedure, which is a double even if declared to be a float. |
| 67 |
*/ |
| 68 |
|
| 69 |
#if IEEESOFT |
| 70 |
#define RtoILOOPHOLE(r) (*(integer *)(&(r))) |
| 71 |
#define RealEq0(r) ((RtoILOOPHOLE(r) << 1) == 0) |
| 72 |
#define RealNe0(r) ((RtoILOOPHOLE(r) << 1) != 0) |
| 73 |
#define RealGt0(r) (RtoILOOPHOLE(r) > 0) |
| 74 |
#define RealGe0(r) ((RtoILOOPHOLE(r) >= 0) || RealEq0(r)) |
| 75 |
#define RealLt0(r) ((RtoILOOPHOLE(r) < 0) && RealNe0(r)) |
| 76 |
#define RealLe0(r) (RtoILOOPHOLE(r) <= 0) |
| 77 |
#else /* IEEESOFT */ |
| 78 |
#define RealEq0(r) ((r) == 0.0) |
| 79 |
#define RealNe0(r) ((r) != 0.0) |
| 80 |
#define RealGt0(r) ((r) > 0.0) |
| 81 |
#define RealGe0(r) ((r) >= 0.0) |
| 82 |
#define RealLt0(r) ((r) < 0.0) |
| 83 |
#define RealLe0(r) ((r) <= 0.0) |
| 84 |
#endif /* IEEESOFT */ |
| 85 |
|
| 86 |
#ifndef fabs |
| 87 |
#define fabs(x) (RealLt0(x) ? -(x) : (x)) |
| 88 |
/* Returns the absolute value of x, which must be a simple variable |
| 89 |
of type real (float), as described above. */ |
| 90 |
#endif /* fabs */ |
| 91 |
|
| 92 |
#define RAD(a) ((a)*1.745329251994329577e-2) |
| 93 |
/* Converts the float or double value a from degrees to radians. */ |
| 94 |
|
| 95 |
/* |
| 96 |
* Fixed Point interface |
| 97 |
*/ |
| 98 |
|
| 99 |
#ifndef FIXEDHALF |
| 100 |
#define FIXEDHALF 0x00008000 |
| 101 |
#endif |
| 102 |
|
| 103 |
/* Data Structures */ |
| 104 |
|
| 105 |
/* A fixed point number consists of a sign bit, i integer bits, and f |
| 106 |
fraction bits; the underlying representation is simply an integer. |
| 107 |
The integer n represents the fixed point value n/(2**f). The current |
| 108 |
definition is pretty heavily dependent on an integer being 32 bits. |
| 109 |
|
| 110 |
There are three kinds of fixed point numbers: |
| 111 |
Fixed i = 15, f = 16, range [-32768, 32768) |
| 112 |
Frac i = 1, f = 30, range [-2, 2) |
| 113 |
UFrac i = 2, f = 30, unsigned, range [0, 4) |
| 114 |
The type "Fixed", defined in the basic types interface, is simply |
| 115 |
a typedef for integer and is type-equivalent to integer as far |
| 116 |
as the C compiler is concerned. |
| 117 |
|
| 118 |
Within the fp interface, the types Fixed, Frac, and UFrac are used to |
| 119 |
document the kinds of fixed point numbers that are the arguments |
| 120 |
and results of procedures. However, most clients of this interface |
| 121 |
declare variables to be of type Fixed, or occasionally of type integer, |
| 122 |
without specifying which kind of fixed point number is actually |
| 123 |
intended. Most Fixed numbers are of the first kind; the other two |
| 124 |
are provided as a convenience to certain specialized algorithms in |
| 125 |
the graphics machinery. |
| 126 |
|
| 127 |
All procedures, including the conversions from real, return a |
| 128 |
correctly signed "infinity" value FixedPosInf or FixedNegInf |
| 129 |
if an overflow occurs; they return zero if an underflow occurs. |
| 130 |
No overflow or underflow traps ever occur; it is the caller's |
| 131 |
responsibility either to detect an out-of-range result or to |
| 132 |
prevent it from occurring in the first place. |
| 133 |
|
| 134 |
All procedures compute exact intermediate results and then round |
| 135 |
according to some consistent rule such as IEEE "round to nearest". |
| 136 |
*/ |
| 137 |
|
| 138 |
#if (ARCH_64BIT == 1) |
| 139 |
typedef int /* Fixed, */ Frac, UFrac, *PFrac; |
| 140 |
#else |
| 141 |
typedef long int /* Fixed, */ Frac, UFrac, *PFrac; |
| 142 |
#endif |
| 143 |
|
| 144 |
#define FixedPosInf MAXinteger |
| 145 |
#define FixedNegInf MINinteger |
| 146 |
|
| 147 |
/* Pass arguments in registers, if using a Metrowerks compiler and target is a 68K */ |
| 148 |
|
| 149 |
/* Exported Procedures */ |
| 150 |
|
| 151 |
#if REGISTER_ARGS && ASM_fixmul |
| 152 |
extern Fixed FIXEDFUNC fixmul_reg(Fixed x MW_D0, Fixed y MW_D1); |
| 153 |
#define fixmul(a, b) fixmul_reg(a, b) |
| 154 |
#else |
| 155 |
extern Fixed FIXEDFUNC fixmul(Fixed x, Fixed y); |
| 156 |
#endif |
| 157 |
/* Multiples two Fixed numbers and returns a Fixed result. */ |
| 158 |
|
| 159 |
extern Frac FIXEDFUNC fracmul(Frac x, Frac y); |
| 160 |
/* Multiples two Frac numbers and returns a Frac result. */ |
| 161 |
|
| 162 |
#define fxfrmul(x, y) ((Fixed)fracmul((Frac)(x), y)) |
| 163 |
/* extern Fixed fxfrmul ( Fixed x, Frac y); |
| 164 |
Multiplies a Fixed number with a Frac number and returns a Fixed |
| 165 |
result. The order of the Fixed and Frac arguments does not matter. */ |
| 166 |
|
| 167 |
#if REGISTER_ARGS && ASM_fixdiv |
| 168 |
extern Fixed FIXEDFUNC fixdiv_reg(Fixed x MW_D0, Fixed y MW_D1); |
| 169 |
#define fixdiv(a, b) fixdiv_reg(a, b) |
| 170 |
#else |
| 171 |
extern Fixed FIXEDFUNC fixdiv(Fixed x, Fixed y); |
| 172 |
#endif |
| 173 |
/* Divides the Fixed number x by the Fixed number y and returns a |
| 174 |
Fixed result. */ |
| 175 |
|
| 176 |
extern Frac FIXEDFUNC fracdiv(Frac x, Frac y); |
| 177 |
/* Divides two Frac numbers and returns a Frac result. */ |
| 178 |
|
| 179 |
#define fracratio(x, y) (fixdiv((Fixed)(x), (Fixed)(y))) |
| 180 |
/* extern Fixed fracratio (Frac x, Frac y); |
| 181 |
Divides the Frac number x by the Frac number y and returns a |
| 182 |
Fixed result. */ |
| 183 |
|
| 184 |
extern Fixed FIXEDFUNC ufixratio(Card32 x, Card32 y); |
| 185 |
/* Divides the unsigned integer x by the unsigned integer y and |
| 186 |
returns a Fixed result. */ |
| 187 |
|
| 188 |
extern Frac FIXEDFUNC fixratio(Fixed x, Fixed y); |
| 189 |
/* Divides the Fixed number x by the Fixed number y and returns a |
| 190 |
Frac result. */ |
| 191 |
|
| 192 |
extern UFrac FIXEDFUNC fracsqrt(UFrac x); |
| 193 |
/* Computes the square root of the UFrac number x and returns a |
| 194 |
UFrac result. */ |
| 195 |
|
| 196 |
/* The following type conversions pass their float arguments and results |
| 197 |
by value; the C language forces them to be of type double. |
| 198 |
*/ |
| 199 |
|
| 200 |
extern double ASFixedToDouble(Fixed x); |
| 201 |
#define fixtodbl ASFixedToDouble |
| 202 |
/* Converts the Fixed number x to a double. */ |
| 203 |
|
| 204 |
extern Fixed ASDoubleToFixed(double d); |
| 205 |
#define dbltofix ASDoubleToFixed |
| 206 |
/* Converts the double d to a Fixed. */ |
| 207 |
|
| 208 |
extern double fractodbl(Frac x); |
| 209 |
/* Converts the Frac number x to a double. */ |
| 210 |
|
| 211 |
extern Frac dbltofrac(double d); |
| 212 |
/* Converts the double d to a Frac. */ |
| 213 |
|
| 214 |
/* The following type conversions pass their float arguments and results |
| 215 |
by pointer. This circumvents C's usual conversion of float arguments |
| 216 |
and results to and from double, which can have considerable |
| 217 |
performance consequences. Caution: do not call these procedures |
| 218 |
with pointers to float ("real") formal parameters, since those are |
| 219 |
actually of type double! |
| 220 |
*/ |
| 221 |
|
| 222 |
extern procedure fixtopflt(Fixed x, float *pf); |
| 223 |
/* Converts the Fixed number x to a float and stores the result at *pf. */ |
| 224 |
|
| 225 |
extern Fixed pflttofix(float *pf); |
| 226 |
/* Converts the float number at *pf to a Fixed and returns it. */ |
| 227 |
|
| 228 |
extern procedure fractopflt(Frac x, float *pf); |
| 229 |
/* Converts the Frac number x to a float and stores the result at *pf. */ |
| 230 |
|
| 231 |
extern Frac pflttofrac(float *pf); |
| 232 |
/* Converts the float number at *pf to a Frac and returns it. */ |
| 233 |
|
| 234 |
/* new code for DDR and anti-alias |
| 235 |
probably make some/all of this macros later |
| 236 |
*/ |
| 237 |
|
| 238 |
extern Fixed FCeilFModN(Fixed x, Card16 n); |
| 239 |
extern Fixed FFloorFModN(Fixed x, Card16 n); |
| 240 |
extern Fixed FRoundFModN(Fixed x, Card16 n); |
| 241 |
extern Int32 ICeilModN(Int32 x, Card16 n); |
| 242 |
extern Int32 IFloorModN(Int32 x, Card16 n); |
| 243 |
extern Fixed FFloorFModNR(Fixed v, Card16 n, Int16 r); |
| 244 |
extern Fixed FCeilFModNR(Fixed v, Card16 n, Int16 r); |
| 245 |
extern Fixed FRoundFModNR(Fixed v, Card16 n, Int16 r); |
| 246 |
|
| 247 |
/* Inline Procedures */ |
| 248 |
|
| 249 |
#define FixInt(x) ((Fixed)((Card32)(x) << 16)) |
| 250 |
/* Converts the integer x to a Fixed and returns it. */ |
| 251 |
|
| 252 |
#define FTrunc(x) ((integer)((x) >> 16)) |
| 253 |
/* Converts the Fixed number x to an integer, truncating it to the |
| 254 |
next lower integer value, and returns it. */ |
| 255 |
|
| 256 |
#define FTruncF(x) ((integer)((x)&0xFFFF0000)) |
| 257 |
/* Returns the Fixed value that represents the next lower integer value. */ |
| 258 |
|
| 259 |
#define FRound(x) ((integer)(((x) + (FIXEDHALF)) >> 16)) |
| 260 |
/* Converts the Fixed number x to an integer, rounding it to the |
| 261 |
nearest integer value, and returns it. */ |
| 262 |
|
| 263 |
#define FRoundF(x) ((integer)(((x) + (FIXEDHALF)) & 0xFFFF0000)) |
| 264 |
/* Returns x as a Fixed number, with its integer part rounded to the |
| 265 |
nearest integer value, and its fraction part 0. */ |
| 266 |
|
| 267 |
#define FCeil(x) (FTrunc((x) + 0xFFFF)) |
| 268 |
#define FCeilF(x) (FTruncF((x) + 0xFFFF)) |
| 269 |
|
| 270 |
#define FracToFixed(f) ((f) >> 14) |
| 271 |
#define FixedToFrac(f) ((f) << 14) |
| 272 |
|
| 273 |
#endif /* FP_H */ |
| 274 |
/* v004 taft Tue Nov 22 14:09:28 PST 1988 */ |
| 275 |
/* v005 brotz Fri Feb 17 11:16:46 PST 1989 */ |