home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / nspr30-e.zip / nspr30-e / include / prlong.h < prev    next >
C/C++ Source or Header  |  1998-07-21  |  13KB  |  410 lines

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