posh.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. /**
  2. @file posh.h
  3. @author Brian Hook
  4. @version 1.3.001
  5. Header file for POSH, the Portable Open Source Harness project.
  6. NOTE: Unlike most header files, this one is designed to be included
  7. multiple times, which is why it does not have the @#ifndef/@#define
  8. preamble.
  9. POSH relies on environment specified preprocessor symbols in order
  10. to infer as much as possible about the target OS/architecture and
  11. the host compiler capabilities.
  12. NOTE: POSH is simple and focused. It attempts to provide basic
  13. functionality and information, but it does NOT attempt to emulate
  14. missing functionality. I am also not willing to make POSH dirty
  15. and hackish to support truly ancient and/or outmoded and/or bizarre
  16. technologies such as non-ANSI compilers, systems with non-IEEE
  17. floating point formats, segmented 16-bit operating systems, etc.
  18. Please refer to the accompanying HTML documentation or visit
  19. http://www.poshlib.org for more information on how to use POSH.
  20. LICENSE:
  21. Copyright (c) 2004, Brian Hook
  22. All rights reserved.
  23. Redistribution and use in source and binary forms, with or without
  24. modification, are permitted provided that the following conditions are
  25. met:
  26. * Redistributions of source code must retain the above copyright
  27. notice, this list of conditions and the following disclaimer.
  28. * Redistributions in binary form must reproduce the above
  29. copyright notice, this list of conditions and the following
  30. disclaimer in the documentation and/or other materials provided
  31. with the distribution.
  32. * The names of this package'ss contributors contributors may not
  33. be used to endorse or promote products derived from this
  34. software without specific prior written permission.
  35. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  36. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  37. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  38. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  39. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  42. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  43. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  44. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  45. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. REVISION:
  47. I've been lax about revision histories, so this starts at, um, 1.3.001.
  48. Sorry for any inconveniences.
  49. 1.3.001 - 2/23/2006 - Incorporated fix for bug reported by Bill Cary,
  50. where I was not detecting Visual Studio
  51. compilation on x86-64 systems. Added check for
  52. _M_X64 which should fix that.
  53. */
  54. /*
  55. I have yet to find an authoritative reference on preprocessor
  56. symbols, but so far this is what I've gleaned:
  57. GNU GCC/G++:
  58. - __GNUC__: GNU C version
  59. - __GNUG__: GNU C++ compiler
  60. - __sun__ : on Sun platforms
  61. - __svr4__: on Solaris and other SysV R4 platforms
  62. - __mips__: on MIPS processor platforms
  63. - __sparc_v9__: on Sparc 64-bit CPUs
  64. - __sparcv9: 64-bit Solaris
  65. - __MIPSEL__: mips processor, compiled for little endian
  66. - __MIPSEB__: mips processor, compiled for big endian
  67. - _R5900: MIPS/Sony/Toshiba R5900 (PS2)
  68. - mc68000: 68K
  69. - m68000: 68K
  70. - m68k: 68K
  71. - __palmos__: PalmOS
  72. Intel C/C++ Compiler:
  73. - __ECC : compiler version, IA64 only
  74. - __EDG__
  75. - __ELF__
  76. - __GXX_ABI_VERSION
  77. - __i386 : IA-32 only
  78. - __i386__ : IA-32 only
  79. - i386 : IA-32 only
  80. - __ia64 : IA-64 only
  81. - __ia64__ : IA-64 only
  82. - ia64 : IA-64 only
  83. - __ICC : IA-32 only
  84. - __INTEL_COMPILER : IA-32 or IA-64, newer versions only
  85. Apple's C/C++ Compiler for OS X:
  86. - __APPLE_CC__
  87. - __APPLE__
  88. - __BIG_ENDIAN__
  89. - __APPLE__
  90. - __ppc__
  91. - __MACH__
  92. DJGPP:
  93. - __MSDOS__
  94. - __unix__
  95. - __unix
  96. - __GNUC__
  97. - __GO32
  98. - DJGPP
  99. - __i386, __i386, i386
  100. Cray's C compiler:
  101. - _ADDR64: if 64-bit pointers
  102. - _UNICOS:
  103. - __unix:
  104. SGI's CC compiler predefines the following (and more) with -ansi:
  105. - __sgi
  106. - __unix
  107. - __host_mips
  108. - _SYSTYPE_SVR4
  109. - __mips
  110. - _MIPSEB
  111. - anyone know if there is a predefined symbol for the compiler?!
  112. MinGW:
  113. - as GnuC but also defines _WIN32, __WIN32, WIN32, _X86_, __i386, __i386__, and several others
  114. - __MINGW32__
  115. Cygwin:
  116. - as Gnu C, but also
  117. - __unix__
  118. - __CYGWIN32__
  119. Microsoft Visual Studio predefines the following:
  120. - _MSC_VER
  121. - _WIN32: on Win32
  122. - _M_IX6 (on x86 systems)
  123. - _M_X64: on x86-64 systems
  124. - _M_ALPHA (on DEC AXP systems)
  125. - _SH3: WinCE, Hitachi SH-3
  126. - _MIPS: WinCE, MIPS
  127. - _ARM: WinCE, ARM
  128. Sun's C Compiler:
  129. - sun and _sun
  130. - unix and _unix
  131. - sparc and _sparc (SPARC systems only)
  132. - i386 and _i386 (x86 systems only)
  133. - __SVR4 (Solaris only)
  134. - __sparcv9: 64-bit solaris
  135. - __SUNPRO_C
  136. - _LP64: defined in 64-bit LP64 mode, but only if <sys/types.h> is included
  137. Borland C/C++ predefines the following:
  138. - __BORLANDC__:
  139. DEC/Compaq C/C++ on Alpha:
  140. - __alpha
  141. - __arch64__
  142. - __unix__ (on Tru64 Unix)
  143. - __osf__
  144. - __DECC
  145. - __DECCXX (C++ compilation)
  146. - __DECC_VER
  147. - __DECCXX_VER
  148. IBM's AIX compiler:
  149. - __64BIT__ if 64-bit mode
  150. - _AIX
  151. - __IBMC__: C compiler version
  152. - __IBMCPP__: C++ compiler version
  153. - _LONG_LONG: compiler allows long long
  154. Watcom:
  155. - __WATCOMC__
  156. - __DOS__ : if targeting DOS
  157. - __386__ : if 32-bit support
  158. - __WIN32__ : if targetin 32-bit Windows
  159. HP-UX C/C++ Compiler:
  160. - __hpux
  161. - __unix
  162. - __hppa (on PA-RISC)
  163. - __LP64__: if compiled in 64-bit mode
  164. Metrowerks:
  165. - __MWERKS__
  166. - __powerpc__
  167. - _powerc
  168. - __MC68K__
  169. - macintosh when compiling for MacOS
  170. - __INTEL__ for x86 targets
  171. - __POWERPC__
  172. LLVM:
  173. - __llvm__
  174. - __clang__
  175. */
  176. /*
  177. ** ----------------------------------------------------------------------------
  178. ** Include <limits.h> optionally
  179. ** ----------------------------------------------------------------------------
  180. */
  181. #ifdef POSH_USE_LIMITS_H
  182. # include <limits.h>
  183. #endif
  184. /*
  185. ** ----------------------------------------------------------------------------
  186. ** Determine compilation environment
  187. ** ----------------------------------------------------------------------------
  188. */
  189. #if defined __ECC || defined __ICC || defined __INTEL_COMPILER
  190. # define POSH_COMPILER_STRING "Intel C/C++"
  191. # define POSH_COMPILER_INTEL 1
  192. #endif
  193. #if ( defined __host_mips || defined __sgi ) && !defined __GNUC__
  194. # define POSH_COMPILER_STRING "MIPSpro C/C++"
  195. # define POSH_COMPILER_MIPSPRO 1
  196. #endif
  197. #if defined __hpux && !defined __GNUC__
  198. # define POSH_COMPILER_STRING "HP-UX CC"
  199. # define POSH_COMPILER_HPCC 1
  200. #endif
  201. #if defined __clang__
  202. # define POSH_COMPILER_STRING "Clang"
  203. # define POSH_COMPILER_CLANG 1
  204. #endif
  205. #if defined __GNUC__ && !defined __clang__
  206. # define POSH_COMPILER_STRING "Gnu GCC"
  207. # define POSH_COMPILER_GCC 1
  208. #endif
  209. #if defined __APPLE_CC__
  210. /* we don't define the compiler string here, let it be GNU */
  211. # define POSH_COMPILER_APPLECC 1
  212. #endif
  213. #if defined __IBMC__ || defined __IBMCPP__
  214. # define POSH_COMPILER_STRING "IBM C/C++"
  215. # define POSH_COMPILER_IBM 1
  216. #endif
  217. #if defined _MSC_VER
  218. #if !defined __clang__
  219. # define POSH_COMPILER_STRING "Microsoft Visual C++"
  220. # define POSH_COMPILER_MSVC 1
  221. #endif
  222. #endif
  223. #if defined __SUNPRO_C
  224. # define POSH_COMPILER_STRING "Sun Pro"
  225. # define POSH_COMPILER_SUN 1
  226. #endif
  227. #if defined __BORLANDC__
  228. # define POSH_COMPILER_STRING "Borland C/C++"
  229. # define POSH_COMPILER_BORLAND 1
  230. #endif
  231. #if defined __MWERKS__
  232. # define POSH_COMPILER_STRING "MetroWerks CodeWarrior"
  233. # define POSH_COMPILER_METROWERKS 1
  234. #endif
  235. #if defined __DECC || defined __DECCXX
  236. # define POSH_COMPILER_STRING "Compaq/DEC C/C++"
  237. # define POSH_COMPILER_DEC 1
  238. #endif
  239. #if defined __WATCOMC__
  240. # define POSH_COMPILER_STRING "Watcom C/C++"
  241. # define POSH_COMPILER_WATCOM 1
  242. #endif
  243. #if !defined POSH_COMPILER_STRING
  244. # define POSH_COMPILER_STRING "Unknown compiler"
  245. #endif
  246. /*
  247. ** ----------------------------------------------------------------------------
  248. ** Determine target operating system
  249. ** ----------------------------------------------------------------------------
  250. */
  251. #if defined linux || defined __linux__ || defined EMSCRIPTEN
  252. # define POSH_OS_LINUX 1
  253. # define POSH_OS_STRING "Linux"
  254. #endif
  255. #if defined __FreeBSD__
  256. # define POSH_OS_FREEBSD 1
  257. # define POSH_OS_STRING "FreeBSD"
  258. #endif
  259. #if defined __OpenBSD__
  260. # define POSH_OS_OPENBSD 1
  261. # define POSH_OS_STRING "OpenBSD"
  262. #endif
  263. #if defined __CYGWIN32__
  264. # define POSH_OS_CYGWIN32 1
  265. # define POSH_OS_STRING "Cygwin"
  266. #endif
  267. #if defined GEKKO
  268. # define POSH_OS_GAMECUBE
  269. # define __powerpc__
  270. # define POSH_OS_STRING "GameCube"
  271. #endif
  272. #if defined __MINGW32__
  273. # define POSH_OS_MINGW 1
  274. # define POSH_OS_STRING "MinGW"
  275. #endif
  276. #if defined GO32 && defined DJGPP && defined __MSDOS__
  277. # define POSH_OS_GO32 1
  278. # define POSH_OS_STRING "GO32/MS-DOS"
  279. #endif
  280. /* NOTE: make sure you use /bt=DOS if compiling for 32-bit DOS,
  281. otherwise Watcom assumes host=target */
  282. #if defined __WATCOMC__ && defined __386__ && defined __DOS__
  283. # define POSH_OS_DOS32 1
  284. # define POSH_OS_STRING "DOS/32-bit"
  285. #endif
  286. #if defined _UNICOS
  287. # define POSH_OS_UNICOS 1
  288. # define POSH_OS_STRING "UNICOS"
  289. #endif
  290. #if ( defined __MWERKS__ && defined __powerc && !defined macintosh ) || defined __APPLE_CC__ || defined macosx
  291. # define POSH_OS_OSX 1
  292. # define POSH_OS_STRING "MacOS X"
  293. #endif
  294. #if defined __sun__ || defined sun || defined __sun || defined __solaris__
  295. # if defined __SVR4 || defined __svr4__ || defined __solaris__
  296. # define POSH_OS_STRING "Solaris"
  297. # define POSH_OS_SOLARIS 1
  298. # endif
  299. # if !defined POSH_OS_STRING
  300. # define POSH_OS_STRING "SunOS"
  301. # define POSH_OS_SUNOS 1
  302. # endif
  303. #endif
  304. #if defined __sgi__ || defined sgi || defined __sgi
  305. # define POSH_OS_IRIX 1
  306. # define POSH_OS_STRING "Irix"
  307. #endif
  308. #if defined __hpux__ || defined __hpux
  309. # define POSH_OS_HPUX 1
  310. # define POSH_OS_STRING "HP-UX"
  311. #endif
  312. #if defined _AIX
  313. # define POSH_OS_AIX 1
  314. # define POSH_OS_STRING "AIX"
  315. #endif
  316. #if ( defined __alpha && defined __osf__ )
  317. # define POSH_OS_TRU64 1
  318. # define POSH_OS_STRING "Tru64"
  319. #endif
  320. #if defined __BEOS__ || defined __beos__
  321. # define POSH_OS_BEOS 1
  322. # define POSH_OS_STRING "BeOS"
  323. #endif
  324. #if defined amiga || defined amigados || defined AMIGA || defined _AMIGA
  325. # define POSH_OS_AMIGA 1
  326. # define POSH_OS_STRING "Amiga"
  327. #endif
  328. #if defined __unix__
  329. # define POSH_OS_UNIX 1
  330. # if !defined POSH_OS_STRING
  331. # define POSH_OS_STRING "Unix-like(generic)"
  332. # endif
  333. #endif
  334. #if defined _WIN32_WCE
  335. # define POSH_OS_WINCE 1
  336. # define POSH_OS_STRING "Windows CE"
  337. #endif
  338. #if defined _XBOX || defined _XBOX_VER
  339. # define POSH_OS_XBOX 1
  340. # define POSH_OS_STRING "XBOX"
  341. #endif
  342. #if defined _WIN32 || defined WIN32 || defined __NT__ || defined __WIN32__
  343. # define POSH_OS_WIN32 1
  344. # if !defined POSH_OS_XBOX
  345. # if defined _WIN64
  346. # define POSH_OS_WIN64 1
  347. # if !defined POSH_OS_STRING
  348. # define POSH_OS_STRING "Win64"
  349. # endif // !defined POSH_OS_STRING
  350. # else
  351. # if !defined POSH_OS_STRING
  352. # define POSH_OS_STRING "Win32"
  353. # endif
  354. # endif
  355. # endif
  356. #endif
  357. #if defined __palmos__
  358. # define POSH_OS_PALM 1
  359. # define POSH_OS_STRING "PalmOS"
  360. #endif
  361. #if defined THINK_C || defined macintosh
  362. # define POSH_OS_MACOS 1
  363. # define POSH_OS_STRING "MacOS"
  364. #endif
  365. /*
  366. ** -----------------------------------------------------------------------------
  367. ** Determine target CPU
  368. ** -----------------------------------------------------------------------------
  369. */
  370. #if defined GEKKO
  371. # define POSH_CPU_PPC750 1
  372. # define POSH_CPU_STRING "IBM PowerPC 750 (NGC)"
  373. #endif
  374. #if defined mc68000 || defined m68k || defined __MC68K__ || defined m68000
  375. # define POSH_CPU_68K 1
  376. # define POSH_CPU_STRING "MC68000"
  377. #endif
  378. #if defined __PPC__ || defined __POWERPC__ || defined powerpc || defined _POWER || defined __ppc__ || defined __powerpc__ || defined _M_PPC
  379. # define POSH_CPU_PPC 1
  380. # if !defined POSH_CPU_STRING
  381. # if defined __powerpc64__
  382. # define POSH_CPU_STRING "PowerPC64"
  383. # else
  384. # define POSH_CPU_STRING "PowerPC"
  385. # endif
  386. # endif
  387. #endif
  388. #if defined _CRAYT3E || defined _CRAYMPP
  389. # define POSH_CPU_CRAYT3E 1 /* target processor is a DEC Alpha 21164 used in a Cray T3E*/
  390. # define POSH_CPU_STRING "Cray T3E (Alpha 21164)"
  391. #endif
  392. #if defined CRAY || defined _CRAY && !defined _CRAYT3E
  393. # error Non-AXP Cray systems not supported
  394. #endif
  395. #if defined _SH3
  396. # define POSH_CPU_SH3 1
  397. # define POSH_CPU_STRING "Hitachi SH-3"
  398. #endif
  399. #if defined __sh4__ || defined __SH4__
  400. # define POSH_CPU_SH3 1
  401. # define POSH_CPU_SH4 1
  402. # define POSH_CPU_STRING "Hitachi SH-4"
  403. #endif
  404. #if defined __sparc__ || defined __sparc
  405. # if defined __arch64__ || defined __sparcv9 || defined __sparc_v9__
  406. # define POSH_CPU_SPARC64 1
  407. # define POSH_CPU_STRING "Sparc/64"
  408. # else
  409. # define POSH_CPU_STRING "Sparc/32"
  410. # endif
  411. # define POSH_CPU_SPARC 1
  412. #endif
  413. #if defined ARM || defined __arm__ || defined _ARM || defined _M_ARM
  414. # define POSH_CPU_STRONGARM 1
  415. # define POSH_CPU_STRING "ARM"
  416. #endif
  417. #if defined __aarch64__ || defined _M_ARM64
  418. # define POSH_CPU_AARCH64 1
  419. # define POSH_CPU_STRING "ARM64"
  420. #endif
  421. #if defined mips || defined __mips__ || defined __MIPS__ || defined _MIPS
  422. # define POSH_CPU_MIPS 1
  423. # if defined _R5900
  424. # define POSH_CPU_STRING "MIPS R5900 (PS2)"
  425. # else
  426. # define POSH_CPU_STRING "MIPS"
  427. # endif
  428. #endif
  429. #if defined __ia64 || defined _M_IA64 || defined __ia64__
  430. # define POSH_CPU_IA64 1
  431. # define POSH_CPU_STRING "IA64"
  432. #endif
  433. #if defined __riscv64 || (defined __riscv && __riscv_xlen == 64)
  434. # define POSH_CPU_RISCV64 1
  435. # define POSH_CPU_STRING "RISC-V 64"
  436. #endif
  437. #if defined __loongarch64 || (defined __loongarch__ && __loongarch_grlen == 64)
  438. # define POSH_CPU_LOONGARCH64 1
  439. # define POSH_CPU_STRING "LoongArch64"
  440. #endif
  441. #if defined __X86__ || defined __i386__ || defined i386 || defined _M_IX86 || defined __386__ || defined __x86_64__ || defined _M_X64
  442. # define POSH_CPU_X86 1
  443. # if defined __x86_64__ || defined _M_X64
  444. # define POSH_CPU_X86_64 1
  445. # endif
  446. # if defined POSH_CPU_X86_64
  447. # define POSH_CPU_STRING "AMD x86-64"
  448. # else
  449. # define POSH_CPU_STRING "Intel 386+"
  450. # endif
  451. #endif
  452. #if defined __alpha || defined alpha || defined _M_ALPHA || defined __alpha__
  453. # define POSH_CPU_AXP 1
  454. # define POSH_CPU_STRING "AXP"
  455. #endif
  456. #if defined __hppa || defined hppa
  457. # define POSH_CPU_HPPA 1
  458. # define POSH_CPU_STRING "PA-RISC"
  459. #endif
  460. #if defined EMSCRIPTEN
  461. # define POSH_CPU_EMSCRIPTEN 1
  462. # define POSH_CPU_STRING "EMSCRIPTEN"
  463. #endif
  464. #if !defined POSH_CPU_STRING
  465. # error POSH cannot determine target CPU
  466. # define POSH_CPU_STRING "Unknown" /* this is here for Doxygen's benefit */
  467. #endif
  468. /*
  469. ** -----------------------------------------------------------------------------
  470. ** Attempt to autodetect building for embedded on Sony PS2
  471. ** -----------------------------------------------------------------------------
  472. */
  473. #if !defined POSH_OS_STRING
  474. # if !defined FORCE_DOXYGEN
  475. # define POSH_OS_EMBEDDED 1
  476. # endif
  477. # if defined _R5900
  478. # define POSH_OS_STRING "Sony PS2(embedded)"
  479. # else
  480. # define POSH_OS_STRING "Embedded/Unknown"
  481. # endif
  482. #endif
  483. /*
  484. ** ---------------------------------------------------------------------------
  485. ** Handle cdecl, stdcall, fastcall, etc.
  486. ** ---------------------------------------------------------------------------
  487. */
  488. #if defined POSH_CPU_X86 && !defined POSH_CPU_X86_64
  489. # if defined __GNUC__
  490. # define POSH_CDECL __attribute__((cdecl))
  491. # define POSH_STDCALL __attribute__((stdcall))
  492. # define POSH_FASTCALL __attribute__((fastcall))
  493. # elif ( defined _MSC_VER || defined __WATCOMC__ || defined __BORLANDC__ || defined __MWERKS__ )
  494. # define POSH_CDECL __cdecl
  495. # define POSH_STDCALL __stdcall
  496. # define POSH_FASTCALL __fastcall
  497. # endif
  498. #else
  499. # define POSH_CDECL
  500. # define POSH_STDCALL
  501. # define POSH_FASTCALL
  502. #endif
  503. /*
  504. ** ---------------------------------------------------------------------------
  505. ** Define POSH_IMPORTEXPORT signature based on POSH_DLL and POSH_BUILDING_LIB
  506. ** ---------------------------------------------------------------------------
  507. */
  508. /*
  509. ** We undefine this so that multiple inclusions will work
  510. */
  511. #if defined POSH_IMPORTEXPORT
  512. # undef POSH_IMPORTEXPORT
  513. #endif
  514. #if defined POSH_DLL
  515. # if defined POSH_OS_WIN32
  516. # if defined _MSC_VER
  517. # if ( _MSC_VER >= 800 )
  518. # if defined POSH_BUILDING_LIB
  519. # define POSH_IMPORTEXPORT __declspec( dllexport )
  520. # else
  521. # define POSH_IMPORTEXPORT __declspec( dllimport )
  522. # endif
  523. # else
  524. # if defined POSH_BUILDING_LIB
  525. # define POSH_IMPORTEXPORT __export
  526. # else
  527. # define POSH_IMPORTEXPORT
  528. # endif
  529. # endif
  530. # endif /* defined _MSC_VER */
  531. # if defined __BORLANDC__
  532. # if ( __BORLANDC__ >= 0x500 )
  533. # if defined POSH_BUILDING_LIB
  534. # define POSH_IMPORTEXPORT __declspec( dllexport )
  535. # else
  536. # define POSH_IMPORTEXPORT __declspec( dllimport )
  537. # endif
  538. # else
  539. # if defined POSH_BUILDING_LIB
  540. # define POSH_IMPORTEXPORT __export
  541. # else
  542. # define POSH_IMPORTEXPORT
  543. # endif
  544. # endif
  545. # endif /* defined __BORLANDC__ */
  546. /* for all other compilers, we're just making a blanket assumption */
  547. # if defined __GNUC__ || defined __WATCOMC__ || defined __MWERKS__
  548. # if defined POSH_BUILDING_LIB
  549. # define POSH_IMPORTEXPORT __declspec( dllexport )
  550. # else
  551. # define POSH_IMPORTEXPORT __declspec( dllimport )
  552. # endif
  553. # endif /* all other compilers */
  554. # if !defined POSH_IMPORTEXPORT
  555. # error Building DLLs not supported on this compiler ([email protected] if you know how)
  556. # endif
  557. # endif /* defined POSH_OS_WIN32 */
  558. #endif
  559. /* On pretty much everything else, we can thankfully just ignore this */
  560. #if !defined POSH_IMPORTEXPORT
  561. # define POSH_IMPORTEXPORT
  562. #endif
  563. #if defined FORCE_DOXYGEN
  564. # define POSH_DLL
  565. # define POSH_BUILDING_LIB
  566. # undef POSH_DLL
  567. # undef POSH_BUILDING_LIB
  568. #endif
  569. /*
  570. ** ----------------------------------------------------------------------------
  571. ** (Re)define POSH_PUBLIC_API export signature
  572. ** ----------------------------------------------------------------------------
  573. */
  574. #ifdef POSH_PUBLIC_API
  575. # undef POSH_PUBLIC_API
  576. #endif
  577. #if ( ( defined _MSC_VER ) && ( _MSC_VER < 800 ) ) || ( defined __BORLANDC__ && ( __BORLANDC__ < 0x500 ) )
  578. # define POSH_PUBLIC_API(rtype) extern rtype POSH_IMPORTEXPORT
  579. #else
  580. # define POSH_PUBLIC_API(rtype) extern POSH_IMPORTEXPORT rtype
  581. #endif
  582. /*
  583. ** ----------------------------------------------------------------------------
  584. ** Try to infer endianess. Basically we just go through the CPUs we know are
  585. ** little endian, and assume anything that isn't one of those is big endian.
  586. ** As a sanity check, we also do this with operating systems we know are
  587. ** little endian, such as Windows. Some processors are bi-endian, such as
  588. ** the MIPS series, so we have to be careful about those.
  589. ** ----------------------------------------------------------------------------
  590. */
  591. #if defined POSH_CPU_X86 || defined POSH_CPU_AXP || defined POSH_CPU_STRONGARM || defined POSH_CPU_AARCH64 || defined POSH_CPU_RISCV64 || defined POSH_CPU_LOONGARCH64 || defined POSH_OS_WIN32 || defined POSH_OS_WINCE || defined __MIPSEL__ || defined POSH_CPU_EMSCRIPTEN
  592. # define POSH_ENDIAN_STRING "little"
  593. # define POSH_LITTLE_ENDIAN 1
  594. #else
  595. # define POSH_ENDIAN_STRING "big"
  596. # define POSH_BIG_ENDIAN 1
  597. #endif
  598. #if defined FORCE_DOXYGEN
  599. # define POSH_LITTLE_ENDIAN
  600. #endif
  601. /*
  602. ** ----------------------------------------------------------------------------
  603. ** Cross-platform compile time assertion macro
  604. ** ----------------------------------------------------------------------------
  605. */
  606. #define POSH_COMPILE_TIME_ASSERT(name, x) typedef int _POSH_dummy_ ## name[(x) ? 1 : -1 ]
  607. /*
  608. ** ----------------------------------------------------------------------------
  609. ** 64-bit Integer
  610. **
  611. ** We don't require 64-bit support, nor do we emulate its functionality, we
  612. ** simply export it if it's available. Since we can't count on <limits.h>
  613. ** for 64-bit support, we ignore the POSH_USE_LIMITS_H directive.
  614. ** ----------------------------------------------------------------------------
  615. */
  616. #if defined ( __LP64__ ) || defined ( __powerpc64__ ) || defined POSH_CPU_SPARC64
  617. # define POSH_64BIT_INTEGER 1
  618. typedef long posh_i64_t;
  619. typedef unsigned long posh_u64_t;
  620. # define POSH_I64( x ) ((posh_i64_t)x)
  621. # define POSH_U64( x ) ((posh_u64_t)x)
  622. # define POSH_I64_PRINTF_PREFIX "l"
  623. #elif defined _MSC_VER || defined __BORLANDC__ || defined __WATCOMC__ || ( defined __alpha && defined __DECC )
  624. # define POSH_64BIT_INTEGER 1
  625. typedef __int64 posh_i64_t;
  626. typedef unsigned __int64 posh_u64_t;
  627. # define POSH_I64( x ) ((posh_i64_t)(x##i64))
  628. # define POSH_U64( x ) ((posh_u64_t)(x##ui64))
  629. # define POSH_I64_PRINTF_PREFIX "I64"
  630. #elif defined __GNUC__ || defined __MWERKS__ || defined __SUNPRO_C || defined __SUNPRO_CC || defined __APPLE_CC__ || defined POSH_OS_IRIX || defined _LONG_LONG || defined _CRAYC
  631. # define POSH_64BIT_INTEGER 1
  632. typedef long long posh_i64_t;
  633. typedef unsigned long long posh_u64_t;
  634. # define POSH_U64( x ) ((posh_u64_t)(x##LL))
  635. # define POSH_I64( x ) ((posh_i64_t)(x##LL))
  636. # define POSH_I64_PRINTF_PREFIX "ll"
  637. #endif
  638. /* hack */
  639. /*#ifdef __MINGW32__
  640. #undef POSH_I64
  641. #undef POSH_U64
  642. #undef POSH_I64_PRINTF_PREFIX
  643. #define POSH_I64( x ) ((posh_i64_t)x)
  644. #define POSH_U64( x ) ((posh_u64_t)x)
  645. #define POSH_I64_PRINTF_PREFIX "I64"
  646. #endif*/
  647. #ifdef FORCE_DOXYGEN
  648. typedef long long posh_i64_t;
  649. typedef unsigned long posh_u64_t;
  650. # define POSH_64BIT_INTEGER
  651. # define POSH_I64_PRINTF_PREFIX
  652. # define POSH_I64(x)
  653. # define POSH_U64(x)
  654. #endif
  655. /** Minimum value for a 64-bit signed integer */
  656. #define POSH_I64_MIN POSH_I64(0x8000000000000000)
  657. /** Maximum value for a 64-bit signed integer */
  658. #define POSH_I64_MAX POSH_I64(0x7FFFFFFFFFFFFFFF)
  659. /** Minimum value for a 64-bit unsigned integer */
  660. #define POSH_U64_MIN POSH_U64(0)
  661. /** Maximum value for a 64-bit unsigned integer */
  662. #define POSH_U64_MAX POSH_U64(0xFFFFFFFFFFFFFFFF)
  663. /* ----------------------------------------------------------------------------
  664. ** Basic Sized Types
  665. **
  666. ** These types are expected to be EXACTLY sized so you can use them for
  667. ** serialization.
  668. ** ----------------------------------------------------------------------------
  669. */
  670. #define POSH_FALSE 0
  671. #define POSH_TRUE 1
  672. typedef int posh_bool_t;
  673. typedef unsigned char posh_byte_t;
  674. /* NOTE: These assume that CHAR_BIT is 8!! */
  675. typedef unsigned char posh_u8_t;
  676. typedef signed char posh_i8_t;
  677. #if defined POSH_USE_LIMITS_H
  678. # if CHAR_BITS > 8
  679. # error This machine uses 9-bit characters. This is a warning, you can comment this out now.
  680. # endif /* CHAR_BITS > 8 */
  681. /* 16-bit */
  682. # if ( USHRT_MAX == 65535 )
  683. typedef unsigned short posh_u16_t;
  684. typedef short posh_i16_t;
  685. # else
  686. /* Yes, in theory there could still be a 16-bit character type and shorts are
  687. 32-bits in size...if you find such an architecture, let me know =P */
  688. # error No 16-bit type found
  689. # endif
  690. /* 32-bit */
  691. # if ( INT_MAX == 2147483647 )
  692. typedef unsigned posh_u32_t;
  693. typedef int posh_i32_t;
  694. # elif ( LONG_MAX == 2147483647 )
  695. typedef unsigned long posh_u32_t;
  696. typedef long posh_i32_t;
  697. # else
  698. error No 32-bit type found
  699. # endif
  700. #else /* POSH_USE_LIMITS_H */
  701. typedef unsigned short posh_u16_t;
  702. typedef short posh_i16_t;
  703. # if !defined POSH_OS_PALM
  704. typedef unsigned posh_u32_t;
  705. typedef int posh_i32_t;
  706. # else
  707. typedef unsigned long posh_u32_t;
  708. typedef long posh_i32_t;
  709. # endif
  710. #endif
  711. /** Minimum value for a byte */
  712. #define POSH_BYTE_MIN 0
  713. /** Maximum value for an 8-bit unsigned value */
  714. #define POSH_BYTE_MAX 255
  715. /** Minimum value for a byte */
  716. #define POSH_I16_MIN ( ( posh_i16_t ) 0x8000 )
  717. /** Maximum value for a 16-bit signed value */
  718. #define POSH_I16_MAX ( ( posh_i16_t ) 0x7FFF )
  719. /** Minimum value for a 16-bit unsigned value */
  720. #define POSH_U16_MIN 0
  721. /** Maximum value for a 16-bit unsigned value */
  722. #define POSH_U16_MAX ( ( posh_u16_t ) 0xFFFF )
  723. /** Minimum value for a 32-bit signed value */
  724. #define POSH_I32_MIN ( ( posh_i32_t ) 0x80000000 )
  725. /** Maximum value for a 32-bit signed value */
  726. #define POSH_I32_MAX ( ( posh_i32_t ) 0x7FFFFFFF )
  727. /** Minimum value for a 32-bit unsigned value */
  728. #define POSH_U32_MIN 0
  729. /** Maximum value for a 32-bit unsigned value */
  730. #define POSH_U32_MAX ( ( posh_u32_t ) 0xFFFFFFFF )
  731. /*
  732. ** ----------------------------------------------------------------------------
  733. ** Sanity checks on expected sizes
  734. ** ----------------------------------------------------------------------------
  735. */
  736. #if !defined FORCE_DOXYGEN
  737. POSH_COMPILE_TIME_ASSERT(posh_byte_t, sizeof(posh_byte_t) == 1);
  738. POSH_COMPILE_TIME_ASSERT(posh_u8_t, sizeof(posh_u8_t) == 1);
  739. POSH_COMPILE_TIME_ASSERT(posh_i8_t, sizeof(posh_i8_t) == 1);
  740. POSH_COMPILE_TIME_ASSERT(posh_u16_t, sizeof(posh_u16_t) == 2);
  741. POSH_COMPILE_TIME_ASSERT(posh_i16_t, sizeof(posh_i16_t) == 2);
  742. POSH_COMPILE_TIME_ASSERT(posh_u32_t, sizeof(posh_u32_t) == 4);
  743. POSH_COMPILE_TIME_ASSERT(posh_i32_t, sizeof(posh_i32_t) == 4);
  744. #if !defined POSH_NO_FLOAT
  745. POSH_COMPILE_TIME_ASSERT(posh_testfloat_t, sizeof(float)==4 );
  746. POSH_COMPILE_TIME_ASSERT(posh_testdouble_t, sizeof(double)==8);
  747. #endif
  748. #if defined POSH_64BIT_INTEGER
  749. POSH_COMPILE_TIME_ASSERT(posh_u64_t, sizeof(posh_u64_t) == 8);
  750. POSH_COMPILE_TIME_ASSERT(posh_i64_t, sizeof(posh_i64_t) == 8);
  751. #endif
  752. #endif
  753. /*
  754. ** ----------------------------------------------------------------------------
  755. ** 64-bit pointer support
  756. ** ----------------------------------------------------------------------------
  757. */
  758. #if defined POSH_CPU_AXP && ( defined POSH_OS_TRU64 || defined POSH_OS_LINUX )
  759. # define POSH_64BIT_POINTER 1
  760. #endif
  761. #if defined POSH_CPU_X86_64 && defined POSH_OS_LINUX
  762. # define POSH_64BIT_POINTER 1
  763. #endif
  764. #if defined POSH_CPU_SPARC64 || defined POSH_OS_WIN64 || defined __64BIT__ || defined __LP64 || defined _LP64 || defined __LP64__ || defined _ADDR64 || defined _CRAYC
  765. # define POSH_64BIT_POINTER 1
  766. #endif
  767. #if defined POSH_64BIT_POINTER
  768. POSH_COMPILE_TIME_ASSERT( posh_64bit_pointer, sizeof( void * ) == 8 );
  769. #elif !defined FORCE_DOXYGEN
  770. /* if this assertion is hit then you're on a system that either has 64-bit
  771. addressing and we didn't catch it, or you're on a system with 16-bit
  772. pointers. In the latter case, POSH doesn't actually care, we're just
  773. triggering this assertion to make sure you're aware of the situation,
  774. so feel free to delete it.
  775. If this assertion is triggered on a known 32 or 64-bit platform,
  776. please let us know ([email protected]) */
  777. POSH_COMPILE_TIME_ASSERT( posh_32bit_pointer, sizeof( void * ) == 4 );
  778. #endif
  779. #if defined FORCE_DOXYGEN
  780. # define POSH_64BIT_POINTER
  781. #endif
  782. /*
  783. ** ----------------------------------------------------------------------------
  784. ** POSH Utility Functions
  785. **
  786. ** These are optional POSH utility functions that are not required if you don't
  787. ** need anything except static checking of your host and target environment.
  788. **
  789. ** These functions are NOT wrapped with POSH_PUBLIC_API because I didn't want
  790. ** to enforce their export if your own library is only using them internally.
  791. ** ----------------------------------------------------------------------------
  792. */
  793. #ifdef __cplusplus
  794. extern "C" {
  795. #endif
  796. const char *POSH_GetArchString( void );
  797. #if !defined POSH_NO_FLOAT
  798. posh_u32_t POSH_LittleFloatBits( float f );
  799. posh_u32_t POSH_BigFloatBits( float f );
  800. float POSH_FloatFromLittleBits( posh_u32_t bits );
  801. float POSH_FloatFromBigBits( posh_u32_t bits );
  802. void POSH_DoubleBits( double d, posh_byte_t dst[ 8 ] );
  803. double POSH_DoubleFromBits( const posh_byte_t src[ 8 ] );
  804. /* unimplemented
  805. float *POSH_WriteFloatToLittle( void *dst, float f );
  806. float *POSH_WriteFloatToBig( void *dst, float f );
  807. float POSH_ReadFloatFromLittle( const void *src );
  808. float POSH_ReadFloatFromBig( const void *src );
  809. double *POSH_WriteDoubleToLittle( void *dst, double d );
  810. double *POSH_WriteDoubleToBig( void *dst, double d );
  811. double POSH_ReadDoubleFromLittle( const void *src );
  812. double POSH_ReadDoubleFromBig( const void *src );
  813. */
  814. #endif /* !defined POSH_NO_FLOAT */
  815. #if defined FORCE_DOXYGEN
  816. # define POSH_NO_FLOAT
  817. # undef POSH_NO_FLOAT
  818. #endif
  819. extern posh_u16_t POSH_SwapU16( posh_u16_t u );
  820. extern posh_i16_t POSH_SwapI16( posh_i16_t u );
  821. extern posh_u32_t POSH_SwapU32( posh_u32_t u );
  822. extern posh_i32_t POSH_SwapI32( posh_i32_t u );
  823. #if defined POSH_64BIT_INTEGER
  824. extern posh_u64_t POSH_SwapU64( posh_u64_t u );
  825. extern posh_i64_t POSH_SwapI64( posh_i64_t u );
  826. #endif /*POSH_64BIT_INTEGER */
  827. extern posh_u16_t *POSH_WriteU16ToLittle( void *dst, posh_u16_t value );
  828. extern posh_i16_t *POSH_WriteI16ToLittle( void *dst, posh_i16_t value );
  829. extern posh_u32_t *POSH_WriteU32ToLittle( void *dst, posh_u32_t value );
  830. extern posh_i32_t *POSH_WriteI32ToLittle( void *dst, posh_i32_t value );
  831. extern posh_u16_t *POSH_WriteU16ToBig( void *dst, posh_u16_t value );
  832. extern posh_i16_t *POSH_WriteI16ToBig( void *dst, posh_i16_t value );
  833. extern posh_u32_t *POSH_WriteU32ToBig( void *dst, posh_u32_t value );
  834. extern posh_i32_t *POSH_WriteI32ToBig( void *dst, posh_i32_t value );
  835. extern posh_u16_t POSH_ReadU16FromLittle( const void *src );
  836. extern posh_i16_t POSH_ReadI16FromLittle( const void *src );
  837. extern posh_u32_t POSH_ReadU32FromLittle( const void *src );
  838. extern posh_i32_t POSH_ReadI32FromLittle( const void *src );
  839. extern posh_u16_t POSH_ReadU16FromBig( const void *src );
  840. extern posh_i16_t POSH_ReadI16FromBig( const void *src );
  841. extern posh_u32_t POSH_ReadU32FromBig( const void *src );
  842. extern posh_i32_t POSH_ReadI32FromBig( const void *src );
  843. #if defined POSH_64BIT_INTEGER
  844. extern posh_u64_t *POSH_WriteU64ToLittle( void *dst, posh_u64_t value );
  845. extern posh_i64_t *POSH_WriteI64ToLittle( void *dst, posh_i64_t value );
  846. extern posh_u64_t *POSH_WriteU64ToBig( void *dst, posh_u64_t value );
  847. extern posh_i64_t *POSH_WriteI64ToBig( void *dst, posh_i64_t value );
  848. extern posh_u64_t POSH_ReadU64FromLittle( const void *src );
  849. extern posh_i64_t POSH_ReadI64FromLittle( const void *src );
  850. extern posh_u64_t POSH_ReadU64FromBig( const void *src );
  851. extern posh_i64_t POSH_ReadI64FromBig( const void *src );
  852. #endif /* POSH_64BIT_INTEGER */
  853. #if defined POSH_LITTLE_ENDIAN
  854. # define POSH_LittleU16(x) (x)
  855. # define POSH_LittleU32(x) (x)
  856. # define POSH_LittleI16(x) (x)
  857. # define POSH_LittleI32(x) (x)
  858. # if defined POSH_64BIT_INTEGER
  859. # define POSH_LittleU64(x) (x)
  860. # define POSH_LittleI64(x) (x)
  861. # endif /* defined POSH_64BIT_INTEGER */
  862. # define POSH_BigU16(x) POSH_SwapU16(x)
  863. # define POSH_BigU32(x) POSH_SwapU32(x)
  864. # define POSH_BigI16(x) POSH_SwapI16(x)
  865. # define POSH_BigI32(x) POSH_SwapI32(x)
  866. # if defined POSH_64BIT_INTEGER
  867. # define POSH_BigU64(x) POSH_SwapU64(x)
  868. # define POSH_BigI64(x) POSH_SwapI64(x)
  869. # endif /* defined POSH_64BIT_INTEGER */
  870. #else
  871. # define POSH_BigU16(x) (x)
  872. # define POSH_BigU32(x) (x)
  873. # define POSH_BigI16(x) (x)
  874. # define POSH_BigI32(x) (x)
  875. # if defined POSH_64BIT_INTEGER
  876. # define POSH_BigU64(x) (x)
  877. # define POSH_BigI64(x) (x)
  878. # endif /* POSH_64BIT_INTEGER */
  879. # define POSH_LittleU16(x) POSH_SwapU16(x)
  880. # define POSH_LittleU32(x) POSH_SwapU32(x)
  881. # define POSH_LittleI16(x) POSH_SwapI16(x)
  882. # define POSH_LittleI32(x) POSH_SwapI32(x)
  883. # if defined POSH_64BIT_INTEGER
  884. # define POSH_LittleU64(x) POSH_SwapU64(x)
  885. # define POSH_LittleI64(x) POSH_SwapI64(x)
  886. # endif /* POSH_64BIT_INTEGER */
  887. #endif
  888. #ifdef __cplusplus
  889. }
  890. #endif