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