]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/erasure_code/gf_vect_dot_prod_1tbl.c
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / isa-l / erasure_code / gf_vect_dot_prod_1tbl.c
1 /**********************************************************************
2 Copyright(c) 2011-2015 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h> // for memset, memcmp
33 #include "test.h"
34 #include "erasure_code.h"
35
36 //#define CACHED_TEST
37 #ifdef CACHED_TEST
38 // Cached test, loop many times over small dataset
39 # define TEST_SOURCES 10
40 # define TEST_LEN 8*1024
41 # define TEST_LOOPS 4000
42 # define TEST_TYPE_STR "_warm"
43 #else
44 # ifndef TEST_CUSTOM
45 // Uncached test. Pull from large mem base.
46 # define TEST_SOURCES 10
47 # define GT_L3_CACHE 32*1024*1024 /* some number > last level cache */
48 # define TEST_LEN GT_L3_CACHE / TEST_SOURCES
49 # define TEST_LOOPS 10
50 # define TEST_TYPE_STR "_cold"
51 # else
52 # define TEST_TYPE_STR "_cus"
53 # ifndef TEST_LOOPS
54 # define TEST_LOOPS 1000
55 # endif
56 # endif
57 #endif
58
59 typedef unsigned char u8;
60
61 // Global GF(256) tables
62 u8 gff[256];
63 u8 gflog[256];
64 u8 gf_mul_table[256 * 256];
65
66 void mk_gf_field(void)
67 {
68 int i;
69 u8 s = 1;
70 gflog[0] = 0;
71
72 for (i = 0; i < 256; i++) {
73 gff[i] = s;
74 gflog[s] = i;
75 s = (s << 1) ^ ((s & 0x80) ? 0x1d : 0); // mult by GF{2}
76 }
77 }
78
79 void mk_gf_mul_table(u8 * table)
80 {
81 // Populate a single table with all multiply combinations for a fast,
82 // single-table lookup of GF(2^8) multiply at the expense of memory.
83 int i, j;
84 for (i = 0; i < 256; i++)
85 for (j = 0; j < 256; j++)
86 table[i * 256 + j] = gf_mul(i, j);
87 }
88
89 void gf_vect_dot_prod_ref(int len, int vlen, u8 * v, u8 ** src, u8 * dest)
90 {
91 int i, j;
92 u8 s;
93 for (i = 0; i < len; i++) {
94 s = 0;
95 for (j = 0; j < vlen; j++)
96 s ^= gf_mul(src[j][i], v[j]);
97
98 dest[i] = s;
99 }
100 }
101
102 int main(void)
103 {
104 int i, j, k;
105 u8 s, vec[TEST_SOURCES], dest1[TEST_LEN], dest2[TEST_LEN];
106 u8 *matrix[TEST_SOURCES];
107 struct perf start, stop;
108
109 mk_gf_field();
110 mk_gf_mul_table(gf_mul_table);
111
112 //generate random vector and matrix/data
113 for (i = 0; i < TEST_SOURCES; i++) {
114 vec[i] = rand();
115
116 if (!(matrix[i] = malloc(TEST_LEN))) {
117 fprintf(stderr, "Error failure\n\n");
118 return -1;
119 }
120 for (j = 0; j < TEST_LEN; j++)
121 matrix[i][j] = rand();
122
123 }
124
125 gf_vect_dot_prod_ref(TEST_LEN, TEST_SOURCES, vec, matrix, dest1);
126
127 perf_start(&start);
128 for (i = 0; i < TEST_LOOPS; i++)
129 gf_vect_dot_prod_ref(TEST_LEN, TEST_SOURCES, vec, matrix, dest1);
130
131 perf_stop(&stop);
132 printf("gf_vect_dot_prod_2tbl" TEST_TYPE_STR ": ");
133 perf_print(stop, start, (long long)TEST_LEN * (TEST_SOURCES + 1) * i);
134
135 // Warm up mult tables
136 for (i = 0; i < TEST_LEN; i++) {
137 s = 0;
138 for (j = 0; j < TEST_SOURCES; j++) {
139 s ^= gf_mul_table[vec[j] * 256 + matrix[j][i]];
140 }
141 dest2[i] = s;
142 }
143
144 perf_start(&start);
145 for (k = 0; k < TEST_LOOPS; k++) {
146 for (i = 0; i < TEST_LEN; i++) {
147 s = 0;
148 for (j = 0; j < TEST_SOURCES; j++) {
149 s ^= gf_mul_table[vec[j] * 256 + matrix[j][i]];
150 }
151 dest2[i] = s;
152 }
153 }
154 perf_stop(&stop);
155 printf("gf_vect_dot_prod_1tbl" TEST_TYPE_STR ": ");
156 perf_print(stop, start, (long long)TEST_LEN * (TEST_SOURCES + 1) * k);
157
158 // Compare with reference function
159 if (0 != memcmp(dest1, dest2, TEST_LEN)) {
160 printf("Error, different results!\n\n");
161 return -1;
162 }
163
164 printf("Pass functional test\n");
165 return 0;
166 }