1 /* cairo - a vector graphics library with display and print output
3 * Copyright © 2004 Keith Packard
5 * This library is free software; you can redistribute it and/or
6 * modify it either under the terms of the GNU Lesser General Public
7 * License version 2.1 as published by the Free Software Foundation
8 * (the "LGPL") or, at your option, under the terms of the Mozilla
9 * Public License Version 1.1 (the "MPL"). If you do not alter this
10 * notice, a recipient may use your version of this file under either
11 * the MPL or the LGPL.
13 * You should have received a copy of the LGPL along with this library
14 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 * You should have received a copy of the MPL along with this library
17 * in the file COPYING-MPL-1.1
19 * The contents of this file are subject to the Mozilla Public License
20 * Version 1.1 (the "License"); you may not use this file except in
21 * compliance with the License. You may obtain a copy of the License at
22 * http://www.mozilla.org/MPL/
24 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26 * the specific language governing rights and limitations.
28 * The Original Code is the cairo graphics library.
30 * The Initial Developer of the Original Code is Keith Packard
33 * Keith R. Packard <keithp@keithp.com>
37 #ifndef CAIRO_WIDEINT_H
38 #define CAIRO_WIDEINT_H
40 #include "cairo-wideint-type-private.h"
42 #include "cairo-compiler-private.h"
45 * 64-bit datatypes. Two separate implementations, one using
46 * built-in 64-bit signed/unsigned types another implemented
47 * as a pair of 32-bit ints
50 #define I cairo_private cairo_const
54 cairo_uint64_t I
_cairo_uint32_to_uint64 (uint32_t i
);
55 #define _cairo_uint64_to_uint32(a) ((a).lo)
56 cairo_uint64_t I
_cairo_uint64_add (cairo_uint64_t a
, cairo_uint64_t b
);
57 cairo_uint64_t I
_cairo_uint64_sub (cairo_uint64_t a
, cairo_uint64_t b
);
58 cairo_uint64_t I
_cairo_uint64_mul (cairo_uint64_t a
, cairo_uint64_t b
);
59 cairo_uint64_t I
_cairo_uint32x32_64_mul (uint32_t a
, uint32_t b
);
60 cairo_uint64_t I
_cairo_uint64_lsl (cairo_uint64_t a
, int shift
);
61 cairo_uint64_t I
_cairo_uint64_rsl (cairo_uint64_t a
, int shift
);
62 cairo_uint64_t I
_cairo_uint64_rsa (cairo_uint64_t a
, int shift
);
63 int I
_cairo_uint64_lt (cairo_uint64_t a
, cairo_uint64_t b
);
64 int I
_cairo_uint64_cmp (cairo_uint64_t a
, cairo_uint64_t b
);
65 int I
_cairo_uint64_eq (cairo_uint64_t a
, cairo_uint64_t b
);
66 cairo_uint64_t I
_cairo_uint64_negate (cairo_uint64_t a
);
67 #define _cairo_uint64_is_zero(a) ((a).hi == 0 && (a).lo == 0)
68 #define _cairo_uint64_negative(a) (((int32_t) ((a).hi)) < 0)
69 cairo_uint64_t I
_cairo_uint64_not (cairo_uint64_t a
);
71 #define _cairo_uint64_to_int64(i) (i)
72 #define _cairo_int64_to_uint64(i) (i)
74 cairo_int64_t I
_cairo_int32_to_int64(int32_t i
);
75 #define _cairo_int64_to_int32(a) ((int32_t) _cairo_uint64_to_uint32(a))
76 #define _cairo_int64_add(a,b) _cairo_uint64_add (a,b)
77 #define _cairo_int64_sub(a,b) _cairo_uint64_sub (a,b)
78 #define _cairo_int64_mul(a,b) _cairo_uint64_mul (a,b)
79 cairo_int64_t I
_cairo_int32x32_64_mul (int32_t a
, int32_t b
);
80 int I
_cairo_int64_lt (cairo_int64_t a
, cairo_int64_t b
);
81 int I
_cairo_int64_cmp (cairo_int64_t a
, cairo_int64_t b
);
82 #define _cairo_int64_is_zero(a) _cairo_uint64_is_zero (a)
83 #define _cairo_int64_eq(a,b) _cairo_uint64_eq (a,b)
84 #define _cairo_int64_lsl(a,b) _cairo_uint64_lsl (a,b)
85 #define _cairo_int64_rsl(a,b) _cairo_uint64_rsl (a,b)
86 #define _cairo_int64_rsa(a,b) _cairo_uint64_rsa (a,b)
87 #define _cairo_int64_negate(a) _cairo_uint64_negate(a)
88 #define _cairo_int64_negative(a) (((int32_t) ((a).hi)) < 0)
89 #define _cairo_int64_not(a) _cairo_uint64_not(a)
93 #define _cairo_uint32_to_uint64(i) ((uint64_t) (i))
94 #define _cairo_uint64_to_uint32(i) ((uint32_t) (i))
95 #define _cairo_uint64_add(a,b) ((a) + (b))
96 #define _cairo_uint64_sub(a,b) ((a) - (b))
97 #define _cairo_uint64_mul(a,b) ((a) * (b))
98 #define _cairo_uint32x32_64_mul(a,b) ((uint64_t) (a) * (b))
99 #define _cairo_uint64_lsl(a,b) ((a) << (b))
100 #define _cairo_uint64_rsl(a,b) ((uint64_t) (a) >> (b))
101 #define _cairo_uint64_rsa(a,b) ((uint64_t) ((int64_t) (a) >> (b)))
102 #define _cairo_uint64_lt(a,b) ((a) < (b))
103 #define _cairo_uint64_cmp(a,b) ((a) == (b) ? 0 : (a) < (b) ? -1 : 1)
104 #define _cairo_uint64_is_zero(a) ((a) == 0)
105 #define _cairo_uint64_eq(a,b) ((a) == (b))
106 #define _cairo_uint64_negate(a) ((uint64_t) -((int64_t) (a)))
107 #define _cairo_uint64_negative(a) ((int64_t) (a) < 0)
108 #define _cairo_uint64_not(a) (~(a))
110 #define _cairo_uint64_to_int64(i) ((int64_t) (i))
111 #define _cairo_int64_to_uint64(i) ((uint64_t) (i))
113 #define _cairo_int32_to_int64(i) ((int64_t) (i))
114 #define _cairo_int64_to_int32(i) ((int32_t) (i))
115 #define _cairo_int64_add(a,b) ((a) + (b))
116 #define _cairo_int64_sub(a,b) ((a) - (b))
117 #define _cairo_int64_mul(a,b) ((a) * (b))
118 #define _cairo_int32x32_64_mul(a,b) ((int64_t) (a) * (b))
119 #define _cairo_int64_lt(a,b) ((a) < (b))
120 #define _cairo_int64_cmp(a,b) ((a) == (b) ? 0 : (a) < (b) ? -1 : 1)
121 #define _cairo_int64_is_zero(a) ((a) == 0)
122 #define _cairo_int64_eq(a,b) ((a) == (b))
123 #define _cairo_int64_lsl(a,b) ((a) << (b))
124 #define _cairo_int64_rsl(a,b) ((int64_t) ((uint64_t) (a) >> (b)))
125 #define _cairo_int64_rsa(a,b) ((int64_t) (a) >> (b))
126 #define _cairo_int64_negate(a) (-(a))
127 #define _cairo_int64_negative(a) ((a) < 0)
128 #define _cairo_int64_not(a) (~(a))
133 * 64-bit comparisions derived from lt or eq
135 #define _cairo_uint64_le(a,b) (!_cairo_uint64_gt(a,b))
136 #define _cairo_uint64_ne(a,b) (!_cairo_uint64_eq(a,b))
137 #define _cairo_uint64_ge(a,b) (!_cairo_uint64_lt(a,b))
138 #define _cairo_uint64_gt(a,b) _cairo_uint64_lt(b,a)
140 #define _cairo_int64_le(a,b) (!_cairo_int64_gt(a,b))
141 #define _cairo_int64_ne(a,b) (!_cairo_int64_eq(a,b))
142 #define _cairo_int64_ge(a,b) (!_cairo_int64_lt(a,b))
143 #define _cairo_int64_gt(a,b) _cairo_int64_lt(b,a)
146 * As the C implementation always computes both, create
147 * a function which returns both for the 'native' type as well
151 _cairo_uint64_divrem (cairo_uint64_t num
, cairo_uint64_t den
);
154 _cairo_int64_divrem (cairo_int64_t num
, cairo_int64_t den
);
157 * 128-bit datatypes. Again, provide two implementations in
158 * case the machine has a native 128-bit datatype. GCC supports int128_t
164 cairo_uint128_t I
_cairo_uint32_to_uint128 (uint32_t i
);
165 cairo_uint128_t I
_cairo_uint64_to_uint128 (cairo_uint64_t i
);
166 #define _cairo_uint128_to_uint64(a) ((a).lo)
167 #define _cairo_uint128_to_uint32(a) _cairo_uint64_to_uint32(_cairo_uint128_to_uint64(a))
168 cairo_uint128_t I
_cairo_uint128_add (cairo_uint128_t a
, cairo_uint128_t b
);
169 cairo_uint128_t I
_cairo_uint128_sub (cairo_uint128_t a
, cairo_uint128_t b
);
170 cairo_uint128_t I
_cairo_uint128_mul (cairo_uint128_t a
, cairo_uint128_t b
);
171 cairo_uint128_t I
_cairo_uint64x64_128_mul (cairo_uint64_t a
, cairo_uint64_t b
);
172 cairo_uint128_t I
_cairo_uint128_lsl (cairo_uint128_t a
, int shift
);
173 cairo_uint128_t I
_cairo_uint128_rsl (cairo_uint128_t a
, int shift
);
174 cairo_uint128_t I
_cairo_uint128_rsa (cairo_uint128_t a
, int shift
);
175 int I
_cairo_uint128_lt (cairo_uint128_t a
, cairo_uint128_t b
);
176 int I
_cairo_uint128_cmp (cairo_uint128_t a
, cairo_uint128_t b
);
177 int I
_cairo_uint128_eq (cairo_uint128_t a
, cairo_uint128_t b
);
178 #define _cairo_uint128_is_zero(a) (_cairo_uint64_is_zero ((a).hi) && _cairo_uint64_is_zero ((a).lo))
179 cairo_uint128_t I
_cairo_uint128_negate (cairo_uint128_t a
);
180 #define _cairo_uint128_negative(a) (_cairo_uint64_negative(a.hi))
181 cairo_uint128_t I
_cairo_uint128_not (cairo_uint128_t a
);
183 #define _cairo_uint128_to_int128(i) (i)
184 #define _cairo_int128_to_uint128(i) (i)
186 cairo_int128_t I
_cairo_int32_to_int128 (int32_t i
);
187 cairo_int128_t I
_cairo_int64_to_int128 (cairo_int64_t i
);
188 #define _cairo_int128_to_int64(a) ((cairo_int64_t) (a).lo)
189 #define _cairo_int128_to_int32(a) _cairo_int64_to_int32(_cairo_int128_to_int64(a))
190 #define _cairo_int128_add(a,b) _cairo_uint128_add(a,b)
191 #define _cairo_int128_sub(a,b) _cairo_uint128_sub(a,b)
192 #define _cairo_int128_mul(a,b) _cairo_uint128_mul(a,b)
193 cairo_int128_t I
_cairo_int64x64_128_mul (cairo_int64_t a
, cairo_int64_t b
);
194 #define _cairo_int64x32_128_mul(a, b) _cairo_int64x64_128_mul(a, _cairo_int32_to_int64(b))
195 #define _cairo_int128_lsl(a,b) _cairo_uint128_lsl(a,b)
196 #define _cairo_int128_rsl(a,b) _cairo_uint128_rsl(a,b)
197 #define _cairo_int128_rsa(a,b) _cairo_uint128_rsa(a,b)
198 int I
_cairo_int128_lt (cairo_int128_t a
, cairo_int128_t b
);
199 int I
_cairo_int128_cmp (cairo_int128_t a
, cairo_int128_t b
);
200 #define _cairo_int128_is_zero(a) _cairo_uint128_is_zero (a)
201 #define _cairo_int128_eq(a,b) _cairo_uint128_eq (a,b)
202 #define _cairo_int128_negate(a) _cairo_uint128_negate(a)
203 #define _cairo_int128_negative(a) (_cairo_uint128_negative(a))
204 #define _cairo_int128_not(a) _cairo_uint128_not(a)
206 #else /* !HAVE_UINT128_T */
208 #define _cairo_uint32_to_uint128(i) ((uint128_t) (i))
209 #define _cairo_uint64_to_uint128(i) ((uint128_t) (i))
210 #define _cairo_uint128_to_uint64(i) ((uint64_t) (i))
211 #define _cairo_uint128_to_uint32(i) ((uint32_t) (i))
212 #define _cairo_uint128_add(a,b) ((a) + (b))
213 #define _cairo_uint128_sub(a,b) ((a) - (b))
214 #define _cairo_uint128_mul(a,b) ((a) * (b))
215 #define _cairo_uint64x64_128_mul(a,b) ((uint128_t) (a) * (b))
216 #define _cairo_uint128_lsl(a,b) ((a) << (b))
217 #define _cairo_uint128_rsl(a,b) ((uint128_t) (a) >> (b))
218 #define _cairo_uint128_rsa(a,b) ((uint128_t) ((int128_t) (a) >> (b)))
219 #define _cairo_uint128_lt(a,b) ((a) < (b))
220 #define _cairo_uint128_cmp(a,b) ((a) == (b) ? 0 : (a) < (b) ? -1 : 1)
221 #define _cairo_uint128_is_zero(a) ((a) == 0)
222 #define _cairo_uint128_eq(a,b) ((a) == (b))
223 #define _cairo_uint128_negate(a) ((uint128_t) -((int128_t) (a)))
224 #define _cairo_uint128_negative(a) ((int128_t) (a) < 0)
225 #define _cairo_uint128_not(a) (~(a))
227 #define _cairo_uint128_to_int128(i) ((int128_t) (i))
228 #define _cairo_int128_to_uint128(i) ((uint128_t) (i))
230 #define _cairo_int32_to_int128(i) ((int128_t) (i))
231 #define _cairo_int64_to_int128(i) ((int128_t) (i))
232 #define _cairo_int128_to_int64(i) ((int64_t) (i))
233 #define _cairo_int128_to_int32(i) ((int32_t) (i))
234 #define _cairo_int128_add(a,b) ((a) + (b))
235 #define _cairo_int128_sub(a,b) ((a) - (b))
236 #define _cairo_int128_mul(a,b) ((a) * (b))
237 #define _cairo_int64x64_128_mul(a,b) ((int128_t) (a) * (b))
238 #define _cairo_int128_lt(a,b) ((a) < (b))
239 #define _cairo_int128_cmp(a,b) ((a) == (b) ? 0 : (a) < (b) ? -1 : 1)
240 #define _cairo_int128_is_zero(a) ((a) == 0)
241 #define _cairo_int128_eq(a,b) ((a) == (b))
242 #define _cairo_int128_lsl(a,b) ((a) << (b))
243 #define _cairo_int128_rsl(a,b) ((int128_t) ((uint128_t) (a) >> (b)))
244 #define _cairo_int128_rsa(a,b) ((int128_t) (a) >> (b))
245 #define _cairo_int128_negate(a) (-(a))
246 #define _cairo_int128_negative(a) ((a) < 0)
247 #define _cairo_int128_not(a) (~(a))
249 #endif /* HAVE_UINT128_T */
252 _cairo_uint128_divrem (cairo_uint128_t num
, cairo_uint128_t den
);
255 _cairo_int128_divrem (cairo_int128_t num
, cairo_int128_t den
);
258 _cairo_uint_96by64_32x64_divrem (cairo_uint128_t num
,
262 _cairo_int_96by64_32x64_divrem (cairo_int128_t num
,
265 #define _cairo_uint128_le(a,b) (!_cairo_uint128_gt(a,b))
266 #define _cairo_uint128_ne(a,b) (!_cairo_uint128_eq(a,b))
267 #define _cairo_uint128_ge(a,b) (!_cairo_uint128_lt(a,b))
268 #define _cairo_uint128_gt(a,b) _cairo_uint128_lt(b,a)
270 #define _cairo_int128_le(a,b) (!_cairo_int128_gt(a,b))
271 #define _cairo_int128_ne(a,b) (!_cairo_int128_eq(a,b))
272 #define _cairo_int128_ge(a,b) (!_cairo_int128_lt(a,b))
273 #define _cairo_int128_gt(a,b) _cairo_int128_lt(b,a)
277 #endif /* CAIRO_WIDEINT_H */