]> git.saurik.com Git - apple/security.git/blame - Security/libsecurity_asn1/lib/prlong.h
Security-57031.30.12.tar.gz
[apple/security.git] / Security / libsecurity_asn1 / lib / prlong.h
CommitLineData
b1ab9ed8
A
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/*
3 * The contents of this file are subject to the Mozilla Public
4 * License Version 1.1 (the "License"); you may not use this file
5 * except in compliance with the License. You may obtain a copy of
6 * the License at http://www.mozilla.org/MPL/
7 *
8 * Software distributed under the License is distributed on an "AS
9 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
10 * implied. See the License for the specific language governing
11 * rights and limitations under the License.
12 *
13 * The Original Code is the Netscape Portable Runtime (NSPR).
14 *
15 * The Initial Developer of the Original Code is Netscape
16 * Communications Corporation. Portions created by Netscape are
17 * Copyright (C) 1998-2000 Netscape Communications Corporation. All
18 * Rights Reserved.
19 *
20 * Contributor(s):
21 *
22 * Alternatively, the contents of this file may be used under the
23 * terms of the GNU General Public License Version 2 or later (the
24 * "GPL"), in which case the provisions of the GPL are applicable
25 * instead of those above. If you wish to allow use of your
26 * version of this file only under the terms of the GPL and not to
27 * allow others to use your version of this file under the MPL,
28 * indicate your decision by deleting the provisions above and
29 * replace them with the notice and other provisions required by
30 * the GPL. If you do not delete the provisions above, a recipient
31 * may use your version of this file under either the MPL or the
32 * GPL.
33 */
34
35/*
36** File: prlong.h
37** Description: Portable access to 64 bit numerics
38**
39** Long-long (64-bit signed integer type) support. Some C compilers
40** don't support 64 bit integers yet, so we use these macros to
41** support both machines that do and don't.
42**/
43#ifndef prlong_h___
44#define prlong_h___
45
46#include <prtypes.h>
47
48PR_BEGIN_EXTERN_C
49
50/***********************************************************************
51** DEFINES: LL_MaxInt
52** LL_MinInt
53** LL_Zero
54** DESCRIPTION:
55** Various interesting constants and static variable
56** initializer
57***********************************************************************/
58#if defined(HAVE_WATCOM_BUG_2)
59PRInt64 __pascal __loadds __export
60 LL_MaxInt(void);
61PRInt64 __pascal __loadds __export
62 LL_MinInt(void);
63PRInt64 __pascal __loadds __export
64 LL_Zero(void);
65#else
66NSPR_API(PRInt64) LL_MaxInt(void);
67NSPR_API(PRInt64) LL_MinInt(void);
68NSPR_API(PRInt64) LL_Zero(void);
69#endif
70
71#define LL_MAXINT LL_MaxInt()
72#define LL_MININT LL_MinInt()
73#define LL_ZERO LL_Zero()
74
75#if defined(HAVE_LONG_LONG)
76
77#if PR_BYTES_PER_LONG == 8
78#define LL_INIT(hi, lo) ((hi ## L << 32) + lo ## L)
79#elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
80#define LL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64)
81#else
82#define LL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL)
83#endif
84
85/***********************************************************************
86** MACROS: LL_*
87** DESCRIPTION:
88** The following macros define portable access to the 64 bit
89** math facilities.
90**
91***********************************************************************/
92
93/***********************************************************************
94** MACROS: LL_<relational operators>
95**
96** LL_IS_ZERO Test for zero
97** LL_EQ Test for equality
98** LL_NE Test for inequality
99** LL_GE_ZERO Test for zero or positive
100** LL_CMP Compare two values
101***********************************************************************/
102#define LL_IS_ZERO(a) ((a) == 0)
103#define LL_EQ(a, b) ((a) == (b))
104#define LL_NE(a, b) ((a) != (b))
105#define LL_GE_ZERO(a) ((a) >= 0)
106#define LL_CMP(a, op, b) ((PRInt64)(a) op (PRInt64)(b))
107#define LL_UCMP(a, op, b) ((PRUint64)(a) op (PRUint64)(b))
108
109/***********************************************************************
110** MACROS: LL_<logical operators>
111**
112** LL_AND Logical and
113** LL_OR Logical or
114** LL_XOR Logical exclusion
115** LL_OR2 A disgusting deviation
116** LL_NOT Negation (one's complement)
117***********************************************************************/
118#define LL_AND(r, a, b) ((r) = (a) & (b))
119#define LL_OR(r, a, b) ((r) = (a) | (b))
120#define LL_XOR(r, a, b) ((r) = (a) ^ (b))
121#define LL_OR2(r, a) ((r) = (r) | (a))
122#define LL_NOT(r, a) ((r) = ~(a))
123
124/***********************************************************************
125** MACROS: LL_<mathematical operators>
126**
127** LL_NEG Negation (two's complement)
128** LL_ADD Summation (two's complement)
129** LL_SUB Difference (two's complement)
130***********************************************************************/
131#define LL_NEG(r, a) ((r) = -(a))
132#define LL_ADD(r, a, b) ((r) = (a) + (b))
133#define LL_SUB(r, a, b) ((r) = (a) - (b))
134
135/***********************************************************************
136** MACROS: LL_<mathematical operators>
137**
138** LL_MUL Product (two's complement)
139** LL_DIV Quotient (two's complement)
140** LL_MOD Modulus (two's complement)
141***********************************************************************/
142#define LL_MUL(r, a, b) ((r) = (a) * (b))
143#define LL_DIV(r, a, b) ((r) = (a) / (b))
144#define LL_MOD(r, a, b) ((r) = (a) % (b))
145
146/***********************************************************************
147** MACROS: LL_<shifting operators>
148**
149** LL_SHL Shift left [0..64] bits
150** LL_SHR Shift right [0..64] bits with sign extension
151** LL_USHR Unsigned shift right [0..64] bits
152** LL_ISHL Signed shift left [0..64] bits
153***********************************************************************/
154#define LL_SHL(r, a, b) ((r) = (PRInt64)(a) << (b))
155#define LL_SHR(r, a, b) ((r) = (PRInt64)(a) >> (b))
156#define LL_USHR(r, a, b) ((r) = (PRUint64)(a) >> (b))
157#define LL_ISHL(r, a, b) ((r) = (PRInt64)(a) << (b))
158
159/***********************************************************************
160** MACROS: LL_<conversion operators>
161**
162** LL_L2I Convert to signed 32 bit
163** LL_L2UI Convert to unsigned 32 bit
164** LL_L2F Convert to floating point
165** LL_L2D Convert to floating point
166** LL_I2L Convert signed to 64 bit
167** LL_UI2L Convert unsigned to 64 bit
168** LL_F2L Convert float to 64 bit
169** LL_D2L Convert float to 64 bit
170***********************************************************************/
171#define LL_L2I(i, l) ((i) = (PRInt32)(l))
172#define LL_L2UI(ui, l) ((ui) = (PRUint32)(l))
173#define LL_L2F(f, l) ((f) = (PRFloat64)(l))
174#define LL_L2D(d, l) ((d) = (PRFloat64)(l))
175
176#define LL_I2L(l, i) ((l) = (PRInt64)(i))
177#define LL_UI2L(l, ui) ((l) = (PRInt64)(ui))
178#define LL_F2L(l, f) ((l) = (PRInt64)(f))
179#define LL_D2L(l, d) ((l) = (PRInt64)(d))
180
181/***********************************************************************
182** MACROS: LL_UDIVMOD
183** DESCRIPTION:
184** Produce both a quotient and a remainder given an unsigned
185** INPUTS: PRUint64 a: The dividend of the operation
186** PRUint64 b: The quotient of the operation
187** OUTPUTS: PRUint64 *qp: pointer to quotient
188** PRUint64 *rp: pointer to remainder
189***********************************************************************/
190#define LL_UDIVMOD(qp, rp, a, b) \
191 (*(qp) = ((PRUint64)(a) / (b)), \
192 *(rp) = ((PRUint64)(a) % (b)))
193
194#else /* !HAVE_LONG_LONG */
195
196#ifdef IS_LITTLE_ENDIAN
197#define LL_INIT(hi, lo) {PR_INT32(lo), PR_INT32(hi)}
198#else
199#define LL_INIT(hi, lo) {PR_INT32(hi), PR_INT32(lo)}
200#endif
201
202#define LL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0))
203#define LL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo))
204#define LL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo))
205#define LL_GE_ZERO(a) (((a).hi >> 31) == 0)
206
207#define LL_CMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
208 ((PRInt32)(a).hi op (PRInt32)(b).hi))
209#define LL_UCMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \
210 ((a).hi op (b).hi))
211
212#define LL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \
213 (r).hi = (a).hi & (b).hi)
214#define LL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \
215 (r).hi = (a).hi | (b).hi)
216#define LL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \
217 (r).hi = (a).hi ^ (b).hi)
218#define LL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \
219 (r).hi = (r).hi | (a).hi)
220#define LL_NOT(r, a) ((r).lo = ~(a).lo, \
221 (r).hi = ~(a).hi)
222
223#define LL_NEG(r, a) ((r).lo = -(PRInt32)(a).lo, \
224 (r).hi = -(PRInt32)(a).hi - ((r).lo != 0))
225#define LL_ADD(r, a, b) { \
226 PRInt64 _a, _b; \
227 _a = a; _b = b; \
228 (r).lo = _a.lo + _b.lo; \
229 (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
230}
231
232#define LL_SUB(r, a, b) { \
233 PRInt64 _a, _b; \
234 _a = a; _b = b; \
235 (r).lo = _a.lo - _b.lo; \
236 (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
237}
238
239#define LL_MUL(r, a, b) { \
240 PRInt64 _a, _b; \
241 _a = a; _b = b; \
242 LL_MUL32(r, _a.lo, _b.lo); \
243 (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
244}
245
246#define _lo16(a) ((a) & PR_BITMASK(16))
247#define _hi16(a) ((a) >> 16)
248
249#define LL_MUL32(r, a, b) { \
250 PRUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
251 _a1 = _hi16(a), _a0 = _lo16(a); \
252 _b1 = _hi16(b), _b0 = _lo16(b); \
253 _y0 = _a0 * _b0; \
254 _y1 = _a0 * _b1; \
255 _y2 = _a1 * _b0; \
256 _y3 = _a1 * _b1; \
257 _y1 += _hi16(_y0); /* can't carry */ \
258 _y1 += _y2; /* might carry */ \
259 if (_y1 < _y2) \
260 _y3 += (PRUint32)(PR_BIT(16)); /* propagate */ \
261 (r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \
262 (r).hi = _y3 + _hi16(_y1); \
263}
264
265#define LL_UDIVMOD(qp, rp, a, b) ll_udivmod(qp, rp, a, b)
266
267NSPR_API(void) ll_udivmod(PRUint64 *qp, PRUint64 *rp, PRUint64 a, PRUint64 b);
268
269#define LL_DIV(r, a, b) { \
270 PRInt64 _a, _b; \
271 PRUint32 _negative = (PRInt32)(a).hi < 0; \
272 if (_negative) { \
273 LL_NEG(_a, a); \
274 } else { \
275 _a = a; \
276 } \
277 if ((PRInt32)(b).hi < 0) { \
278 _negative ^= 1; \
279 LL_NEG(_b, b); \
280 } else { \
281 _b = b; \
282 } \
283 LL_UDIVMOD(&(r), 0, _a, _b); \
284 if (_negative) \
285 LL_NEG(r, r); \
286}
287
288#define LL_MOD(r, a, b) { \
289 PRInt64 _a, _b; \
290 PRUint32 _negative = (PRInt32)(a).hi < 0; \
291 if (_negative) { \
292 LL_NEG(_a, a); \
293 } else { \
294 _a = a; \
295 } \
296 if ((PRInt32)(b).hi < 0) { \
297 LL_NEG(_b, b); \
298 } else { \
299 _b = b; \
300 } \
301 LL_UDIVMOD(0, &(r), _a, _b); \
302 if (_negative) \
303 LL_NEG(r, r); \
304}
305
306#define LL_SHL(r, a, b) { \
307 if (b) { \
308 PRInt64 _a; \
309 _a = a; \
310 if ((b) < 32) { \
311 (r).lo = _a.lo << ((b) & 31); \
312 (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
313 } else { \
314 (r).lo = 0; \
315 (r).hi = _a.lo << ((b) & 31); \
316 } \
317 } else { \
318 (r) = (a); \
319 } \
320}
321
322/* a is an PRInt32, b is PRInt32, r is PRInt64 */
323#define LL_ISHL(r, a, b) { \
324 if (b) { \
325 PRInt64 _a; \
326 _a.lo = (a); \
327 _a.hi = 0; \
328 if ((b) < 32) { \
329 (r).lo = (a) << ((b) & 31); \
330 (r).hi = ((a) >> (32 - (b))); \
331 } else { \
332 (r).lo = 0; \
333 (r).hi = (a) << ((b) & 31); \
334 } \
335 } else { \
336 (r).lo = (a); \
337 (r).hi = 0; \
338 } \
339}
340
341#define LL_SHR(r, a, b) { \
342 if (b) { \
343 PRInt64 _a; \
344 _a = a; \
345 if ((b) < 32) { \
346 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
347 (r).hi = (PRInt32)_a.hi >> ((b) & 31); \
348 } else { \
349 (r).lo = (PRInt32)_a.hi >> ((b) & 31); \
350 (r).hi = (PRInt32)_a.hi >> 31; \
351 } \
352 } else { \
353 (r) = (a); \
354 } \
355}
356
357#define LL_USHR(r, a, b) { \
358 if (b) { \
359 PRInt64 _a; \
360 _a = a; \
361 if ((b) < 32) { \
362 (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
363 (r).hi = _a.hi >> ((b) & 31); \
364 } else { \
365 (r).lo = _a.hi >> ((b) & 31); \
366 (r).hi = 0; \
367 } \
368 } else { \
369 (r) = (a); \
370 } \
371}
372
373#define LL_L2I(i, l) ((i) = (l).lo)
374#define LL_L2UI(ui, l) ((ui) = (l).lo)
375#define LL_L2F(f, l) { double _d; LL_L2D(_d, l); (f) = (PRFloat64)_d; }
376
377#define LL_L2D(d, l) { \
378 int _negative; \
379 PRInt64 _absval; \
380 \
381 _negative = (l).hi >> 31; \
382 if (_negative) { \
383 LL_NEG(_absval, l); \
384 } else { \
385 _absval = l; \
386 } \
387 (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
388 if (_negative) \
389 (d) = -(d); \
390}
391
392#define LL_I2L(l, i) { PRInt32 _i = ((PRInt32)(i)) >> 31; (l).lo = (i); (l).hi = _i; }
393#define LL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0)
394#define LL_F2L(l, f) { double _d = (double)f; LL_D2L(l, _d); }
395
396#define LL_D2L(l, d) { \
397 int _negative; \
398 double _absval, _d_hi; \
399 PRInt64 _lo_d; \
400 \
401 _negative = ((d) < 0); \
402 _absval = _negative ? -(d) : (d); \
403 \
404 (l).hi = _absval / 4.294967296e9; \
405 (l).lo = 0; \
406 LL_L2D(_d_hi, l); \
407 _absval -= _d_hi; \
408 _lo_d.hi = 0; \
409 if (_absval < 0) { \
410 _lo_d.lo = -_absval; \
411 LL_SUB(l, l, _lo_d); \
412 } else { \
413 _lo_d.lo = _absval; \
414 LL_ADD(l, l, _lo_d); \
415 } \
416 \
417 if (_negative) \
418 LL_NEG(l, l); \
419}
420
421#endif /* !HAVE_LONG_LONG */
422
423PR_END_EXTERN_C
424
425#endif /* prlong_h___ */