-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests.c
128 lines (124 loc) · 3.54 KB
/
tests.c
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
/* Testing Code */
#include <limits.h>
/* Copyright (C) 1991-2016 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */
/* This header is separate from features.h so that the compiler can
include it implicitly at the start of every compilation. It must
not itself include <features.h> or any other header that includes
<features.h> because the implicit include comes before any feature
test macros that may be defined in a source file before it first
explicitly includes a system header. GCC knows the name of this
header in order to preinclude it. */
/* glibc's intent is to support the IEC 559 math functionality, real
and complex. If the GCC (4.9 and later) predefined macros
specifying compiler intent are available, use them to determine
whether the overall intent is to support these features; otherwise,
presume an older compiler has intent to support these features and
define these macros by default. */
/* wchar_t uses Unicode 9.0.0. Version 9.0 of the Unicode Standard is
synchronized with ISO/IEC 10646:2014, fourth edition, plus
Amd. 1 and Amd. 2 and 273 characters from forthcoming 10646, fifth edition.
(Amd. 2 was published 2016-05-01,
see https://www.iso.org/obp/ui/#iso:std:iso-iec:10646:ed-4:v1:amd:2:v1:en) */
/* We do not support C11 <threads.h>. */
int test_bang(int x)
{
return !x;
}
int test_bitCount(int x) {
int result = 0;
int i;
for (i = 0; i < 32; i++)
result += (x >> i) & 0x1;
return result;
}
int test_copyLSB(int x)
{
return (x & 0x1) ? -1 : 0;
}
int test_divpwr2(int x, int n)
{
int p2n = 1<<n;
return x/p2n;
}
int test_evenBits(void) {
int result = 0;
int i;
for (i = 0; i < 32; i+=2)
result |= 1<<i;
return result;
}
int test_fitsBits(int x, int n)
{
int TMin_n = -(1 << (n-1));
int TMax_n = (1 << (n-1)) - 1;
return x >= TMin_n && x <= TMax_n;
}
int test_getByte(int x, int n)
{
union {
int word;
unsigned char bytes[4];
} u;
int test = 1;
int littleEndian = (int) *(char *) &test;
u.word = x;
return littleEndian ? (unsigned) u.bytes[n] : (unsigned) u.bytes[3-n];
}
int test_isGreater(int x, int y)
{
return x > y;
}
int test_isNonNegative(int x) {
return x >= 0;
}
int test_isNotEqual(int x, int y)
{
return x != y;
}
int test_isPower2(int x) {
int i;
for (i = 0; i < 31; i++) {
if (x == 1<<i)
return 1;
}
return 0;
}
int test_leastBitPos(int x) {
int mask = 1;
if (x == 0)
return 0;
while (!(mask & x)) {
mask = mask << 1;
}
return mask;
}
int test_logicalShift(int x, int n) {
unsigned u = (unsigned) x;
unsigned shifted = u >> n;
return (int) shifted;
}
int test_satAdd(int x, int y)
{
if (x > 0 && y > 0 && x+y < 0)
return LONG_MAX;
if (x < 0 && y < 0 && x+y >= 0)
return LONG_MIN;
return x + y;
}
int test_tc2sm(int x) {
int sign = x < 0;
int mag = x < 0 ? -x : x;
return (sign << 31) | mag;
}