PageRenderTime 56ms CodeModel.GetById 40ms RepoModel.GetById 0ms app.codeStats 1ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jslong.h

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C++ Header | 412 lines | 258 code | 40 blank | 114 comment | 58 complexity | 6b5342ebc85ea0d70a213c0f721cb75d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  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 Mozilla Communicator client code, released
  16. * March 31, 1998.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications Corporation.
  20. * Portions created by the Initial Developer are Copyright (C) 1998
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. /*
  39. ** File: jslong.h
  40. ** Description: Portable access to 64 bit numerics
  41. **
  42. ** Long-long (64-bit signed integer type) support. Some C compilers
  43. ** don't support 64 bit integers yet, so we use these macros to
  44. ** support both machines that do and don't.
  45. **/
  46. #ifndef jslong_h___
  47. #define jslong_h___
  48. #include "jstypes.h"
  49. JS_BEGIN_EXTERN_C
  50. #ifdef JS_HAVE_LONG_LONG
  51. #if JS_BYTES_PER_LONG == 8
  52. #define JSLL_INIT(hi, lo) ((hi ## L << 32) + lo ## L)
  53. #elif (defined(WIN32) || defined(WIN16)) && !defined(__GNUC__)
  54. #define JSLL_INIT(hi, lo) ((hi ## i64 << 32) + lo ## i64)
  55. #else
  56. #define JSLL_INIT(hi, lo) ((hi ## LL << 32) + lo ## LL)
  57. #endif
  58. /***********************************************************************
  59. ** MACROS: JSLL_*
  60. ** DESCRIPTION:
  61. ** The following macros define portable access to the 64 bit
  62. ** math facilities.
  63. **
  64. ***********************************************************************/
  65. /***********************************************************************
  66. ** MACROS: JSLL_<relational operators>
  67. **
  68. ** JSLL_IS_ZERO Test for zero
  69. ** JSLL_EQ Test for equality
  70. ** JSLL_NE Test for inequality
  71. ** JSLL_GE_ZERO Test for zero or positive
  72. ** JSLL_CMP Compare two values
  73. ***********************************************************************/
  74. #define JSLL_IS_ZERO(a) ((a) == 0)
  75. #define JSLL_EQ(a, b) ((a) == (b))
  76. #define JSLL_NE(a, b) ((a) != (b))
  77. #define JSLL_GE_ZERO(a) ((a) >= 0)
  78. #define JSLL_CMP(a, op, b) ((JSInt64)(a) op (JSInt64)(b))
  79. #define JSLL_UCMP(a, op, b) ((JSUint64)(a) op (JSUint64)(b))
  80. /***********************************************************************
  81. ** MACROS: JSLL_<logical operators>
  82. **
  83. ** JSLL_AND Logical and
  84. ** JSLL_OR Logical or
  85. ** JSLL_XOR Logical exclusion
  86. ** JSLL_OR2 A disgusting deviation
  87. ** JSLL_NOT Negation (one's compliment)
  88. ***********************************************************************/
  89. #define JSLL_AND(r, a, b) ((r) = (a) & (b))
  90. #define JSLL_OR(r, a, b) ((r) = (a) | (b))
  91. #define JSLL_XOR(r, a, b) ((r) = (a) ^ (b))
  92. #define JSLL_OR2(r, a) ((r) = (r) | (a))
  93. #define JSLL_NOT(r, a) ((r) = ~(a))
  94. /***********************************************************************
  95. ** MACROS: JSLL_<mathematical operators>
  96. **
  97. ** JSLL_NEG Negation (two's compliment)
  98. ** JSLL_ADD Summation (two's compliment)
  99. ** JSLL_SUB Difference (two's compliment)
  100. ***********************************************************************/
  101. #define JSLL_NEG(r, a) ((r) = -(a))
  102. #define JSLL_ADD(r, a, b) ((r) = (a) + (b))
  103. #define JSLL_SUB(r, a, b) ((r) = (a) - (b))
  104. /***********************************************************************
  105. ** MACROS: JSLL_<mathematical operators>
  106. **
  107. ** JSLL_MUL Product (two's compliment)
  108. ** JSLL_DIV Quotient (two's compliment)
  109. ** JSLL_MOD Modulus (two's compliment)
  110. ***********************************************************************/
  111. #define JSLL_MUL(r, a, b) ((r) = (a) * (b))
  112. #define JSLL_DIV(r, a, b) ((r) = (a) / (b))
  113. #define JSLL_MOD(r, a, b) ((r) = (a) % (b))
  114. /***********************************************************************
  115. ** MACROS: JSLL_<shifting operators>
  116. **
  117. ** JSLL_SHL Shift left [0..64] bits
  118. ** JSLL_SHR Shift right [0..64] bits with sign extension
  119. ** JSLL_USHR Unsigned shift right [0..64] bits
  120. ** JSLL_ISHL Signed shift left [0..64] bits
  121. ***********************************************************************/
  122. #define JSLL_SHL(r, a, b) ((r) = (JSInt64)(a) << (b))
  123. #define JSLL_SHR(r, a, b) ((r) = (JSInt64)(a) >> (b))
  124. #define JSLL_USHR(r, a, b) ((r) = (JSUint64)(a) >> (b))
  125. #define JSLL_ISHL(r, a, b) ((r) = (JSInt64)(a) << (b))
  126. /***********************************************************************
  127. ** MACROS: JSLL_<conversion operators>
  128. **
  129. ** JSLL_L2I Convert to signed 32 bit
  130. ** JSLL_L2UI Convert to unsigned 32 bit
  131. ** JSLL_L2F Convert to floating point
  132. ** JSLL_L2D Convert to floating point
  133. ** JSLL_I2L Convert signed to 64 bit
  134. ** JSLL_UI2L Convert unsigned to 64 bit
  135. ** JSLL_F2L Convert float to 64 bit
  136. ** JSLL_D2L Convert float to 64 bit
  137. ***********************************************************************/
  138. #define JSLL_L2I(i, l) ((i) = (JSInt32)(l))
  139. #define JSLL_L2UI(ui, l) ((ui) = (JSUint32)(l))
  140. #define JSLL_L2F(f, l) ((f) = (JSFloat64)(l))
  141. #define JSLL_L2D(d, l) ((d) = (JSFloat64)(l))
  142. #define JSLL_I2L(l, i) ((l) = (JSInt64)(i))
  143. #define JSLL_UI2L(l, ui) ((l) = (JSInt64)(ui))
  144. #define JSLL_F2L(l, f) ((l) = (JSInt64)(f))
  145. #define JSLL_D2L(l, d) ((l) = (JSInt64)(d))
  146. /***********************************************************************
  147. ** MACROS: JSLL_UDIVMOD
  148. ** DESCRIPTION:
  149. ** Produce both a quotient and a remainder given an unsigned
  150. ** INPUTS: JSUint64 a: The dividend of the operation
  151. ** JSUint64 b: The quotient of the operation
  152. ** OUTPUTS: JSUint64 *qp: pointer to quotient
  153. ** JSUint64 *rp: pointer to remainder
  154. ***********************************************************************/
  155. #define JSLL_UDIVMOD(qp, rp, a, b) \
  156. (*(qp) = ((JSUint64)(a) / (b)), \
  157. *(rp) = ((JSUint64)(a) % (b)))
  158. #else /* !JS_HAVE_LONG_LONG */
  159. #ifdef IS_LITTLE_ENDIAN
  160. #define JSLL_INIT(hi, lo) {JS_INT32(lo), JS_INT32(hi)}
  161. #else
  162. #define JSLL_INIT(hi, lo) {JS_INT32(hi), JS_INT32(lo)}
  163. #endif
  164. #define JSLL_IS_ZERO(a) (((a).hi == 0) && ((a).lo == 0))
  165. #define JSLL_EQ(a, b) (((a).hi == (b).hi) && ((a).lo == (b).lo))
  166. #define JSLL_NE(a, b) (((a).hi != (b).hi) || ((a).lo != (b).lo))
  167. #define JSLL_GE_ZERO(a) (((a).hi >> 31) == 0)
  168. #ifdef DEBUG
  169. #define JSLL_CMP(a, op, b) (JS_ASSERT((#op)[1] != '='), JSLL_REAL_CMP(a, op, b))
  170. #define JSLL_UCMP(a, op, b) (JS_ASSERT((#op)[1] != '='), JSLL_REAL_UCMP(a, op, b))
  171. #else
  172. #define JSLL_CMP(a, op, b) JSLL_REAL_CMP(a, op, b)
  173. #define JSLL_UCMP(a, op, b) JSLL_REAL_UCMP(a, op, b)
  174. #endif
  175. #define JSLL_REAL_CMP(a,op,b) (((JSInt32)(a).hi op (JSInt32)(b).hi) || \
  176. (((a).hi == (b).hi) && ((a).lo op (b).lo)))
  177. #define JSLL_REAL_UCMP(a,op,b) (((a).hi op (b).hi) || \
  178. (((a).hi == (b).hi) && ((a).lo op (b).lo)))
  179. #define JSLL_AND(r, a, b) ((r).lo = (a).lo & (b).lo, \
  180. (r).hi = (a).hi & (b).hi)
  181. #define JSLL_OR(r, a, b) ((r).lo = (a).lo | (b).lo, \
  182. (r).hi = (a).hi | (b).hi)
  183. #define JSLL_XOR(r, a, b) ((r).lo = (a).lo ^ (b).lo, \
  184. (r).hi = (a).hi ^ (b).hi)
  185. #define JSLL_OR2(r, a) ((r).lo = (r).lo | (a).lo, \
  186. (r).hi = (r).hi | (a).hi)
  187. #define JSLL_NOT(r, a) ((r).lo = ~(a).lo, \
  188. (r).hi = ~(a).hi)
  189. #define JSLL_NEG(r, a) ((r).lo = -(JSInt32)(a).lo, \
  190. (r).hi = -(JSInt32)(a).hi - ((r).lo != 0))
  191. #define JSLL_ADD(r, a, b) { \
  192. JSInt64 _a, _b; \
  193. _a = a; _b = b; \
  194. (r).lo = _a.lo + _b.lo; \
  195. (r).hi = _a.hi + _b.hi + ((r).lo < _b.lo); \
  196. }
  197. #define JSLL_SUB(r, a, b) { \
  198. JSInt64 _a, _b; \
  199. _a = a; _b = b; \
  200. (r).lo = _a.lo - _b.lo; \
  201. (r).hi = _a.hi - _b.hi - (_a.lo < _b.lo); \
  202. }
  203. #define JSLL_MUL(r, a, b) { \
  204. JSInt64 _a, _b; \
  205. _a = a; _b = b; \
  206. JSLL_MUL32(r, _a.lo, _b.lo); \
  207. (r).hi += _a.hi * _b.lo + _a.lo * _b.hi; \
  208. }
  209. #define jslo16(a) ((a) & JS_BITMASK(16))
  210. #define jshi16(a) ((a) >> 16)
  211. #define JSLL_MUL32(r, a, b) { \
  212. JSUint32 _a1, _a0, _b1, _b0, _y0, _y1, _y2, _y3; \
  213. _a1 = jshi16(a), _a0 = jslo16(a); \
  214. _b1 = jshi16(b), _b0 = jslo16(b); \
  215. _y0 = _a0 * _b0; \
  216. _y1 = _a0 * _b1; \
  217. _y2 = _a1 * _b0; \
  218. _y3 = _a1 * _b1; \
  219. _y1 += jshi16(_y0); /* can't carry */ \
  220. _y1 += _y2; /* might carry */ \
  221. if (_y1 < _y2) \
  222. _y3 += (JSUint32)(JS_BIT(16)); /* propagate */ \
  223. (r).lo = (jslo16(_y1) << 16) + jslo16(_y0); \
  224. (r).hi = _y3 + jshi16(_y1); \
  225. }
  226. #define JSLL_UDIVMOD(qp, rp, a, b) jsll_udivmod(qp, rp, a, b)
  227. extern JS_PUBLIC_API(void) jsll_udivmod(JSUint64 *qp, JSUint64 *rp, JSUint64 a, JSUint64 b);
  228. #define JSLL_DIV(r, a, b) { \
  229. JSInt64 _a, _b; \
  230. JSUint32 _negative = (JSInt32)(a).hi < 0; \
  231. if (_negative) { \
  232. JSLL_NEG(_a, a); \
  233. } else { \
  234. _a = a; \
  235. } \
  236. if ((JSInt32)(b).hi < 0) { \
  237. _negative ^= 1; \
  238. JSLL_NEG(_b, b); \
  239. } else { \
  240. _b = b; \
  241. } \
  242. JSLL_UDIVMOD(&(r), 0, _a, _b); \
  243. if (_negative) \
  244. JSLL_NEG(r, r); \
  245. }
  246. #define JSLL_MOD(r, a, b) { \
  247. JSInt64 _a, _b; \
  248. JSUint32 _negative = (JSInt32)(a).hi < 0; \
  249. if (_negative) { \
  250. JSLL_NEG(_a, a); \
  251. } else { \
  252. _a = a; \
  253. } \
  254. if ((JSInt32)(b).hi < 0) { \
  255. JSLL_NEG(_b, b); \
  256. } else { \
  257. _b = b; \
  258. } \
  259. JSLL_UDIVMOD(0, &(r), _a, _b); \
  260. if (_negative) \
  261. JSLL_NEG(r, r); \
  262. }
  263. #define JSLL_SHL(r, a, b) { \
  264. if (b) { \
  265. JSInt64 _a; \
  266. _a = a; \
  267. if ((b) < 32) { \
  268. (r).lo = _a.lo << ((b) & 31); \
  269. (r).hi = (_a.hi << ((b) & 31)) | (_a.lo >> (32 - (b))); \
  270. } else { \
  271. (r).lo = 0; \
  272. (r).hi = _a.lo << ((b) & 31); \
  273. } \
  274. } else { \
  275. (r) = (a); \
  276. } \
  277. }
  278. /* a is an JSInt32, b is JSInt32, r is JSInt64 */
  279. #define JSLL_ISHL(r, a, b) { \
  280. if (b) { \
  281. JSInt64 _a; \
  282. _a.lo = (a); \
  283. _a.hi = 0; \
  284. if ((b) < 32) { \
  285. (r).lo = (a) << ((b) & 31); \
  286. (r).hi = ((a) >> (32 - (b))); \
  287. } else { \
  288. (r).lo = 0; \
  289. (r).hi = (a) << ((b) & 31); \
  290. } \
  291. } else { \
  292. (r).lo = (a); \
  293. (r).hi = 0; \
  294. } \
  295. }
  296. #define JSLL_SHR(r, a, b) { \
  297. if (b) { \
  298. JSInt64 _a; \
  299. _a = a; \
  300. if ((b) < 32) { \
  301. (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
  302. (r).hi = (JSInt32)_a.hi >> ((b) & 31); \
  303. } else { \
  304. (r).lo = (JSInt32)_a.hi >> ((b) & 31); \
  305. (r).hi = (JSInt32)_a.hi >> 31; \
  306. } \
  307. } else { \
  308. (r) = (a); \
  309. } \
  310. }
  311. #define JSLL_USHR(r, a, b) { \
  312. if (b) { \
  313. JSInt64 _a; \
  314. _a = a; \
  315. if ((b) < 32) { \
  316. (r).lo = (_a.hi << (32 - (b))) | (_a.lo >> ((b) & 31)); \
  317. (r).hi = _a.hi >> ((b) & 31); \
  318. } else { \
  319. (r).lo = _a.hi >> ((b) & 31); \
  320. (r).hi = 0; \
  321. } \
  322. } else { \
  323. (r) = (a); \
  324. } \
  325. }
  326. #define JSLL_L2I(i, l) ((i) = (l).lo)
  327. #define JSLL_L2UI(ui, l) ((ui) = (l).lo)
  328. #define JSLL_L2F(f, l) { double _d; JSLL_L2D(_d, l); (f) = (JSFloat64)_d; }
  329. #define JSLL_L2D(d, l) { \
  330. int _negative; \
  331. JSInt64 _absval; \
  332. \
  333. _negative = (l).hi >> 31; \
  334. if (_negative) { \
  335. JSLL_NEG(_absval, l); \
  336. } else { \
  337. _absval = l; \
  338. } \
  339. (d) = (double)_absval.hi * 4.294967296e9 + _absval.lo; \
  340. if (_negative) \
  341. (d) = -(d); \
  342. }
  343. #define JSLL_I2L(l, i) { JSInt32 _i = (i) >> 31; (l).lo = (i); (l).hi = _i; }
  344. #define JSLL_UI2L(l, ui) ((l).lo = (ui), (l).hi = 0)
  345. #define JSLL_F2L(l, f) { double _d = (double)f; JSLL_D2L(l, _d); }
  346. #define JSLL_D2L(l, d) { \
  347. int _negative; \
  348. double _absval, _d_hi; \
  349. JSInt64 _lo_d; \
  350. \
  351. _negative = ((d) < 0); \
  352. _absval = _negative ? -(d) : (d); \
  353. \
  354. (l).hi = _absval / 4.294967296e9; \
  355. (l).lo = 0; \
  356. JSLL_L2D(_d_hi, l); \
  357. _absval -= _d_hi; \
  358. _lo_d.hi = 0; \
  359. if (_absval < 0) { \
  360. _lo_d.lo = -_absval; \
  361. JSLL_SUB(l, l, _lo_d); \
  362. } else { \
  363. _lo_d.lo = _absval; \
  364. JSLL_ADD(l, l, _lo_d); \
  365. } \
  366. \
  367. if (_negative) \
  368. JSLL_NEG(l, l); \
  369. }
  370. #endif /* !JS_HAVE_LONG_LONG */
  371. JS_END_EXTERN_C
  372. #endif /* jslong_h___ */