lbn.h 4.1 KB

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