/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jscpucfg.cpp

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs · C++ · 388 lines · 272 code · 49 blank · 67 comment · 67 complexity · aafa88c6460c3ab6e18d78a4089b5420 MD5 · raw file

  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is Mozilla Communicator client code, released
  17. * March 31, 1998.
  18. *
  19. * The Initial Developer of the Original Code is
  20. * Netscape Communications Corporation.
  21. * Portions created by the Initial Developer are Copyright (C) 1998
  22. * the Initial Developer. All Rights Reserved.
  23. *
  24. * Contributor(s):
  25. * Roland Mainz <roland.mainz@informatik.med.uni-giessen.de>
  26. *
  27. * Alternatively, the contents of this file may be used under the terms of
  28. * either of the GNU General Public License Version 2 or later (the "GPL"),
  29. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  30. * in which case the provisions of the GPL or the LGPL are applicable instead
  31. * of those above. If you wish to allow use of your version of this file only
  32. * under the terms of either the GPL or the LGPL, and not to allow others to
  33. * use your version of this file under the terms of the MPL, indicate your
  34. * decision by deleting the provisions above and replace them with the notice
  35. * and other provisions required by the GPL or the LGPL. If you do not delete
  36. * the provisions above, a recipient may use your version of this file under
  37. * the terms of any one of the MPL, the GPL or the LGPL.
  38. *
  39. * ***** END LICENSE BLOCK ***** */
  40. /*
  41. * Generate CPU-specific bit-size and similar #defines.
  42. */
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #ifdef CROSS_COMPILE
  46. #include <prtypes.h>
  47. #define INT64 PRInt64
  48. #else
  49. /************************************************************************/
  50. /* Generate cpucfg.h */
  51. #if defined(XP_WIN) || defined(XP_OS2)
  52. #ifdef WIN32
  53. #if defined(__GNUC__)
  54. #define INT64 long long
  55. #else
  56. #define INT64 _int64
  57. #endif /* __GNUC__ */
  58. #else
  59. #define INT64 long
  60. #endif
  61. #else
  62. #if defined(HPUX) || defined(__QNX__)
  63. #define INT64 long
  64. #else
  65. #define INT64 long long
  66. #endif
  67. #endif
  68. #endif /* CROSS_COMPILE */
  69. #ifdef __GNUC__
  70. #define NS_NEVER_INLINE __attribute__((noinline))
  71. #else
  72. #define NS_NEVER_INLINE
  73. #endif
  74. #ifdef __SUNPRO_C
  75. static int StackGrowthDirection(int *dummy1addr);
  76. #pragma no_inline(StackGrowthDirection)
  77. #endif
  78. typedef void *prword;
  79. struct align_short {
  80. char c;
  81. short a;
  82. };
  83. struct align_int {
  84. char c;
  85. int a;
  86. };
  87. struct align_long {
  88. char c;
  89. long a;
  90. };
  91. struct align_int64 {
  92. char c;
  93. INT64 a;
  94. };
  95. struct align_fakelonglong {
  96. char c;
  97. struct {
  98. long hi, lo;
  99. } a;
  100. };
  101. struct align_float {
  102. char c;
  103. float a;
  104. };
  105. struct align_double {
  106. char c;
  107. double a;
  108. };
  109. struct align_pointer {
  110. char c;
  111. void *a;
  112. };
  113. struct align_prword {
  114. char c;
  115. prword a;
  116. };
  117. #define ALIGN_OF(type) \
  118. (((char*)&(((struct align_##type *)0)->a)) - ((char*)0))
  119. unsigned int bpb;
  120. static int Log2(unsigned int n)
  121. {
  122. int log2 = 0;
  123. if (n & (n-1))
  124. log2++;
  125. if (n >> 16)
  126. log2 += 16, n >>= 16;
  127. if (n >> 8)
  128. log2 += 8, n >>= 8;
  129. if (n >> 4)
  130. log2 += 4, n >>= 4;
  131. if (n >> 2)
  132. log2 += 2, n >>= 2;
  133. if (n >> 1)
  134. log2++;
  135. return log2;
  136. }
  137. /*
  138. * Conceivably this could actually be used, but there is lots of code out
  139. * there with ands and shifts in it that assumes a byte is exactly 8 bits,
  140. * so forget about porting THIS code to all those non 8 bit byte machines.
  141. */
  142. static void BitsPerByte(void)
  143. {
  144. bpb = 8;
  145. }
  146. static int NS_NEVER_INLINE StackGrowthDirection(int *dummy1addr)
  147. {
  148. int dummy2;
  149. return (&dummy2 < dummy1addr) ? -1 : 1;
  150. }
  151. int main(int argc, char **argv)
  152. {
  153. int sizeof_char, sizeof_short, sizeof_int, sizeof_int64, sizeof_long,
  154. sizeof_float, sizeof_double, sizeof_word, sizeof_dword;
  155. int bits_per_int64_log2, align_of_short, align_of_int, align_of_long,
  156. align_of_int64, align_of_float, align_of_double, align_of_pointer,
  157. align_of_word;
  158. int dummy1;
  159. BitsPerByte();
  160. printf("#ifndef js_cpucfg___\n");
  161. printf("#define js_cpucfg___\n\n");
  162. printf("/* AUTOMATICALLY GENERATED - DO NOT EDIT */\n\n");
  163. #ifdef CROSS_COMPILE
  164. #if defined(IS_LITTLE_ENDIAN)
  165. printf("#define IS_LITTLE_ENDIAN 1\n");
  166. printf("#undef IS_BIG_ENDIAN\n\n");
  167. #elif defined(IS_BIG_ENDIAN)
  168. printf("#undef IS_LITTLE_ENDIAN\n");
  169. printf("#define IS_BIG_ENDIAN 1\n\n");
  170. #else
  171. #error "Endianess not defined."
  172. #endif
  173. sizeof_char = PR_BYTES_PER_BYTE;
  174. sizeof_short = PR_BYTES_PER_SHORT;
  175. sizeof_int = PR_BYTES_PER_INT;
  176. sizeof_int64 = PR_BYTES_PER_INT64;
  177. sizeof_long = PR_BYTES_PER_LONG;
  178. sizeof_float = PR_BYTES_PER_FLOAT;
  179. sizeof_double = PR_BYTES_PER_DOUBLE;
  180. sizeof_word = PR_BYTES_PER_WORD;
  181. sizeof_dword = PR_BYTES_PER_DWORD;
  182. bits_per_int64_log2 = PR_BITS_PER_INT64_LOG2;
  183. align_of_short = PR_ALIGN_OF_SHORT;
  184. align_of_int = PR_ALIGN_OF_INT;
  185. align_of_long = PR_ALIGN_OF_LONG;
  186. align_of_int64 = PR_ALIGN_OF_INT64;
  187. align_of_float = PR_ALIGN_OF_FLOAT;
  188. align_of_double = PR_ALIGN_OF_DOUBLE;
  189. align_of_pointer = PR_ALIGN_OF_POINTER;
  190. align_of_word = PR_ALIGN_OF_WORD;
  191. #else /* !CROSS_COMPILE */
  192. /*
  193. * We don't handle PDP-endian or similar orders: if a short is big-endian,
  194. * so must int and long be big-endian for us to generate the IS_BIG_ENDIAN
  195. * #define and the IS_LITTLE_ENDIAN #undef.
  196. */
  197. {
  198. int big_endian = 0, little_endian = 0, ntests = 0;
  199. if (sizeof(short) == 2) {
  200. /* force |volatile| here to get rid of any compiler optimisations
  201. * (var in register etc.) which may be appiled to |auto| vars -
  202. * even those in |union|s...
  203. * (|static| is used to get the same functionality for compilers
  204. * which do not honor |volatile|...).
  205. */
  206. volatile static union {
  207. short i;
  208. char c[2];
  209. } u;
  210. u.i = 0x0102;
  211. big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02);
  212. little_endian += (u.c[0] == 0x02 && u.c[1] == 0x01);
  213. ntests++;
  214. }
  215. if (sizeof(int) == 4) {
  216. /* force |volatile| here ... */
  217. volatile static union {
  218. int i;
  219. char c[4];
  220. } u;
  221. u.i = 0x01020304;
  222. big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 &&
  223. u.c[2] == 0x03 && u.c[3] == 0x04);
  224. little_endian += (u.c[0] == 0x04 && u.c[1] == 0x03 &&
  225. u.c[2] == 0x02 && u.c[3] == 0x01);
  226. ntests++;
  227. }
  228. if (sizeof(long) == 8) {
  229. /* force |volatile| here ... */
  230. volatile static union {
  231. long i;
  232. char c[8];
  233. } u;
  234. /*
  235. * Write this as portably as possible: avoid 0x0102030405060708L
  236. * and <<= 32.
  237. */
  238. u.i = 0x01020304;
  239. u.i <<= 16, u.i <<= 16;
  240. u.i |= 0x05060708;
  241. big_endian += (u.c[0] == 0x01 && u.c[1] == 0x02 &&
  242. u.c[2] == 0x03 && u.c[3] == 0x04 &&
  243. u.c[4] == 0x05 && u.c[5] == 0x06 &&
  244. u.c[6] == 0x07 && u.c[7] == 0x08);
  245. little_endian += (u.c[0] == 0x08 && u.c[1] == 0x07 &&
  246. u.c[2] == 0x06 && u.c[3] == 0x05 &&
  247. u.c[4] == 0x04 && u.c[5] == 0x03 &&
  248. u.c[6] == 0x02 && u.c[7] == 0x01);
  249. ntests++;
  250. }
  251. if (big_endian && big_endian == ntests) {
  252. printf("#undef IS_LITTLE_ENDIAN\n");
  253. printf("#define IS_BIG_ENDIAN 1\n\n");
  254. } else if (little_endian && little_endian == ntests) {
  255. printf("#define IS_LITTLE_ENDIAN 1\n");
  256. printf("#undef IS_BIG_ENDIAN\n\n");
  257. } else {
  258. fprintf(stderr, "%s: unknown byte order"
  259. "(big_endian=%d, little_endian=%d, ntests=%d)!\n",
  260. argv[0], big_endian, little_endian, ntests);
  261. return EXIT_FAILURE;
  262. }
  263. }
  264. sizeof_char = sizeof(char);
  265. sizeof_short = sizeof(short);
  266. sizeof_int = sizeof(int);
  267. sizeof_int64 = 8;
  268. sizeof_long = sizeof(long);
  269. sizeof_float = sizeof(float);
  270. sizeof_double = sizeof(double);
  271. sizeof_word = sizeof(prword);
  272. sizeof_dword = 8;
  273. bits_per_int64_log2 = 6;
  274. align_of_short = ALIGN_OF(short);
  275. align_of_int = ALIGN_OF(int);
  276. align_of_long = ALIGN_OF(long);
  277. if (sizeof(INT64) < 8) {
  278. /* this machine doesn't actually support int64's */
  279. align_of_int64 = ALIGN_OF(fakelonglong);
  280. } else {
  281. align_of_int64 = ALIGN_OF(int64);
  282. }
  283. align_of_float = ALIGN_OF(float);
  284. align_of_double = ALIGN_OF(double);
  285. align_of_pointer = ALIGN_OF(pointer);
  286. align_of_word = ALIGN_OF(prword);
  287. #endif /* CROSS_COMPILE */
  288. printf("#define JS_BYTES_PER_BYTE %dL\n", sizeof_char);
  289. printf("#define JS_BYTES_PER_SHORT %dL\n", sizeof_short);
  290. printf("#define JS_BYTES_PER_INT %dL\n", sizeof_int);
  291. printf("#define JS_BYTES_PER_INT64 %dL\n", sizeof_int64);
  292. printf("#define JS_BYTES_PER_LONG %dL\n", sizeof_long);
  293. printf("#define JS_BYTES_PER_FLOAT %dL\n", sizeof_float);
  294. printf("#define JS_BYTES_PER_DOUBLE %dL\n", sizeof_double);
  295. printf("#define JS_BYTES_PER_WORD %dL\n", sizeof_word);
  296. printf("#define JS_BYTES_PER_DWORD %dL\n", sizeof_dword);
  297. printf("\n");
  298. printf("#define JS_BITS_PER_BYTE %dL\n", bpb);
  299. printf("#define JS_BITS_PER_SHORT %dL\n", bpb * sizeof_short);
  300. printf("#define JS_BITS_PER_INT %dL\n", bpb * sizeof_int);
  301. printf("#define JS_BITS_PER_INT64 %dL\n", bpb * sizeof_int64);
  302. printf("#define JS_BITS_PER_LONG %dL\n", bpb * sizeof_long);
  303. printf("#define JS_BITS_PER_FLOAT %dL\n", bpb * sizeof_float);
  304. printf("#define JS_BITS_PER_DOUBLE %dL\n", bpb * sizeof_double);
  305. printf("#define JS_BITS_PER_WORD %dL\n", bpb * sizeof_word);
  306. printf("\n");
  307. printf("#define JS_BITS_PER_BYTE_LOG2 %dL\n", Log2(bpb));
  308. printf("#define JS_BITS_PER_SHORT_LOG2 %dL\n", Log2(bpb * sizeof_short));
  309. printf("#define JS_BITS_PER_INT_LOG2 %dL\n", Log2(bpb * sizeof_int));
  310. printf("#define JS_BITS_PER_INT64_LOG2 %dL\n", bits_per_int64_log2);
  311. printf("#define JS_BITS_PER_LONG_LOG2 %dL\n", Log2(bpb * sizeof_long));
  312. printf("#define JS_BITS_PER_FLOAT_LOG2 %dL\n", Log2(bpb * sizeof_float));
  313. printf("#define JS_BITS_PER_DOUBLE_LOG2 %dL\n", Log2(bpb * sizeof_double));
  314. printf("#define JS_BITS_PER_WORD_LOG2 %dL\n", Log2(bpb * sizeof_word));
  315. printf("\n");
  316. printf("#define JS_ALIGN_OF_SHORT %dL\n", align_of_short);
  317. printf("#define JS_ALIGN_OF_INT %dL\n", align_of_int);
  318. printf("#define JS_ALIGN_OF_LONG %dL\n", align_of_long);
  319. printf("#define JS_ALIGN_OF_INT64 %dL\n", align_of_int64);
  320. printf("#define JS_ALIGN_OF_FLOAT %dL\n", align_of_float);
  321. printf("#define JS_ALIGN_OF_DOUBLE %dL\n", align_of_double);
  322. printf("#define JS_ALIGN_OF_POINTER %dL\n", align_of_pointer);
  323. printf("#define JS_ALIGN_OF_WORD %dL\n", align_of_word);
  324. printf("\n");
  325. printf("#define JS_BYTES_PER_WORD_LOG2 %dL\n", Log2(sizeof_word));
  326. printf("#define JS_BYTES_PER_DWORD_LOG2 %dL\n", Log2(sizeof_dword));
  327. printf("#define JS_WORDS_PER_DWORD_LOG2 %dL\n", Log2(sizeof_dword/sizeof_word));
  328. printf("\n");
  329. printf("#define JS_STACK_GROWTH_DIRECTION (%d)\n", StackGrowthDirection(&dummy1));
  330. printf("\n");
  331. printf("#define JS_HAVE_LONG_LONG\n");
  332. printf("\n");
  333. #if defined __GNUC__ && defined __x86_64__
  334. printf("#define HAVE_VA_LIST_AS_ARRAY 1\n");
  335. printf("\n");
  336. #endif
  337. printf("#endif /* js_cpucfg___ */\n");
  338. return EXIT_SUCCESS;
  339. }