]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/isa-l/erasure_code/gf_vect_dot_prod_1tbl.c
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / spdk / 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_TYPE_STR "_warm"
42 #else
43 # ifndef TEST_CUSTOM
44 // Uncached test. Pull from large mem base.
45 # define TEST_SOURCES 10
46 # define GT_L3_CACHE 32*1024*1024 /* some number > last level cache */
47 # define TEST_LEN GT_L3_CACHE / TEST_SOURCES
48 # define TEST_TYPE_STR "_cold"
49 # else
50 # define TEST_TYPE_STR "_cus"
51 # endif
52 #endif
53
54 typedef unsigned char u8;
55
56 // Global GF(256) tables
57 u8 gff[256];
58 u8 gflog[256];
59 u8 gf_mul_table[256 * 256];
60
61 void mk_gf_field(void)
62 {
63 int i;
64 u8 s = 1;
65 gflog[0] = 0;
66
67 for (i = 0; i < 256; i++) {
68 gff[i] = s;
69 gflog[s] = i;
70 s = (s << 1) ^ ((s & 0x80) ? 0x1d : 0); // mult by GF{2}
71 }
72 }
73
74 void mk_gf_mul_table(u8 * table)
75 {
76 // Populate a single table with all multiply combinations for a fast,
77 // single-table lookup of GF(2^8) multiply at the expense of memory.
78 int i, j;
79 for (i = 0; i < 256; i++)
80 for (j = 0; j < 256; j++)
81 table[i * 256 + j] = gf_mul(i, j);
82 }
83
84 void gf_vect_dot_prod_ref(int len, int vlen, u8 * v, u8 ** src, u8 * dest)
85 {
86 int i, j;
87 u8 s;
88 for (i = 0; i < len; i++) {
89 s = 0;
90 for (j = 0; j < vlen; j++)
91 s ^= gf_mul(src[j][i], v[j]);
92
93 dest[i] = s;
94 }
95 }
96
97 void gf_vect_dot_prod_mult(int len, int vlen, u8 * v, u8 ** src, u8 * dest)
98 {
99 int i, j;
100 u8 s;
101 for (i = 0; i < len; i++) {
102 s = 0;
103 for (j = 0; j < vlen; j++) {
104 s ^= gf_mul_table[v[j] * 256 + src[j][i]];
105 }
106 dest[i] = s;
107 }
108
109 }
110
111 int main(void)
112 {
113 int i, j;
114 u8 vec[TEST_SOURCES], dest1[TEST_LEN], dest2[TEST_LEN];
115 u8 *matrix[TEST_SOURCES];
116 struct perf start;
117
118 mk_gf_field();
119 mk_gf_mul_table(gf_mul_table);
120
121 //generate random vector and matrix/data
122 for (i = 0; i < TEST_SOURCES; i++) {
123 vec[i] = rand();
124
125 if (!(matrix[i] = malloc(TEST_LEN))) {
126 fprintf(stderr, "Error failure\n\n");
127 return -1;
128 }
129 for (j = 0; j < TEST_LEN; j++)
130 matrix[i][j] = rand();
131
132 }
133
134 BENCHMARK(&start, BENCHMARK_TIME,
135 gf_vect_dot_prod_ref(TEST_LEN, TEST_SOURCES, vec, matrix, dest1));
136 printf("gf_vect_dot_prod_2tbl" TEST_TYPE_STR ": ");
137 perf_print(start, (long long)TEST_LEN * (TEST_SOURCES + 1));
138
139 BENCHMARK(&start, BENCHMARK_TIME,
140 gf_vect_dot_prod_mult(TEST_LEN, TEST_SOURCES, vec, matrix, dest2));
141 printf("gf_vect_dot_prod_1tbl" TEST_TYPE_STR ": ");
142 perf_print(start, (long long)TEST_LEN * (TEST_SOURCES + 1));
143
144 // Compare with reference function
145 if (0 != memcmp(dest1, dest2, TEST_LEN)) {
146 printf("Error, different results!\n\n");
147 return -1;
148 }
149
150 printf("Pass functional test\n");
151 return 0;
152 }