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