1 /* Copyright (C) 1992, 1993, 2000 artofcode LLC. All rights reserved. 2 3 This program is free software; you can redistribute it and/or modify it 4 under the terms of the GNU General Public License as published by the 5 Free Software Foundation; either version 2 of the License, or (at your 6 option) any later version. 7 8 This program is distributed in the hope that it will be useful, but 9 WITHOUT ANY WARRANTY; without even the implied warranty of 10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 General Public License for more details. 12 13 You should have received a copy of the GNU General Public License along 14 with this program; if not, write to the Free Software Foundation, Inc., 15 59 Temple Place, Suite 330, Boston, MA, 02111-1307. 16 17 */ 18 19 /*$Id: gxfrac.h,v 1.3.4.1.2.1 2003/01/17 00:49:03 giles Exp $ */ 20 /* Fraction representation for Ghostscript */ 21 22 #ifndef gxfrac_INCLUDED 23 # define gxfrac_INCLUDED 24 25 /* 26 * Represent a fraction in [0.0..1.0]. Note that the 1.0 endpoint is 27 * included. Since undercolor removal requires a signed frac, we limit 28 * fracs to 15 bits rather than 16. 29 */ 30 typedef short frac; 31 typedef short signed_frac; 32 33 #define arch_log2_sizeof_frac arch_log2_sizeof_short 34 #define arch_sizeof_frac arch_sizeof_short 35 #define frac_bits 15 36 #define frac_0 ((frac)0) 37 38 /* 39 * Normally one would represent a fractional value of this kind as a short 40 * integer, in [-32767..32767]. Unfortunately, this approach cannot 41 * represent any of the common values like 1/2, 1/3, or 1/5 exactly, causing 42 * rounding errors. Instead, we opt for using the range [-32760..32760], 43 * which allows exact representation of almost all commonly used fractions 44 * (e.g., N/360 for 0<=N<=360). 45 */ 46 #define frac_1_0bits 3 47 #define frac_1 ((frac)0x7ff8) 48 #define frac_1_long ((long)frac_1) 49 #define frac_1_float ((float)frac_1) 50 /* Conversion between fracs and floats. */ 51 #define frac2float(fr) ((fr) / frac_1_float) 52 #define float2frac(fl) ((frac)(((fl) + 0.5 / frac_1_float) * frac_1_float)) 53 54 /* 55 * Conversion between unsigned fracs and bytes (or, in general, 56 * shorter integers) representing fractions. This is highly dependent 57 * on the definition of frac_1 above. 58 */ 59 #define _frac2s(fr)\ 60 (((fr) >> (frac_bits - frac_1_0bits)) + (fr)) 61 #define frac2bits(fr, nb)\ 62 ((uint)(_frac2s(fr) >> (frac_bits - (nb)))) 63 #define frac2byte(fr) ((byte)frac2bits(fr, 8)) 64 /* bits2frac requires frac_bits / 2 <= nb <= frac_bits. */ 65 #define bits2frac(v, nb) ((frac)(\ 66 ((frac)(v) << (frac_bits - (nb))) +\ 67 ((v) >> ((nb) * 2 - frac_bits)) -\ 68 ((v) >> ((nb) - frac_1_0bits)) )) 69 #define byte2frac(b) bits2frac(b, 8) 70 /* Produce a result that is guaranteed to convert back to a frac */ 71 /* not exceeding the original value fr. */ 72 #define frac2bits_floor(fr, nb)\ 73 ((uint)((_frac2s(fr) - (_frac2s(fr) >> (nb))) >> (frac_bits - (nb)))) 74 /* 75 * Conversion between fracs and unsigned shorts. 76 */ 77 #define ushort_bits (arch_sizeof_short * 8) 78 #define frac2ushort(fr) ((ushort)(\ 79 ((fr) << (ushort_bits - frac_bits)) +\ 80 ((fr) >> (frac_bits * 2 - ushort_bits - frac_1_0bits)) )) 81 #define ushort2frac(us) ((frac)(\ 82 ((us) >> (ushort_bits - frac_bits)) -\ 83 ((us) >> (ushort_bits - frac_1_0bits)) )) 84 /* 85 * Compute the quotient Q = floor(P / frac_1), 86 * where P is the (ulong) product of a uint or ushort V and a frac F. 87 * See gxarith.h for the underlying algorithm. 88 */ 89 #define frac_1_quo(p)\ 90 ( (((p) >> frac_1_0bits) + ((p) >> frac_bits) + 1) >> (frac_bits - frac_1_0bits) ) 91 /* 92 * Compute the remainder similarly, having already computed the quotient. 93 * This is, of course, P - Q * frac_1. 94 */ 95 #define frac_1_rem(p, q)\ 96 ((frac)( (uint)(p) - ((q) << frac_bits) + ((q) << frac_1_0bits) )) 97 98 #endif /* gxfrac_INCLUDED */ 99