Debian Codegraph @

regressions/ck_pr/validate/ck_pr_xor.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 BM(m, w) ((uint##m##_t)-1 << (w))
42
43 #define CK_PR_XOR_T(w, v, d) \
44 { \
45 uint##w##_t t = v; \
46 ck_pr_xor_##w(&t, d); \
47 if (t != (uint##w##_t)(v ^ d)) { \
48 printf("FAIL ["); \
49 printf("%" PRIu##w " (%" PRIu##w ") -> %" PRIu##w "]\n",\
50 (uint##w##_t)v, d, t); \
51 exit(EXIT_FAILURE); \
52 } \
53 }
54
55 #define CK_PR_XOR_B(w) \
56 { \
57 unsigned int __ck_i = 0; \
58 printf("ck_pr_xor_" #w ": "); \
59 if (w < 10) \
60 printf(" "); \
61 for (__ck_i = 0; __ck_i < R_REPEAT; __ck_i++) { \
62 uint##w##_t a = (uint##w##_t)common_rand(); \
63 uint##w##_t b = (uint##w##_t)common_rand(); \
64 CK_PR_XOR_T(w, a, b); \
65 } \
66 rg_width(w); \
67 printf(" SUCCESS\n"); \
68 }
69
70 #define CK_PR_XOR_W(m, w) \
71 { \
72 uint##m##_t t = -1; \
73 ck_pr_xor_##w((uint##w##_t *)(void *)&t, -1); \
74 if (t != BM(m, w)) { \
75 printf(" FAIL [%#" PRIx##m " != %#" PRIx##m "]\n", t, BM(m, w)); \
76 exit(EXIT_FAILURE); \
77 } \
78 }
79
80 static void
81 rg_width(int m)
82 {
83
84 /* Other architectures are bi-endian. */
85 #if !defined(__x86__) && !defined(__x86_64__)
86 return;
87 #endif
88
89 #ifdef CK_F_PR_XOR_64
90 if (m == 64) {
91 #if defined(CK_F_PR_XOR_32)
92 CK_PR_XOR_W(64, 32);
93 #endif
94 #if defined(CK_PR_XOR_16)
95 CK_PR_XOR_W(64, 16);
96 #endif
97 #if defined(CK_PR_XOR_8)
98 CK_PR_XOR_W(64, 8);
99 #endif
100 }
101 #endif /* CK_PR_XOR_64 */
102
103 #ifdef CK_F_PR_XOR_32
104 if (m == 32) {
105 #if defined(CK_F_PR_XOR_16)
106 CK_PR_XOR_W(32, 16);
107 #endif
108 #if defined(CK_PR_XOR_8)
109 CK_PR_XOR_W(32, 8);
110 #endif
111 }
112 #endif /* CK_PR_XOR_32 */
113
114 #if defined(CK_F_PR_XOR_16) && defined(CK_PR_XOR_8)
115 if (m == 16) {
116 CK_PR_XOR_W(16, 8);
117 }
118 #endif /* CK_PR_XOR_16 && CK_PR_XOR_8 */
119
120 return;
121 }
122
123 int
124 main(void)
125 {
126
127 common_srand((unsigned int)getpid());
128
129 #ifdef CK_F_PR_XOR_64
130 CK_PR_XOR_B(64);
131 #endif
132
133 #ifdef CK_F_PR_XOR_32
134 CK_PR_XOR_B(32);
135 #endif
136
137 #ifdef CK_F_PR_XOR_16
138 CK_PR_XOR_B(16);
139 #endif
140
141 #ifdef CK_F_PR_XOR_8
142 CK_PR_XOR_B(8);
143 #endif
144
145 return (0);
146 }
147