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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST
extern inline void libj2_j2i_rsh(struct libj2_j2i *a, unsigned b);
/* TODO Add man pages
libj2_j2i_rsh
libj2_j2i_rsh_to_j2i
libj2_ji_rsh_to_j2i
libj2_j2u_rsh
libj2_j2u_rsh_to_j2u
libj2_ju_rsh_to_j2u
libj2_j2i_rsh_underflow
libj2_j2i_rsh_to_j2i_underflow
libj2_ji_rsh_to_j2i_underflow
libj2_j2u_rsh_underflow
libj2_j2u_rsh_to_j2u_underflow
libj2_ju_rsh_to_j2u_underflow
libj2_j2i_rsh_underflow_p
libj2_ji_rsh_underflow_p
libj2_j2u_rsh_underflow_p
libj2_ju_rsh_underflow_p
*/
#else
static const char *patterns[] = {
"0",
"1111111111111111111111111111111111111111111111111111111111111111",
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111",
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111",
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111"
"1111111111111111111111111111111111111111111111111111111111111111",
"1010101010101001010100100010101010101011000101101011101001000110",
"1110100100100010000010111111011101101001000010001000011110101010",
"11111111111100001001011",
"0010011011001",
"110010001011110100101001010011010111101",
NULL
};
static int
set(struct libj2_j2i *a, const char *bits, size_t leading_signbits)
{
size_t leading_ones = leading_signbits;
size_t i, n;
int punderflow = 0;
int nunderflow = 0;
int negative = 0;
a->high = 0;
a->low = 0;
n = strlen(bits);
if (n > LIBJ2_J2I_BIT) {
bits = &bits[n - LIBJ2_J2I_BIT];
n = LIBJ2_J2I_BIT;
}
if (n == LIBJ2_J2I_BIT)
negative = bits[0] == '1';
while (n && leading_signbits--) {
--n;
punderflow |= bits[n] == '1';
nunderflow |= bits[n] == '0';
}
for (i = 0; n && i < LIBJ2_JU_BIT; i++)
if (bits[--n] == '1')
a->low |= (uintmax_t)1 << i;
for (i = 0; n && i < LIBJ2_JU_BIT; i++)
if (bits[--n] == '1')
a->high |= (uintmax_t)1 << i;
if (!negative)
return +punderflow;
for (i = LIBJ2_JU_BIT; i-- && leading_ones; leading_ones--)
a->high |= (uintmax_t)1 << i;
for (i = LIBJ2_JU_BIT; i-- && leading_ones; leading_ones--)
a->low |= (uintmax_t)1 << i;
return -nunderflow;
}
static void
check(const char *pattern)
{
struct libj2_j2i a, a_saved, r, expected;
unsigned i;
int underflow;
set(&a, pattern, 0);
a_saved = a;
for (i = 0; i < LIBJ2_J2I_BIT + 8U; i++) {
underflow = set(&expected, pattern, i);
EXPECT(libj2_j2i_is_negative(&a) ? underflow <= 0 : underflow >= 0);
r = a;
libj2_j2i_rsh(&r, i);
EXPECT(libj2_j2i_eq_j2i(&r, &expected));
r = a;
EXPECT(libj2_j2i_rsh_underflow(&r, i) == underflow);
EXPECT(libj2_j2i_eq_j2i(&r, &expected));
r = a;
EXPECT(libj2_j2i_rsh_underflow_p((const struct libj2_j2i *)&r, i) == underflow);
EXPECT(libj2_j2i_eq_j2i(&r, &a));
r = (struct libj2_j2i){111, 222};
libj2_j2i_rsh_to_j2i(&a, i, &r);
EXPECT(libj2_j2i_eq_j2i(&a, &a_saved));
EXPECT(libj2_j2i_eq_j2i(&r, &expected));
r = (struct libj2_j2i){111, 222};
EXPECT(libj2_j2i_rsh_to_j2i_underflow(&a, i, &r) == underflow);
EXPECT(libj2_j2i_eq_j2i(&a, &a_saved));
EXPECT(libj2_j2i_eq_j2i(&r, &expected));
r = a;
libj2_j2i_rsh_to_j2i(&r, i, &r);
EXPECT(libj2_j2i_eq_j2i(&r, &expected));
r = a;
EXPECT(libj2_j2i_rsh_to_j2i_underflow(&r, i, &r) == underflow);
EXPECT(libj2_j2i_eq_j2i(&r, &expected));
}
}
int
main(void)
{
size_t i, j;
char pattern[LIBJ2_J2I_BIT + 1U];
srand((unsigned)time(NULL));
for (i = 0; patterns[i]; i++)
check(patterns[i]);
for (i = 0; i < 64; i++) {
for (j = 1; j < LIBJ2_J2I_BIT; j++)
pattern[j] = '0' + (rand() < rand());
pattern[LIBJ2_J2I_BIT] = '\0';
pattern[0] = '0';
check(pattern);
pattern[0] = '1';
check(pattern);
}
return 0;
}
#endif
|