Debian Codegraph @

regressions/ck_pr/validate/ck_pr_cas.c

raw
1 /*
2 * Copyright 2009 Samy Al Bahra.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #include <inttypes.h>
28 #include <stdbool.h>
29 #include <stdio.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #include <ck_pr.h>
35
36 #include "../../common.h"
37 #ifndef R_REPEAT
38 #define R_REPEAT 200000
39 #endif
40
41 #define W(w, x) (uint##w##_t)((x) & (uint##w##_t)~0)
42
43 #define CK_PR_CAS_T(w, v, c, s) \
44 { \
45 uint##w##_t t = v; \
46 bool r; \
47 r = ck_pr_cas_##w(&t, c, s); \
48 if (((c == v) && (r == false)) || ((c != v) && (r == true)) || \
49 ((r == true) && (W(w, s) != t))) { \
50 printf("FAIL ["); \
51 printf("%" PRIu##w " (%" PRIu##w " -> %" PRIu##w ")" \
52 " -> %" PRIu##w "]\n", \
53 (uint##w##_t)(v), (uint##w##_t)(c), W(w, s), (uint##w##_t)(t)); \
54 } \
55 }
56
57 #define CK_PR_CAS_B(w) \
58 { \
59 unsigned int __ck_i; \
60 printf("ck_pr_cas_" #w ": "); \
61 if (w < 10) \
62 printf(" "); \
63 for (__ck_i = 0; __ck_i < R_REPEAT; __ck_i++) { \
64 uint##w##_t a = common_rand() % (uint##w##_t)-1; \
65 CK_PR_CAS_T(w, a, a + 1, (a - 1)); \
66 CK_PR_CAS_T(w, a, a, (a - 1)); \
67 CK_PR_CAS_T(w, a, a + 1, a); \
68 } \
69 rg_width(w); \
70 printf(" SUCCESS\n"); \
71 }
72
73 #define CK_PR_CAS_W(m, w) \
74 { \
75 uint##m##_t t = -1, r = -1 & ~(uint##m##_t)(uint##w##_t)-1; \
76 ck_pr_cas_##w((uint##w##_t *)(void *)&t, (uint##w##_t)t, 0); \
77 if (t != r) { \
78 printf("FAIL [%#" PRIx##m " != %#" PRIx##m "]\n", \
79 (uint##m##_t)t, (uint##m##_t)r); \
80 } \
81 }
82
83 static void
84 rg_width(int m)
85 {
86
87 /* Other architectures are bi-endian. */
88 #if !defined(__x86__) && !defined(__x86_64__)
89 return;
90 #endif
91
92 #ifdef CK_F_PR_CAS_64
93 if (m == 64) {
94 #if defined(CK_F_PR_CAS_32)
95 CK_PR_CAS_W(64, 32);
96 #endif
97 #if defined(CK_PR_CAS_16)
98 CK_PR_CAS_W(64, 16);
99 #endif
100 #if defined(CK_PR_CAS_8)
101 CK_PR_CAS_W(64, 8);
102 #endif
103 }
104 #endif /* CK_PR_CAS_64 */
105
106 #ifdef CK_F_PR_CAS_32
107 if (m == 32) {
108 #if defined(CK_F_PR_CAS_16)
109 CK_PR_CAS_W(32, 16);
110 #endif
111 #if defined(CK_PR_CAS_8)
112 CK_PR_CAS_W(32, 8);
113 #endif
114 }
115 #endif /* CK_PR_CAS_32 */
116
117 #if defined(CK_F_PR_CAS_16) && defined(CK_PR_CAS_8)
118 if (m == 16) {
119 CK_PR_CAS_W(16, 8);
120 }
121 #endif /* CK_PR_CAS_16 && CK_PR_CAS_8 */
122
123 return;
124 }
125
126 int
127 main(void)
128 {
129
130 common_srand((unsigned int)getpid());
131
132 #ifdef CK_F_PR_CAS_64
133 CK_PR_CAS_B(64);
134 #endif
135
136 #ifdef CK_F_PR_CAS_32
137 CK_PR_CAS_B(32);
138 #endif
139
140 #ifdef CK_F_PR_CAS_16
141 CK_PR_CAS_B(16);
142 #endif
143
144 #ifdef CK_F_PR_CAS_8
145 CK_PR_CAS_B(8);
146 #endif
147
148 #ifdef CK_F_PR_CAS_64_VALUE
149 uint64_t a = 0xffffffffaaaaaaaa, b = 0x8888888800000000;
150
151 printf("%" PRIx64 " (%" PRIx64 ") -> ", b, a);
152 ck_pr_cas_64_value(&a, a, b, &b);
153 printf("%" PRIx64 " (%" PRIx64 ")\n", b, a);
154 #endif
155
156 return (0);
157 }
158