xref: /plan9/sys/src/cmd/gs/src/gxarith.h (revision 593dc095aefb2a85c828727bbfa9da139a49bdf4)
1 /* Copyright (C) 1990, 1993, 1994, 1996, 1998 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 #ifndef gxarith_INCLUDED
18 #  define gxarith_INCLUDED
19 
20 /* $Id: gxarith.h,v 1.5 2002/06/16 08:45:43 lpd Exp $ */
21 /* Arithmetic macros for Ghostscript library */
22 
23 /* Define an in-line abs function, good for any signed numeric type. */
24 #define any_abs(x) ((x) < 0 ? -(x) : (x))
25 
26 /* Compute M modulo N.  Requires N > 0; guarantees 0 <= imod(M,N) < N, */
27 /* regardless of the whims of the % operator for negative operands. */
28 int imod(int m, int n);
29 
30 /* Compute the GCD of two integers. */
31 int igcd(int x, int y);
32 
33 /*
34  * Given A, B, and M, compute X such that A*X = B mod M, 0 < X < M.
35  * Requires: M > 0, 0 < A < M, 0 < B < M, gcd(A, M) | gcd(A, B).
36  */
37 int idivmod(int a, int b, int m);
38 
39 /*
40  * Compute floor(log2(N)).  Requires N > 0.
41  */
42 int ilog2(int n);
43 
44 /* Test whether an integral value fits in a given number of bits. */
45 /* This works for all integral types. */
46 #define fits_in_bits(i, n)\
47   (sizeof(i) <= sizeof(int) ? fits_in_ubits((i) + (1 << ((n) - 1)), (n) + 1) :\
48    fits_in_ubits((i) + (1L << ((n) - 1)), (n) + 1))
49 #define fits_in_ubits(i, n) (((i) >> (n)) == 0)
50 
51 /*
52  * There are some floating point operations that can be implemented
53  * very efficiently on machines that have no floating point hardware,
54  * assuming IEEE representation and no range overflows.
55  * We define straightforward versions of them here, and alternate versions
56  * for no-floating-point machines in gxfarith.h.
57  */
58 /* Test floating point values against constants. */
59 #define is_fzero(f) ((f) == 0.0)
60 #define is_fzero2(f1,f2) ((f1) == 0.0 && (f2) == 0.0)
61 #define is_fneg(f) ((f) < 0.0)
62 #define is_fge1(f) ((f) >= 1.0)
63 /* Test whether a floating point value fits in a given number of bits. */
64 #define f_fits_in_bits(f, n)\
65   ((f) >= -2.0 * (1L << ((n) - 2)) && (f) < 2.0 * (1L << ((n) - 2)))
66 #define f_fits_in_ubits(f, n)\
67   ((f) >= 0 && (f) < 4.0 * (1L << ((n) - 2)))
68 
69 /*
70  * Define a macro for computing log2(n), where n=1,2,4,...,128.
71  * Because some compilers limit the total size of a statement,
72  * this macro must only mention n once.  The macro should really
73  * only be used with compile-time constant arguments, but it will work
74  * even if n is an expression computed at run-time.
75  */
76 #define small_exact_log2(n)\
77  ((uint)(05637042010L >> ((((n) % 11) - 1) * 3)) & 7)
78 
79 /*
80  * The following doesn't give rise to a macro, but is used in several
81  * places in Ghostscript.  We observe that if M = 2^n-1 and V < M^2,
82  * then the quotient Q and remainder R can be computed as:
83  *              Q = V / M = (V + (V >> n) + 1) >> n;
84  *              R = V % M = (V + (V / M)) & M = V - (Q << n) + Q.
85  */
86 
87 #endif /* gxarith_INCLUDED */
88