Debian Codegraph @

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