2
0

lbn.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 1995 Colin Plumb. All rights reserved.
  3. * For licensing and other legal details, see the file legal.c.
  4. *
  5. * lbn.h - Low-level bignum header.
  6. * Defines various word sizes and useful macros.
  7. * TODO: Rewrite this to use <stdint.h> and/or <inttypes.h>
  8. */
  9. #ifndef LBN_H
  10. #define LBN_H
  11. #ifndef HAVE_CONFIG_H
  12. #define HAVE_CONFIG_H 0
  13. #endif
  14. #if HAVE_CONFIG_H
  15. #include "bnconfig.h"
  16. #endif
  17. /*
  18. * Some compilers complain about #if FOO if FOO isn't defined,
  19. * so do the ANSI-mandated thing explicitly...
  20. */
  21. #ifndef NO_LIMITS_H
  22. #define NO_LIMITS_H 0
  23. #endif
  24. /* Make sure we have 8-bit bytes */
  25. #if !NO_LIMITS_H
  26. #include <limits.h>
  27. #if UCHAR_MAX != 0xff || CHAR_BIT != 8
  28. #error The bignum library requires 8-bit unsigned characters.
  29. #endif
  30. #endif /* !NO_LIMITS_H */
  31. #ifdef BNINCLUDE /* If this is defined as, say, foo.h */
  32. #define STR(x) #x /* STR(BNINCLUDE) -> "BNINCLUDE" */
  33. #define XSTR(x) STR(x) /* XSTR(BNINCLUDE) -> STR(foo.h) -> "foo.h" */
  34. #include XSTR(BNINCLUDE) /* #include "foo.h" */
  35. #undef XSTR
  36. #undef STR
  37. #endif
  38. /* Do we want bnYield()? */
  39. #ifndef BNYIELD
  40. #define BNYIELD 0
  41. #endif
  42. /* Figure out the endianness */
  43. /* Error if more than one is defined */
  44. #if defined(BN_BIG_ENDIAN) && defined(BN_LITTLE_ENDIAN)
  45. #error Only one of BN_BIG_ENDIAN or BN_LITTLE_ENDIAN may be defined
  46. #endif
  47. /*
  48. * If no preference is stated, little-endian C code is slightly more
  49. * efficient, so prefer that. (The endianness here does NOT have to
  50. * match the machine's native byte sex; the library's C code will work
  51. * either way. The flexibility is allowed for assembly routines
  52. * that do care.
  53. */
  54. #if !defined(BN_BIG_ENDIAN) && !defined(BN_LITTLE_ENDIAN)
  55. #define BN_LITTLE_ENDIAN 1
  56. #endif /* !BN_BIG_ENDIAN && !BN_LITTLE_ENDIAN */
  57. /* Macros to choose between big and little endian */
  58. #if defined(BN_BIG_ENDIAN)
  59. #define BIG(b) b
  60. #define LITTLE(l) /*nothing*/
  61. #define BIGLITTLE(b,l) b
  62. #elif BN_LITTLE_ENDIAN
  63. #define BIG(b) /*nothing*/
  64. #define LITTLE(l) l
  65. #define BIGLITTLE(b,l) l
  66. #else
  67. #error One of BN_BIG_ENDIAN or BN_LITTLE_ENDIAN must be defined as 1
  68. #endif
  69. /*
  70. * Find a 16-bit unsigned type.
  71. * Unsigned short is preferred over unsigned int to make the type chosen
  72. * by this file more stable on platforms (such as many 68000 compilers)
  73. * which support both 16- and 32-bit ints.
  74. */
  75. #ifndef BNWORD16
  76. #ifndef USHRT_MAX /* No <limits.h> available - guess */
  77. typedef unsigned short bnword16;
  78. #define BNWORD16 bnword16
  79. #elif USHRT_MAX == 0xffff
  80. typedef unsigned short bnword16;
  81. #define BNWORD16 bnword16
  82. #elif UINT_MAX == 0xffff
  83. typedef unsigned bnword16;
  84. #define BNWORD16 bnword16
  85. #endif
  86. #endif /* BNWORD16 */
  87. /*
  88. * Find a 32-bit unsigned type.
  89. * Unsigned long is preferred over unsigned int to make the type chosen
  90. * by this file more stable on platforms (such as many 68000 compilers)
  91. * which support both 16- and 32-bit ints.
  92. */
  93. #ifndef BNWORD32
  94. #ifndef ULONG_MAX /* No <limits.h> available - guess */
  95. typedef unsigned long bnword32;
  96. #define BNWORD32 bnword32
  97. #elif ULONG_MAX == 0xfffffffful
  98. typedef unsigned long bnword32;
  99. #define BNWORD32 bnword32
  100. #elif UINT_MAX == 0xffffffff
  101. typedef unsigned bnword32;
  102. #define BNWORD32 bnword32
  103. #elif USHRT_MAX == 0xffffffff
  104. typedef unsigned short bnword32;
  105. #define BNWORD32 bnword32
  106. #endif
  107. #endif /* BNWORD16 */
  108. /*
  109. * Find a 64-bit unsigned type.
  110. * The conditions here are more complicated to avoid using numbers that
  111. * will choke lesser preprocessors (like 0xffffffffffffffff) unless
  112. * we're reasonably certain that they'll be acceptable.
  113. */
  114. #if !defined(BNWORD64) && ULONG_MAX > 0xfffffffful
  115. #if ULONG_MAX == 0xffffffffffffffff
  116. typedef unsigned long bnword64;
  117. #define BNWORD64 bnword64
  118. #endif
  119. #endif
  120. /*
  121. * I would test the value of unsigned long long, but some *preprocessors*
  122. * don't constants that long even if the compiler can accept them, so it
  123. * doesn't work reliably. So cross our fingers and hope that it's a 64-bit
  124. * type.
  125. *
  126. * GCC uses ULONG_LONG_MAX. Solaris uses ULLONG_MAX. IRIX uses ULONGLONG_MAX.
  127. * Are there any other names for this?
  128. */
  129. #if !defined(BNWORD64) && \
  130. (defined(ULONG_LONG_MAX) || defined (ULLONG_MAX) || defined(ULONGLONG_MAX) || defined(ULONG_MAX))
  131. typedef unsigned long long bnword64;
  132. #define BNWORD64 bnword64
  133. #endif
  134. /*Workaround for windows for now. */
  135. #ifdef WIN32
  136. typedef unsigned long long bnword64;
  137. #define BNWORD64 bnword64
  138. #endif
  139. /* We don't even try to find a 128-bit type at the moment */
  140. #endif /* !LBN_H */