prlong.h 14 KB

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