-
Notifications
You must be signed in to change notification settings - Fork 12
/
barrett_reduction_test.c
113 lines (93 loc) · 2.51 KB
/
barrett_reduction_test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Test POWER8 Barrett reduction algorithms.
*
* Copyright (C) 2015 Anton Blanchard <anton@au.ibm.com>, IBM
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of either:
*
* a) the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version, or
* b) the Apache License, Version 2.0
*/
#include <stdio.h>
#include <string.h>
#include "crcmodel.h"
/* CRC without the top bit */
#define CRC 0x04C11DB7
char crc_data[] __attribute__((aligned(4))) = { 0x9e, 0xd3, 0x20, 0xcc, };
/*
* The CRC appends 32bits of 0s to the end of the message, so replicate
* that for our Barrett reduction.
*/
char data[] __attribute__((aligned(8))) = { 0x9e, 0xd3, 0x20, 0xcc, 0, 0, 0, 0, };
static void doit(p_cm_t p_cm, char *str)
{
int i;
for (i = 0; i < sizeof(crc_data)/sizeof(crc_data[0]); i++)
cm_nxt(p_cm, crc_data[i]);
printf("%-25s = 0x%08lx\n", str, cm_crc(p_cm));
}
#ifdef __powerpc__
unsigned int barrett_reduction(unsigned long val);
unsigned int barrett_reduction_reflected(unsigned long val);
static void do_barrett(void)
{
unsigned long val;
unsigned int crc;
printf("Barrett reduction\n");
memcpy(&val, data, sizeof(data));
#ifdef __LITTLE_ENDIAN__
val = __builtin_bswap64(val);
#endif
crc = barrett_reduction(val);
printf("%-25s = 0x%08x\n", "Base", crc);
crc = barrett_reduction(val ^ 0xffffffff00000000UL);
printf("%-25s = 0x%08x\n", "Inverted", ~crc);
memcpy(&val, data, sizeof(data));
#ifndef __LITTLE_ENDIAN__
val = __builtin_bswap64(val);
#endif
crc = barrett_reduction_reflected(val);
printf("%-25s = 0x%08x\n", "Reflected", crc);
crc = barrett_reduction_reflected(val ^ 0x00000000ffffffffUL);
printf("%-25s = 0x%08x\n", "Reflected and Inverted", ~crc);
printf("\n");
}
#else
static void do_barrett(void) { }
#endif
static void do_crc(void)
{
cm_t cm_t = { 0, };
printf("CRC comparision\n");
cm_t.cm_width = 32;
cm_t.cm_poly = CRC;
cm_t.cm_init = 0x0;
cm_t.cm_refin = FALSE;
cm_t.cm_refot = FALSE;
cm_t.cm_xorot = 0x0;
cm_ini(&cm_t);
doit(&cm_t, "Base");
cm_t.cm_init = 0xffffffff;
cm_t.cm_xorot = 0xffffffff;
cm_ini(&cm_t);
doit(&cm_t, "Inverted");
cm_t.cm_init = 0x0;
cm_t.cm_xorot = 0x0;
cm_t.cm_refin = TRUE;
cm_t.cm_refot = TRUE;
cm_ini(&cm_t);
doit(&cm_t, "Reflected");
cm_t.cm_init = 0xffffffff;
cm_t.cm_xorot = 0xffffffff;
cm_ini(&cm_t);
doit(&cm_t, "Reflected and Inverted");
}
int main(void)
{
do_barrett();
do_crc();
return 0;
}