| 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 |
fixed.c |
| 7 |
*/ |
| 8 |
|
| 9 |
/* |
| 10 |
Fixed point multiply, divide, and conversions -- machine independent. |
| 11 |
See fp.h for the specification of what these procedures do. |
| 12 |
|
| 13 |
The double-to-int conversion is assumed to truncate rather than round; |
| 14 |
this is specified by the C language manual. The direction of truncation |
| 15 |
is machine-dependent, but is toward zero rather than toward minus |
| 16 |
infinity on the Vax and Sun. This explains the peculiar way in which |
| 17 |
fixmul and fixdiv do rounding. |
| 18 |
*/ |
| 19 |
|
| 20 |
#include "supportpublictypes.h" |
| 21 |
#include "supportfp.h" |
| 22 |
|
| 23 |
#define fixedScale 65536.0 /* i=15, f=16, range [-32768, 32768) */ |
| 24 |
#define fracScale 1073741824.0 /* i=1, f=30 , range [-2, 2) */ |
| 25 |
|
| 26 |
PUBLIC Fixed FIXEDFUNC fixmul(Fixed x, Fixed y) /* returns x*y */ |
| 27 |
{ |
| 28 |
double d = (double)x * (double)y / fixedScale; |
| 29 |
if (d >= (double)0.0) { |
| 30 |
d += (double)0.5; |
| 31 |
if (d < (double)FixedPosInf) |
| 32 |
return (Fixed)d; |
| 33 |
else |
| 34 |
return FixedPosInf; |
| 35 |
} else { |
| 36 |
d -= (double)0.5; |
| 37 |
if (d > (double)FixedNegInf) |
| 38 |
return (Fixed)d; |
| 39 |
else |
| 40 |
return FixedNegInf; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
PUBLIC Fixed FIXEDFUNC fixdiv(Fixed x, Fixed y) /* returns x/y */ |
| 45 |
{ |
| 46 |
double d; |
| 47 |
if (y == 0) return (x < 0) ? FixedNegInf : FixedPosInf; |
| 48 |
d = (double)x / (double)y * fixedScale; |
| 49 |
d += (d < 0) ? -0.5 : 0.5; |
| 50 |
if (d >= FixedPosInf) return FixedPosInf; |
| 51 |
if (d <= FixedNegInf) return FixedNegInf; |
| 52 |
return (Fixed)d; |
| 53 |
} |
| 54 |
|
| 55 |
PUBLIC procedure fixtopflt(Fixed x, float *pf) { *pf = (float)x / (float)fixedScale; } |
| 56 |
|
| 57 |
PUBLIC Fixed pflttofix(float *pf) /* converts float *pf to fixed */ |
| 58 |
{ |
| 59 |
float f = *pf; |
| 60 |
if (f >= (float)(FixedPosInf / fixedScale)) return FixedPosInf; |
| 61 |
if (f <= (float)(FixedNegInf / fixedScale)) return FixedNegInf; |
| 62 |
return (Fixed)(f * fixedScale); |
| 63 |
} |