1 /**
2  ** arith.h ---- some common integer arithmetic macros/inline functions
3  **
4  ** Copyright (c) 1995 Csaba Biegl, 820 Stirrup Dr, Nashville, TN 37221
5  ** [e-mail: csaba@vuse.vanderbilt.edu]
6  **
7  ** This file is part of the GRX graphics library.
8  **
9  ** The GRX graphics library is free software; you can redistribute it
10  ** and/or modify it under some conditions; see the "copying.grx" file
11  ** for details.
12  **
13  ** This library is distributed in the hope that it will be useful,
14  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
15  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  **
17  ** Intel CPU specific support is provided for the Turbo C and GNU C. May
18  ** work with other compilers and CPU-s, but is not optimized for them.
19  **
20  **/
21 
22 #ifndef __ARITH_H_INCLUDED__
23 #define __ARITH_H_INCLUDED__
24 
25 #if defined(__GNUC__) && !defined(__clang__)
26 #include "gcc/arith.h"
27 #elif defined(__TURBOC__)
28 #include "bcc/arith.h"
29 #endif
30 
31 #ifdef _MSC_VER
32 #define __emit__(x) __asm{ __emit (x) }
33 #endif
34 
35 /*
36  * old standbys
37  */
38 #ifndef min
39 #define min(x,y)    (((x) < (y)) ?  (x) : (y))
40 #endif
41 #ifndef max
42 #define max(x,y)    (((x) > (y)) ?  (x) : (y))
43 #endif
44 #ifndef __ABS
45 #define __ABS(x)    (((x) < (0)) ? -(x) : (x))
46 #endif
47 #ifndef abs
48 #define abs(x)      __ABS(x)
49 #endif
50 #ifndef scale
51 #define scale(x,n,d)    (((x) * (n)) / (d))
52 #endif
53 #ifndef imin
54 #define imin(x,y)   min((int)(x),(int)(y))
55 #endif
56 #ifndef imax
57 #define imax(x,y)   max((int)(x),(int)(y))
58 #endif
59 #ifndef iabs
60 #define iabs(x)     abs((int)(x))
61 #endif
62 #ifndef umin
63 #define umin(x,y)   min((unsigned int)(x),(unsigned int)(y))
64 #endif
65 #ifndef umax
66 #define umax(x,y)   max((unsigned int)(x),(unsigned int)(y))
67 #endif
68 #ifndef lmin
69 #define lmin(x,y)   min((long)(x),(long)(y))
70 #endif
71 #ifndef lmax
72 #define lmax(x,y)   max((long)(x),(long)(y))
73 #endif
74 #ifndef labs
75 #define labs(x)     __ABS((long)(x))
76 #endif
77 #ifndef ulmin
78 #define ulmin(x,y)  min((unsigned long)(x),(unsigned long)(y))
79 #endif
80 #ifndef ulmax
81 #define ulmax(x,y)  max((unsigned long)(x),(unsigned long)(y))
82 #endif
83 
84 /*
85  * swap and sort stuff
86  */
87 #define iswap(x,y) {                                            \
88 	int _swap_tmpval_ = (x);                                \
89 	(x) = (y);                                              \
90 	(y) = _swap_tmpval_;                                    \
91 }
92 
93 #define lswap(x,y) {                                            \
94 	long _swap_tmpval_ = (x);                               \
95 	(x) = (y);                                              \
96 	(y) = _swap_tmpval_;                                    \
97 }
98 
99 #define isort(x,y) {                                            \
100 	if((int)(x) > (int)(y)) iswap(x,y)                      \
101 }
102 
103 #define usort(x,y) {                                            \
104 	if((unsigned int)(x) > (unsigned int)(y)) iswap(x,y)    \
105 }
106 
107 #define lsort(x,y) {                                            \
108 	if((long)(x) > (long)(y)) lswap(x,y)                    \
109 }
110 
111 #define ulsort(x,y) {                                           \
112 	if((unsigned long)(x) > (unsigned long)(y)) lswap(x,y)  \
113 }
114 
115 
116 /*
117  * couple of 'sizeof'-like useful macros
118  */
119 #ifndef bsizeof
120 #define bsizeof(s)  (sizeof(s) / sizeof(char))
121 #endif
122 #ifndef wsizeof
123 #define wsizeof(s)  (sizeof(s) / sizeof(short))
124 #endif
125 #ifndef lsizeof
126 #define lsizeof(s)  (sizeof(s) / sizeof(long))
127 #endif
128 #ifndef bitsof
129 #define bitsof(s)   (sizeof(s) * 8)
130 #endif
131 #ifndef bytesof
132 #define bytesof(s)  ((sizeof(s) + sizeof(char) - 1) / sizeof(char))
133 #endif
134 #ifndef wordsof
135 #define wordsof(s)  ((sizeof(s) + sizeof(short) - 1) / sizeof(short))
136 #endif
137 #ifndef longsof
138 #define longsof(s)  ((sizeof(s) + sizeof(long) - 1) / sizeof(long))
139 #endif
140 #ifndef itemsof
141 #define itemsof(arr)    (sizeof(arr) / sizeof((arr)[0]))
142 #endif
143 #ifndef offsetof
144 #define offsetof(structype,field) (int)(                        \
145 	(char *)(&((structype *)(0))->field) -                  \
146 	(char *)(0)                                             \
147 )
148 #endif
149 
150 /*
151  * [i|u]mul32(x,y)
152  * multiply two int-s for a long result
153  */
154 #ifndef imul32
155 #define imul32(X,Y) ((long)(X) * (long)(Y))
156 #endif
157 #ifndef umul32
158 #define umul32(X,Y) ((unsigned long)(X) * (unsigned long)(Y))
159 #endif
160 
161 
162 /*
163  * umuladd32(x,y,z)
164  * multiply two unsigned-s for a long result and add an unsigned
165  */
166 #ifndef umuladd32
167 #define umuladd32(X,Y,Z) (umul32((X),(Y))+(Z))
168 #endif
169 
170 
171 /*
172  * [i|u]scale(X,N,D)
173  * scale an integer with long intermediate result but without using long
174  * arithmetic all the way
175  */
176 #ifndef iscale
177 #define iscale(X,N,D)   (int)(imul32(X,N) / (long)(D))
178 #endif
179 #ifndef uscale
180 #define uscale(X,N,D)   (unsigned int)(umul32(X,N) / (unsigned long)(D))
181 #endif
182 #ifndef irscale
183 #define irscale(X,N,D)  ((                                              \
184 	iscale(((int)(X) << 1),N,D) +                                   \
185 	(((int)(X) ^ (int)(N) ^ (int)(D)) >> (bitsof(int) - 1)) +       \
186 	1                                                               \
187 ) >> 1)
188 #endif
189 #ifndef urscale
190 #define urscale(X,N,D)  ((uscale(((int)(X) << 1),N,D) + 1) >> 1)
191 #endif
192 
193 /*
194  * replicate_<FROM>2<TO>(byte_or_word_value)
195  * copy the lower byte(s) of a byte or word into the upper byte(s)
196  */
197 #define __INLINE_REPLICATE__(V,TFROM,TTO) (                             \
198 	((unsigned TTO)(unsigned TFROM)(V)) |                           \
199 	((unsigned TTO)(unsigned TFROM)(V) << (sizeof(TFROM) * 8))      \
200 )
201 
202 #ifndef replicate_b2b
203 #define replicate_b2b(BYTE) (BYTE)
204 #endif
205 #ifndef replicate_b2w
206 #define replicate_b2w(BYTE) __INLINE_REPLICATE__(BYTE,GR_int8,GR_int16)
207 #endif
208 #ifndef replicate_b2l
209 #define replicate_b2l(BYTE) replicate_w2l(replicate_b2w(BYTE))
210 #endif
211 #ifndef replicate_b2h
212 #define replicate_b2h(BYTE) replicate_l2h(replicate_w2l(replicate_b2w(BYTE)))
213 #endif
214 
215 #ifndef replicate_w2w
216 #define replicate_w2w(WORD) (WORD)
217 #endif
218 #ifndef replicate_w2l
219 #define replicate_w2l(WORD) __INLINE_REPLICATE__(WORD,GR_int16,GR_int32)
220 #endif
221 #ifndef replicate_w2h
222 #define replicate_w2h(WORD) replicate_l2h(replicate_w2l(WORD))
223 #endif
224 
225 #ifndef replicate_l2l
226 #define replicate_l2l(LONG) (LONG)
227 #endif
228 #ifndef replicate_l2h
229 #define replicate_l2h(LONG) __INLINE_REPLICATE__(LONG,GR_int32,GR_int64)
230 #endif
231 
232 #ifndef replicate_h2h
233 #define replicate_h2h(LLONG) (LLONG)
234 #endif
235 
236 #endif  /* whole file */
237 
238