1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
/* See LICENSE file for copyright and license details. */
#ifndef LIBJ2_H
#define LIBJ2_H
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdarg.h>
#if 1
# if defined(__GNUC__)
# define LIBJ2_USE_GCC_INTRINSIC_FUNCTIONS_
# if !defined(__clang__)
# define LIBJ2_USE_GCC_PARITYG_
# endif
# endif
#endif
#if defined(__GNUC__)
# define LIBJ2_PURE_ __attribute__((__pure__))
#else
# define LIBJ2_PURE_
#endif
/**
* The number of bits in an `uintmax_t`
*
* @since 1.0
*/
#define LIBJ2_JU_BIT ((unsigned)CHAR_BIT * (unsigned)sizeof(uintmax_t))
/**
* The number of bits in a `struct libj2_j2u`
*
* @since 1.0
*/
#define LIBJ2_J2U_BIT (2U * LIBJ2_JU_BIT)
/**
* The number of bits in a `struct libj2_j2i`,
* including the sign bit
*
* @since 1.1
*/
#define LIBJ2_J2I_BIT LIBJ2_J2U_BIT
/**
* The number of value bits in a `struct libj2_j2i`,
* that is, the number of bits excluding the sign
* bit
*
* @since 1.1
*/
#define LIBJ2_J2I_VBIT (LIBJ2_J2I_BIT - 1U)
/**
* Unsigned double-maximum precision integer
*
* If `uintmax_t` is a 64-bit type, this `struct` is a 128-bit type
*
* @since 1.0
*/
struct libj2_j2u {
/**
* Most significant half
*/
uintmax_t high;
/**
* Least significant half
*/
uintmax_t low;
};
/**
* Signed double-maximum precision integer,
* using two's complement
*
* libj2 does not implement bitwise operations,
* bit-scanning, or bit-rotation operations
* for this type, but a `struct libj2_j2i *`
* can safetly be casted to `struct libj2_j2u *`
* for such operators as well as the for
* unsigned bit shifting operations
*
* If `a` is the maximum representable value,
* `-a - 1` is the minimum representable value;
* there is only one zero value (has no sign)
*
* @since 1.1
*/
struct libj2_j2i {
/**
* Most significant half, of which the most
* significant bit is the sign bit
*/
uintmax_t high;
/**
* Least significant half
*/
uintmax_t low;
};
/**
* Arithmetic overflow prediction result
*
* Use by some functions for which prediction
* is costly in edge cases, and thus stopped
* as soon as such an edge case is detected
*
* @since 1.0
*/
enum libj2_overflow {
/**
* Where will not be an overflow
*/
LIBJ2_NO_OVERFLOW = 0,
/**
* Where will be an overflow
*/
LIBJ2_OVERFLOW = 1,
/**
* Overflow prediction stopped
* (due to high cost)
*/
LIBJ2_OVERFLOW_UNKNOWN
};
#include "libj2/constants.h"
#include "libj2/signum.h"
#include "libj2/constructors.h"
#include "libj2/unsigned-comparsion.h"
#include "libj2/signed-comparsion.h"
#include "libj2/mixed-comparsion.h"
#include "libj2/bitwise-logic.h"
#include "libj2/sign-shifting.h"
#include "libj2/bit-shifting.h"
#include "libj2/bit-scanning.h"
#include "libj2/addition.h" /* TODO add signed versions */
#include "libj2/subtraction.h" /* TODO add signed versions */
#include "libj2/multiplication.h" /* TODO add signed versions */
#include "libj2/division.h" /* TODO add signed versions */
#include "libj2/saturated-math.h" /* TODO add signed versions */
#include "libj2/strings.h"
#if defined(LIBJ2_USE_GCC_INTRINSIC_FUNCTIONS_)
# undef LIBJ2_USE_GCC_INTRINSIC_FUNCTIONS_
#endif
#undef LIBJ2_PURE_
#endif
|