xref: /isa-l/raid/raid_base.c (revision ae951677ab3f79d379a9da251b5a1245e7b76aca)
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 <limits.h>
31 #include <stdint.h>
32 
33 #if __WORDSIZE == 64 || _WIN64 || __x86_64__
34 #define notbit0 0xfefefefefefefefeULL
35 #define bit7    0x8080808080808080ULL
36 #define gf8poly 0x1d1d1d1d1d1d1d1dULL
37 #else
38 #define notbit0 0xfefefefeUL
39 #define bit7    0x80808080UL
40 #define gf8poly 0x1d1d1d1dUL
41 #endif
42 
43 int
pq_gen_base(int vects,int len,void ** array)44 pq_gen_base(int vects, int len, void **array)
45 {
46         int i, j;
47         unsigned long p, q, s;
48         unsigned long **src = (unsigned long **) array;
49         int blocks = len / sizeof(long);
50 
51         if (vects < 4)
52                 return 1; // Must have at least 2 src and 2 dest
53 
54         for (i = 0; i < blocks; i++) {
55                 q = p = src[vects - 3][i];
56 
57                 for (j = vects - 4; j >= 0; j--) {
58                         p ^= s = src[j][i];
59                         q = s ^ (((q << 1) & notbit0) ^                   // shift each byte
60                                  ((((q & bit7) << 1) - ((q & bit7) >> 7)) // mask out bytes
61                                   & gf8poly));                            // apply poly
62                 }
63 
64                 src[vects - 2][i] = p; // second to last pointer is p
65                 src[vects - 1][i] = q; // last pointer is q
66         }
67         return 0;
68 }
69 
70 int
pq_check_base(int vects,int len,void ** array)71 pq_check_base(int vects, int len, void **array)
72 {
73         int i, j;
74         unsigned char p, q, s;
75         unsigned char **src = (unsigned char **) array;
76 
77         if (vects < 4)
78                 return 1; // Must have at least 2 src and 2 dest
79 
80         for (i = 0; i < len; i++) {
81                 q = p = src[vects - 3][i];
82 
83                 for (j = vects - 4; j >= 0; j--) {
84                         s = src[j][i];
85                         p ^= s;
86 
87                         // mult by GF{2}
88                         q = s ^ ((q << 1) ^ ((q & 0x80) ? 0x1d : 0));
89                 }
90 
91                 if (src[vects - 2][i] != p) // second to last pointer is p
92                         return i | 1;
93                 if (src[vects - 1][i] != q) // last pointer is q
94                         return i | 2;
95         }
96         return 0;
97 }
98 
99 int
xor_gen_base(int vects,int len,void ** array)100 xor_gen_base(int vects, int len, void **array)
101 {
102         int i, j;
103         unsigned char parity;
104         unsigned char **src = (unsigned char **) array;
105 
106         if (vects < 3)
107                 return 1; // Must have at least 2 src and 1 dest
108 
109         for (i = 0; i < len; i++) {
110                 parity = src[0][i];
111                 for (j = 1; j < vects - 1; j++)
112                         parity ^= src[j][i];
113 
114                 src[vects - 1][i] = parity; // last pointer is dest
115         }
116 
117         return 0;
118 }
119 
120 int
xor_check_base(int vects,int len,void ** array)121 xor_check_base(int vects, int len, void **array)
122 {
123         int i, j, fail = 0;
124 
125         unsigned char parity;
126         unsigned char **src = (unsigned char **) array;
127 
128         if (vects < 2)
129                 return 1; // Must have at least 2 src
130 
131         for (i = 0; i < len; i++) {
132                 parity = 0;
133                 for (j = 0; j < vects; j++)
134                         parity ^= src[j][i];
135 
136                 if (parity != 0) {
137                         fail = 1;
138                         break;
139                 }
140         }
141         if (fail && len > 0)
142                 return len;
143         return fail;
144 }
145