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