| 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 CK_PR_LOAD_B(w) \ |
| 42 |
{ \ |
| 43 |
uint##w##_t t = (uint##w##_t)-1, a = 0; \ |
| 44 |
unsigned int i; \ |
| 45 |
printf("ck_pr_load_" #w ": "); \ |
| 46 |
if (w < 10) \ |
| 47 |
printf(" "); \ |
| 48 |
a = ck_pr_load_##w(&t); \ |
| 49 |
if (a != t) { \ |
| 50 |
printf("FAIL [%#" PRIx##w " != %#" PRIx##w "]\n", a, t); \ |
| 51 |
exit(EXIT_FAILURE); \ |
| 52 |
} \ |
| 53 |
for (i = 0; i < R_REPEAT; i++) { \ |
| 54 |
t = (uint##w##_t)common_rand(); \ |
| 55 |
a = ck_pr_load_##w(&t); \ |
| 56 |
if (a != t) { \ |
| 57 |
printf("FAIL [%#" PRIx##w " != %#" PRIx##w "]\n", a, t);\ |
| 58 |
exit(EXIT_FAILURE); \ |
| 59 |
} \ |
| 60 |
} \ |
| 61 |
rg_width(w); \ |
| 62 |
printf(" SUCCESS\n"); \ |
| 63 |
} |
| 64 |
|
| 65 |
#define CK_PR_LOAD_W(m, w) \ |
| 66 |
{ \ |
| 67 |
uint##m##_t f = 0; \ |
| 68 |
uint##w##_t j = (uint##w##_t)-1; \ |
| 69 |
f = ck_pr_load_##w(&j); \ |
| 70 |
if (f != j) { \ |
| 71 |
printf("FAIL [%#" PRIx##m " != %#" PRIx##w "]\n", f, j);\ |
| 72 |
exit(EXIT_FAILURE); \ |
| 73 |
} \ |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
rg_width(int m) |
| 78 |
{ |
| 79 |
|
| 80 |
/* Other architectures are bi-endian. */ |
| 81 |
#if !defined(__x86__) && !defined(__x86_64__) |
| 82 |
return; |
| 83 |
#endif |
| 84 |
|
| 85 |
#ifdef CK_F_PR_LOAD_64 |
| 86 |
if (m == 64) { |
| 87 |
#if defined(CK_F_PR_LOAD_32) |
| 88 |
CK_PR_LOAD_W(64, 32); |
| 89 |
#endif |
| 90 |
#if defined(CK_PR_LOAD_16) |
| 91 |
CK_PR_LOAD_W(64, 16); |
| 92 |
#endif |
| 93 |
#if defined(CK_PR_LOAD_8) |
| 94 |
CK_PR_LOAD_W(64, 8); |
| 95 |
#endif |
| 96 |
} |
| 97 |
#endif /* CK_PR_LOAD_64 */ |
| 98 |
|
| 99 |
#ifdef CK_F_PR_LOAD_32 |
| 100 |
if (m == 32) { |
| 101 |
#if defined(CK_F_PR_LOAD_16) |
| 102 |
CK_PR_LOAD_W(32, 16); |
| 103 |
#endif |
| 104 |
#if defined(CK_PR_LOAD_8) |
| 105 |
CK_PR_LOAD_W(32, 8); |
| 106 |
#endif |
| 107 |
} |
| 108 |
#endif /* CK_PR_LOAD_32 */ |
| 109 |
|
| 110 |
#if defined(CK_F_PR_LOAD_16) && defined(CK_PR_LOAD_8) |
| 111 |
if (m == 16) |
| 112 |
CK_PR_LOAD_W(16, 8); |
| 113 |
#endif /* CK_PR_LOAD_16 && CK_PR_LOAD_8 */ |
| 114 |
|
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
int |
| 119 |
main(void) |
| 120 |
{ |
| 121 |
void *ptr = (void *)(intptr_t)-1; |
| 122 |
|
| 123 |
common_srand((unsigned int)getpid()); |
| 124 |
|
| 125 |
#ifdef CK_F_PR_LOAD_64 |
| 126 |
CK_PR_LOAD_B(64); |
| 127 |
#endif |
| 128 |
|
| 129 |
#ifdef CK_F_PR_LOAD_32 |
| 130 |
CK_PR_LOAD_B(32); |
| 131 |
#endif |
| 132 |
|
| 133 |
#ifdef CK_F_PR_LOAD_16 |
| 134 |
CK_PR_LOAD_B(16); |
| 135 |
#endif |
| 136 |
|
| 137 |
#ifdef CK_F_PR_LOAD_8 |
| 138 |
CK_PR_LOAD_B(8); |
| 139 |
#endif |
| 140 |
|
| 141 |
#if 0 |
| 142 |
uint64_t a[2] = {0, 0}, b[2] = {0x1111111144444444, 0x2222222266666666}; |
| 143 |
printf("%" PRIx64 ":%" PRIx64 " -> ", a[0], a[1]); |
| 144 |
ck_pr_load_64_2(&b, &a); |
| 145 |
printf("%" PRIx64 ":%" PRIx64 "\n", a[0], a[1]); |
| 146 |
#endif |
| 147 |
printf("ck_pr_load_ptr: "); |
| 148 |
if (ck_pr_load_ptr(&ptr) != (void *)(intptr_t)(-1)) |
| 149 |
printf("Failed : %p != %p\n", ck_pr_load_ptr(&ptr), (void *)(intptr_t)(-1)); |
| 150 |
else |
| 151 |
printf("SUCCESS\n"); |
| 152 |
|
| 153 |
return (0); |
| 154 |
} |
| 155 |
|