1 /* Copyright (C) 1992, 1993, 2000 Aladdin Enterprises. All rights reserved. 2 3 This software is provided AS-IS with no warranty, either express or 4 implied. 5 6 This software is distributed under license and may not be copied, 7 modified or distributed except as expressly authorized under the terms 8 of the license contained in the file LICENSE in this distribution. 9 10 For more information about licensing, please refer to 11 http://www.ghostscript.com/licensing/. For information on 12 commercial licensing, go to http://www.artifex.com/licensing/ or 13 contact Artifex Software, Inc., 101 Lucas Valley Road #110, 14 San Rafael, CA 94903, U.S.A., +1(415)492-9861. 15 */ 16 17 /* $Id: gxfrac.h,v 1.5 2002/02/21 22:24:53 giles Exp $ */ 18 /* Fraction representation for Ghostscript */ 19 20 #ifndef gxfrac_INCLUDED 21 # define gxfrac_INCLUDED 22 23 /* 24 * Represent a fraction in [0.0..1.0]. Note that the 1.0 endpoint is 25 * included. Since undercolor removal requires a signed frac, we limit 26 * fracs to 15 bits rather than 16. 27 */ 28 typedef short frac; 29 typedef short signed_frac; 30 31 #define arch_log2_sizeof_frac arch_log2_sizeof_short 32 #define arch_sizeof_frac arch_sizeof_short 33 #define frac_bits 15 34 #define frac_0 ((frac)0) 35 36 /* 37 * Normally one would represent a fractional value of this kind as a short 38 * integer, in [-32767..32767]. Unfortunately, this approach cannot 39 * represent any of the common values like 1/2, 1/3, or 1/5 exactly, causing 40 * rounding errors. Instead, we opt for using the range [-32760..32760], 41 * which allows exact representation of almost all commonly used fractions 42 * (e.g., N/360 for 0<=N<=360). 43 */ 44 #define frac_1_0bits 3 45 #define frac_1 ((frac)0x7ff8) 46 #define frac_1_long ((long)frac_1) 47 #define frac_1_float ((float)frac_1) 48 /* Conversion between fracs and floats. */ 49 #define frac2float(fr) ((fr) / frac_1_float) 50 #define float2frac(fl) ((frac)(((fl) + 0.5 / frac_1_float) * frac_1_float)) 51 52 /* 53 * Conversion between unsigned fracs and bytes (or, in general, 54 * shorter integers) representing fractions. This is highly dependent 55 * on the definition of frac_1 above. 56 */ 57 #define _frac2s(fr)\ 58 (((fr) >> (frac_bits - frac_1_0bits)) + (fr)) 59 #define frac2bits(fr, nb)\ 60 ((uint)(_frac2s(fr) >> (frac_bits - (nb)))) 61 #define frac2byte(fr) ((byte)frac2bits(fr, 8)) 62 /* bits2frac requires frac_bits / 2 <= nb <= frac_bits. */ 63 #define bits2frac(v, nb) ((frac)(\ 64 ((frac)(v) << (frac_bits - (nb))) +\ 65 ((v) >> ((nb) * 2 - frac_bits)) -\ 66 ((v) >> ((nb) - frac_1_0bits)) )) 67 #define byte2frac(b) bits2frac(b, 8) 68 /* Produce a result that is guaranteed to convert back to a frac */ 69 /* not exceeding the original value fr. */ 70 #define frac2bits_floor(fr, nb)\ 71 ((uint)((_frac2s(fr) - (_frac2s(fr) >> (nb))) >> (frac_bits - (nb)))) 72 /* 73 * Conversion between fracs and unsigned shorts. 74 */ 75 #define ushort_bits (arch_sizeof_short * 8) 76 #define frac2ushort(fr) ((ushort)(\ 77 ((fr) << (ushort_bits - frac_bits)) +\ 78 ((fr) >> (frac_bits * 2 - ushort_bits - frac_1_0bits)) )) 79 #define ushort2frac(us) ((frac)(\ 80 ((us) >> (ushort_bits - frac_bits)) -\ 81 ((us) >> (ushort_bits - frac_1_0bits)) )) 82 /* 83 * Compute the quotient Q = floor(P / frac_1), 84 * where P is the (ulong) product of a uint or ushort V and a frac F. 85 * See gxarith.h for the underlying algorithm. 86 */ 87 #define frac_1_quo(p)\ 88 ( (((p) >> frac_1_0bits) + ((p) >> frac_bits) + 1) >> (frac_bits - frac_1_0bits) ) 89 /* 90 * Compute the remainder similarly, having already computed the quotient. 91 * This is, of course, P - Q * frac_1. 92 */ 93 #define frac_1_rem(p, q)\ 94 ((frac)( (uint)(p) - ((q) << frac_bits) + ((q) << frac_1_0bits) )) 95 96 #endif /* gxfrac_INCLUDED */ 97