numbers.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef OSSL_INTERNAL_NUMBERS_H
  10. # define OSSL_INTERNAL_NUMBERS_H
  11. # pragma once
  12. # include <limits.h>
  13. # if (-1 & 3) == 0x03 /* Two's complement */
  14. # define __MAXUINT__(T) ((T) -1)
  15. # define __MAXINT__(T) ((T) ((((T) 1) << ((sizeof(T) * CHAR_BIT) - 1)) ^ __MAXUINT__(T)))
  16. # define __MININT__(T) (-__MAXINT__(T) - 1)
  17. # elif (-1 & 3) == 0x02 /* One's complement */
  18. # define __MAXUINT__(T) (((T) -1) + 1)
  19. # define __MAXINT__(T) ((T) ((((T) 1) << ((sizeof(T) * CHAR_BIT) - 1)) ^ __MAXUINT__(T)))
  20. # define __MININT__(T) (-__MAXINT__(T))
  21. # elif (-1 & 3) == 0x01 /* Sign/magnitude */
  22. # define __MAXINT__(T) ((T) (((((T) 1) << ((sizeof(T) * CHAR_BIT) - 2)) - 1) | (((T) 1) << ((sizeof(T) * CHAR_BIT) - 2))))
  23. # define __MAXUINT__(T) ((T) (__MAXINT__(T) | (((T) 1) << ((sizeof(T) * CHAR_BIT) - 1))))
  24. # define __MININT__(T) (-__MAXINT__(T))
  25. # else
  26. # error "do not know the integer encoding on this architecture"
  27. # endif
  28. # ifndef INT8_MAX
  29. # define INT8_MIN __MININT__(int8_t)
  30. # define INT8_MAX __MAXINT__(int8_t)
  31. # define UINT8_MAX __MAXUINT__(uint8_t)
  32. # endif
  33. # ifndef INT16_MAX
  34. # define INT16_MIN __MININT__(int16_t)
  35. # define INT16_MAX __MAXINT__(int16_t)
  36. # define UINT16_MAX __MAXUINT__(uint16_t)
  37. # endif
  38. # ifndef INT32_MAX
  39. # define INT32_MIN __MININT__(int32_t)
  40. # define INT32_MAX __MAXINT__(int32_t)
  41. # define UINT32_MAX __MAXUINT__(uint32_t)
  42. # endif
  43. # ifndef INT64_MAX
  44. # define INT64_MIN __MININT__(int64_t)
  45. # define INT64_MAX __MAXINT__(int64_t)
  46. # define UINT64_MAX __MAXUINT__(uint64_t)
  47. # endif
  48. /*
  49. * 64-bit processor with LP64 ABI
  50. */
  51. # ifdef SIXTY_FOUR_BIT_LONG
  52. # ifndef UINT32_C
  53. # define UINT32_C(c) (c)
  54. # endif
  55. # ifndef UINT64_C
  56. # define UINT64_C(c) (c##UL)
  57. # endif
  58. # endif
  59. /*
  60. * 64-bit processor other than LP64 ABI
  61. */
  62. # ifdef SIXTY_FOUR_BIT
  63. # ifndef UINT32_C
  64. # define UINT32_C(c) (c##UL)
  65. # endif
  66. # ifndef UINT64_C
  67. # define UINT64_C(c) (c##ULL)
  68. # endif
  69. # endif
  70. # ifndef INT128_MAX
  71. # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16
  72. typedef __int128_t int128_t;
  73. typedef __uint128_t uint128_t;
  74. # define INT128_MIN __MININT__(int128_t)
  75. # define INT128_MAX __MAXINT__(int128_t)
  76. # define UINT128_MAX __MAXUINT__(uint128_t)
  77. # endif
  78. # endif
  79. # ifndef SIZE_MAX
  80. # define SIZE_MAX __MAXUINT__(size_t)
  81. # endif
  82. # ifndef OSSL_INTMAX_MAX
  83. # define OSSL_INTMAX_MIN __MININT__(ossl_intmax_t)
  84. # define OSSL_INTMAX_MAX __MAXINT__(ossl_intmax_t)
  85. # define OSSL_UINTMAX_MAX __MAXUINT__(ossl_uintmax_t)
  86. # endif
  87. #endif