numbers.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2015-2021 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. # ifndef INT128_MAX
  49. # if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ == 16
  50. typedef __int128_t int128_t;
  51. typedef __uint128_t uint128_t;
  52. # define INT128_MIN __MININT__(int128_t)
  53. # define INT128_MAX __MAXINT__(int128_t)
  54. # define UINT128_MAX __MAXUINT__(uint128_t)
  55. # endif
  56. # endif
  57. # ifndef SIZE_MAX
  58. # define SIZE_MAX __MAXUINT__(size_t)
  59. # endif
  60. #endif