1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/include/prlong.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,403 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 +** File: prlong.h 1.11 +** Description: Portable access to 64 bit numerics 1.12 +** 1.13 +** Long-long (64-bit signed integer type) support. Some C compilers 1.14 +** don't support 64 bit integers yet, so we use these macros to 1.15 +** support both machines that do and don't. 1.16 +**/ 1.17 +#ifndef prlong_h___ 1.18 +#define prlong_h___ 1.19 + 1.20 +#include "prtypes.h" 1.21 + 1.22 +PR_BEGIN_EXTERN_C 1.23 + 1.24 +/*********************************************************************** 1.25 +** DEFINES: LL_MaxInt 1.26 +** LL_MinInt 1.27 +** LL_Zero 1.28 +** LL_MaxUint 1.29 +** DESCRIPTION: 1.30 +** Various interesting constants and static variable 1.31 +** initializer 1.32 +***********************************************************************/ 1.33 +NSPR_API(PRInt64) LL_MaxInt(void); 1.34 +NSPR_API(PRInt64) LL_MinInt(void); 1.35 +NSPR_API(PRInt64) LL_Zero(void); 1.36 +NSPR_API(PRUint64) LL_MaxUint(void); 1.37 + 1.38 +#if defined(HAVE_LONG_LONG) 1.39 + 1.40 +/* Keep this in sync with prtypes.h. */ 1.41 +#if PR_BYTES_PER_LONG == 8 && !defined(PR_ALTERNATE_INT64_TYPEDEF) 1.42 +#define LL_MAXINT 9223372036854775807L 1.43 +#define LL_MININT (-LL_MAXINT - 1L) 1.44 +#define LL_ZERO 0L 1.45 +#define LL_MAXUINT 18446744073709551615UL 1.46 +#define LL_INIT(hi, lo) ((hi ## L << 32) + lo ## L) 1.47 +#elif defined(WIN32) && !defined(__GNUC__) 1.48 +#define LL_MAXINT 9223372036854775807i64 1.49 +#define LL_MININT (-LL_MAXINT - 1i64) 1.50 +#define LL_ZERO 0i64 1.51 +#define LL_MAXUINT 18446744073709551615ui64 1.52 +#define LL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64) 1.53 +#else 1.54 +#define LL_MAXINT 9223372036854775807LL 1.55 +#define LL_MININT (-LL_MAXINT - 1LL) 1.56 +#define LL_ZERO 0LL 1.57 +#define LL_MAXUINT 18446744073709551615ULL 1.58 +#define LL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL) 1.59 +#endif 1.60 + 1.61 +/*********************************************************************** 1.62 +** MACROS: LL_* 1.63 +** DESCRIPTION: 1.64 +** The following macros define portable access to the 64 bit 1.65 +** math facilities. 1.66 +** 1.67 +***********************************************************************/ 1.68 + 1.69 +/*********************************************************************** 1.70 +** MACROS: LL_<relational operators> 1.71 +** 1.72 +** LL_IS_ZERO Test for zero 1.73 +** LL_EQ Test for equality 1.74 +** LL_NE Test for inequality 1.75 +** LL_GE_ZERO Test for zero or positive 1.76 +** LL_CMP Compare two values 1.77 +***********************************************************************/ 1.78 +#define LL_IS_ZERO(a) ((a) == 0) 1.79 +#define LL_EQ(a, b) ((a) == (b)) 1.80 +#define LL_NE(a, b) ((a) != (b)) 1.81 +#define LL_GE_ZERO(a) ((a) >= 0) 1.82 +#define LL_CMP(a, op, b) ((PRInt64)(a) op (PRInt64)(b)) 1.83 +#define LL_UCMP(a, op, b) ((PRUint64)(a) op (PRUint64)(b)) 1.84 + 1.85 +/*********************************************************************** 1.86 +** MACROS: LL_<logical operators> 1.87 +** 1.88 +** LL_AND Logical and 1.89 +** LL_OR Logical or 1.90 +** LL_XOR Logical exclusion 1.91 +** LL_OR2 A disgusting deviation 1.92 +** LL_NOT Negation (one's complement) 1.93 +***********************************************************************/ 1.94 +#define LL_AND(r, a, b) ((r) = (a) & (b)) 1.95 +#define LL_OR(r, a, b) ((r) = (a) | (b)) 1.96 +#define LL_XOR(r, a, b) ((r) = (a) ^ (b)) 1.97 +#define LL_OR2(r, a) ((r) = (r) | (a)) 1.98 +#define LL_NOT(r, a) ((r) = ~(a)) 1.99 + 1.100 +/*********************************************************************** 1.101 +** MACROS: LL_<mathematical operators> 1.102 +** 1.103 +** LL_NEG Negation (two's complement) 1.104 +** LL_ADD Summation (two's complement) 1.105 +** LL_SUB Difference (two's complement) 1.106 +***********************************************************************/ 1.107 +#define LL_NEG(r, a) ((r) = -(a)) 1.108 +#define LL_ADD(r, a, b) ((r) = (a) + (b)) 1.109 +#define LL_SUB(r, a, b) ((r) = (a) - (b)) 1.110 + 1.111 +/*********************************************************************** 1.112 +** MACROS: LL_<mathematical operators> 1.113 +** 1.114 +** LL_MUL Product (two's complement) 1.115 +** LL_DIV Quotient (two's complement) 1.116 +** LL_MOD Modulus (two's complement) 1.117 +***********************************************************************/ 1.118 +#define LL_MUL(r, a, b) ((r) = (a) * (b)) 1.119 +#define LL_DIV(r, a, b) ((r) = (a) / (b)) 1.120 +#define LL_MOD(r, a, b) ((r) = (a) % (b)) 1.121 + 1.122 +/*********************************************************************** 1.123 +** MACROS: LL_<shifting operators> 1.124 +** 1.125 +** LL_SHL Shift left [0..64] bits 1.126 +** LL_SHR Shift right [0..64] bits with sign extension 1.127 +** LL_USHR Unsigned shift right [0..64] bits 1.128 +** LL_ISHL Signed shift left [0..64] bits 1.129 +***********************************************************************/ 1.130 +#define LL_SHL(r, a, b) ((r) = (PRInt64)(a) << (b)) 1.131 +#define LL_SHR(r, a, b) ((r) = (PRInt64)(a) >> (b)) 1.132 +#define LL_USHR(r, a, b) ((r) = (PRUint64)(a) >> (b)) 1.133 +#define LL_ISHL(r, a, b) ((r) = (PRInt64)(a) << (b)) 1.134 + 1.135 +/*********************************************************************** 1.136 +** MACROS: LL_<conversion operators> 1.137 +** 1.138 +** LL_L2I Convert to signed 32 bit 1.139 +** LL_L2UI Convert to unsigned 32 bit 1.140 +** LL_L2F Convert to floating point 1.141 +** LL_L2D Convert to floating point 1.142 +** LL_I2L Convert signed to 64 bit 1.143 +** LL_UI2L Convert unsigned to 64 bit 1.144 +** LL_F2L Convert float to 64 bit 1.145 +** LL_D2L Convert float to 64 bit 1.146 +***********************************************************************/ 1.147 +#define LL_L2I(i, l) ((i) = (PRInt32)(l)) 1.148 +#define LL_L2UI(ui, l) ((ui) = (PRUint32)(l)) 1.149 +#define LL_L2F(f, l) ((f) = (PRFloat64)(l)) 1.150 +#define LL_L2D(d, l) ((d) = (PRFloat64)(l)) 1.151 + 1.152 +#define LL_I2L(l, i) ((l) = (PRInt64)(i)) 1.153 +#define LL_UI2L(l, ui) ((l) = (PRInt64)(ui)) 1.154 +#define LL_F2L(l, f) ((l) = (PRInt64)(f)) 1.155 +#define LL_D2L(l, d) ((l) = (PRInt64)(d)) 1.156 + 1.157 +/*********************************************************************** 1.158 +** MACROS: LL_UDIVMOD 1.159 +** DESCRIPTION: 1.160 +** Produce both a quotient and a remainder given an unsigned 1.161 +** INPUTS: PRUint64 a: The dividend of the operation 1.162 +** PRUint64 b: The quotient of the operation 1.163 +** OUTPUTS: PRUint64 *qp: pointer to quotient 1.164 +** PRUint64 *rp: pointer to remainder 1.165 +***********************************************************************/ 1.166 +#define LL_UDIVMOD(qp, rp, a, b) \ 1.167 + (*(qp) = ((PRUint64)(a) / (b)), \ 1.168 + *(rp) = ((PRUint64)(a) % (b))) 1.169 + 1.170 +#else /* !HAVE_LONG_LONG */ 1.171 + 1.172 +#define LL_MAXINT LL_MaxInt() 1.173 +#define LL_MININT LL_MinInt() 1.174 +#define LL_ZERO LL_Zero() 1.175 +#define LL_MAXUINT LL_MaxUint() 1.176 + 1.177 +#ifdef IS_LITTLE_ENDIAN 1.178 +#define LL_INIT(hi, lo) {PR_UINT32(lo), PR_UINT32(hi)} 1.179 +#else 1.180 +#define LL_INIT(hi, lo) {PR_UINT32(hi), PR_UINT32(lo)} 1.181 +#endif 1.182 + 1.183 +#define LL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0)) 1.184 +#define LL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo)) 1.185 +#define LL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo)) 1.186 +#define LL_GE_ZERO(a) (((a).hi >> 31) == 0) 1.187 + 1.188 +#define LL_CMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \ 1.189 + ((PRInt32)(a).hi op (PRInt32)(b).hi)) 1.190 +#define LL_UCMP(a, op, b) (((a).hi == (b).hi) ? ((a).lo op (b).lo) : \ 1.191 + ((a).hi op (b).hi)) 1.192 + 1.193 +#define LL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \ 1.194 + (r).hi = (a).hi & (b).hi) 1.195 +#define LL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \ 1.196 + (r).hi = (a).hi | (b).hi) 1.197 +#define LL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \ 1.198 + (r).hi = (a).hi ^ (b).hi) 1.199 +#define LL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \ 1.200 + (r).hi = (r).hi | (a).hi) 1.201 +#define LL_NOT(r, a) ((r).lo = ~(a).lo, \ 1.202 + (r).hi = ~(a).hi) 1.203 + 1.204 +#define LL_NEG(r, a) ((r).lo = -(PRInt32)(a).lo, \ 1.205 + (r).hi = -(PRInt32)(a).hi - ((r).lo != 0)) 1.206 +#define LL_ADD(r, a, b) { \ 1.207 + PRInt64 _a, _b; \ 1.208 + _a = a; _b = b; \ 1.209 + (r).lo = _a.lo + _b.lo; \ 1.210 + (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \ 1.211 +} 1.212 + 1.213 +#define LL_SUB(r, a, b) { \ 1.214 + PRInt64 _a, _b; \ 1.215 + _a = a; _b = b; \ 1.216 + (r).lo = _a.lo - _b.lo; \ 1.217 + (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \ 1.218 +} 1.219 + 1.220 +#define LL_MUL(r, a, b) { \ 1.221 + PRInt64 _a, _b; \ 1.222 + _a = a; _b = b; \ 1.223 + LL_MUL32(r, _a.lo, _b.lo); \ 1.224 + (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \ 1.225 +} 1.226 + 1.227 +#define _lo16(a) ((a) & PR_BITMASK(16)) 1.228 +#define _hi16(a) ((a) >> 16) 1.229 + 1.230 +#define LL_MUL32(r, a, b) { \ 1.231 + PRUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \ 1.232 + _a1 = _hi16(a), _a0 = _lo16(a); \ 1.233 + _b1 = _hi16(b), _b0 = _lo16(b); \ 1.234 + _y0 = _a0 * _b0; \ 1.235 + _y1 = _a0 * _b1; \ 1.236 + _y2 = _a1 * _b0; \ 1.237 + _y3 = _a1 * _b1; \ 1.238 + _y1 += _hi16(_y0); /* can't carry */ \ 1.239 + _y1 += _y2; /* might carry */ \ 1.240 + if (_y1 < _y2) \ 1.241 + _y3 += (PRUint32)(PR_BIT(16)); /* propagate */ \ 1.242 + (r).lo = (_lo16(_y1) << 16) + _lo16(_y0); \ 1.243 + (r).hi = _y3 + _hi16(_y1); \ 1.244 +} 1.245 + 1.246 +#define LL_UDIVMOD(qp, rp, a, b) ll_udivmod(qp, rp, a, b) 1.247 + 1.248 +NSPR_API(void) ll_udivmod(PRUint64 *qp, PRUint64 *rp, PRUint64 a, PRUint64 b); 1.249 + 1.250 +#define LL_DIV(r, a, b) { \ 1.251 + PRInt64 _a, _b; \ 1.252 + PRUint32 _negative = (PRInt32)(a).hi < 0; \ 1.253 + if (_negative) { \ 1.254 + LL_NEG(_a, a); \ 1.255 + } else { \ 1.256 + _a = a; \ 1.257 + } \ 1.258 + if ((PRInt32)(b).hi < 0) { \ 1.259 + _negative ^= 1; \ 1.260 + LL_NEG(_b, b); \ 1.261 + } else { \ 1.262 + _b = b; \ 1.263 + } \ 1.264 + LL_UDIVMOD(&(r), 0, _a, _b); \ 1.265 + if (_negative) \ 1.266 + LL_NEG(r, r); \ 1.267 +} 1.268 + 1.269 +#define LL_MOD(r, a, b) { \ 1.270 + PRInt64 _a, _b; \ 1.271 + PRUint32 _negative = (PRInt32)(a).hi < 0; \ 1.272 + if (_negative) { \ 1.273 + LL_NEG(_a, a); \ 1.274 + } else { \ 1.275 + _a = a; \ 1.276 + } \ 1.277 + if ((PRInt32)(b).hi < 0) { \ 1.278 + LL_NEG(_b, b); \ 1.279 + } else { \ 1.280 + _b = b; \ 1.281 + } \ 1.282 + LL_UDIVMOD(0, &(r), _a, _b); \ 1.283 + if (_negative) \ 1.284 + LL_NEG(r, r); \ 1.285 +} 1.286 + 1.287 +#define LL_SHL(r, a, b) { \ 1.288 + if (b) { \ 1.289 + PRInt64 _a; \ 1.290 + _a = a; \ 1.291 + if ((b) < 32) { \ 1.292 + (r).lo = _a.lo << ((b) & 31); \ 1.293 + (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \ 1.294 + } else { \ 1.295 + (r).lo = 0; \ 1.296 + (r).hi = _a.lo << ((b) & 31); \ 1.297 + } \ 1.298 + } else { \ 1.299 + (r) = (a); \ 1.300 + } \ 1.301 +} 1.302 + 1.303 +/* a is an PRInt32, b is PRInt32, r is PRInt64 */ 1.304 +#define LL_ISHL(r, a, b) { \ 1.305 + if (b) { \ 1.306 + PRInt64 _a; \ 1.307 + _a.lo = (a); \ 1.308 + _a.hi = 0; \ 1.309 + if ((b) < 32) { \ 1.310 + (r).lo = (a) << ((b) & 31); \ 1.311 + (r).hi = ((a) >> (32 - (b))); \ 1.312 + } else { \ 1.313 + (r).lo = 0; \ 1.314 + (r).hi = (a) << ((b) & 31); \ 1.315 + } \ 1.316 + } else { \ 1.317 + (r).lo = (a); \ 1.318 + (r).hi = 0; \ 1.319 + } \ 1.320 +} 1.321 + 1.322 +#define LL_SHR(r, a, b) { \ 1.323 + if (b) { \ 1.324 + PRInt64 _a; \ 1.325 + _a = a; \ 1.326 + if ((b) < 32) { \ 1.327 + (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \ 1.328 + (r).hi = (PRInt32)_a.hi >> ((b) & 31); \ 1.329 + } else { \ 1.330 + (r).lo = (PRInt32)_a.hi >> ((b) & 31); \ 1.331 + (r).hi = (PRInt32)_a.hi >> 31; \ 1.332 + } \ 1.333 + } else { \ 1.334 + (r) = (a); \ 1.335 + } \ 1.336 +} 1.337 + 1.338 +#define LL_USHR(r, a, b) { \ 1.339 + if (b) { \ 1.340 + PRInt64 _a; \ 1.341 + _a = a; \ 1.342 + if ((b) < 32) { \ 1.343 + (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \ 1.344 + (r).hi = _a.hi >> ((b) & 31); \ 1.345 + } else { \ 1.346 + (r).lo = _a.hi >> ((b) & 31); \ 1.347 + (r).hi = 0; \ 1.348 + } \ 1.349 + } else { \ 1.350 + (r) = (a); \ 1.351 + } \ 1.352 +} 1.353 + 1.354 +#define LL_L2I(i, l) ((i) = (l).lo) 1.355 +#define LL_L2UI(ui, l) ((ui) = (l).lo) 1.356 +#define LL_L2F(f, l) { double _d; LL_L2D(_d, l); (f) = (PRFloat64)_d; } 1.357 + 1.358 +#define LL_L2D(d, l) { \ 1.359 + int _negative; \ 1.360 + PRInt64 _absval; \ 1.361 + \ 1.362 + _negative = (l).hi >> 31; \ 1.363 + if (_negative) { \ 1.364 + LL_NEG(_absval, l); \ 1.365 + } else { \ 1.366 + _absval = l; \ 1.367 + } \ 1.368 + (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \ 1.369 + if (_negative) \ 1.370 + (d) = -(d); \ 1.371 +} 1.372 + 1.373 +#define LL_I2L(l, i) { PRInt32 _i = ((PRInt32)(i)) >> 31; (l).lo = (i); (l).hi = _i; } 1.374 +#define LL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0) 1.375 +#define LL_F2L(l, f) { double _d = (double)f; LL_D2L(l, _d); } 1.376 + 1.377 +#define LL_D2L(l, d) { \ 1.378 + int _negative; \ 1.379 + double _absval, _d_hi; \ 1.380 + PRInt64 _lo_d; \ 1.381 + \ 1.382 + _negative = ((d) < 0); \ 1.383 + _absval = _negative ? -(d) : (d); \ 1.384 + \ 1.385 + (l).hi = _absval / 4.294967296e9; \ 1.386 + (l).lo = 0; \ 1.387 + LL_L2D(_d_hi, l); \ 1.388 + _absval -= _d_hi; \ 1.389 + _lo_d.hi = 0; \ 1.390 + if (_absval < 0) { \ 1.391 + _lo_d.lo = -_absval; \ 1.392 + LL_SUB(l, l, _lo_d); \ 1.393 + } else { \ 1.394 + _lo_d.lo = _absval; \ 1.395 + LL_ADD(l, l, _lo_d); \ 1.396 + } \ 1.397 + \ 1.398 + if (_negative) \ 1.399 + LL_NEG(l, l); \ 1.400 +} 1.401 + 1.402 +#endif /* !HAVE_LONG_LONG */ 1.403 + 1.404 +PR_END_EXTERN_C 1.405 + 1.406 +#endif /* prlong_h___ */