Merge branch 'for-chris' of git://repo.or.cz/btrfs-progs-unstable/devel into raid56
[platform/upstream/btrfs-progs.git] / raid6.c
1 /* -*- linux-c -*- ------------------------------------------------------- *
2  *
3  *   Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8  *   Boston MA 02111-1307, USA; either version 2 of the License, or
9  *   (at your option) any later version; incorporated herein by reference.
10  *
11  * ----------------------------------------------------------------------- */
12
13 /*
14  * raid6int1.c
15  *
16  * 1-way unrolled portable integer math RAID-6 instruction set
17  *
18  * This file was postprocessed using unroll.pl and then ported to userspace
19  */
20 #include <stdint.h>
21 #include <unistd.h>
22 #include "kerncompat.h"
23
24 /*
25  * This is the C data type to use
26  */
27
28 /* Change this from BITS_PER_LONG if there is something better... */
29 #if BITS_PER_LONG == 64
30 # define NBYTES(x) ((x) * 0x0101010101010101UL)
31 # define NSIZE  8
32 # define NSHIFT 3
33 typedef uint64_t unative_t;
34 #else
35 # define NBYTES(x) ((x) * 0x01010101U)
36 # define NSIZE  4
37 # define NSHIFT 2
38 typedef uint32_t unative_t;
39 #endif
40
41 /*
42  * These sub-operations are separate inlines since they can sometimes be
43  * specially optimized using architecture-specific hacks.
44  */
45
46 /*
47  * The SHLBYTE() operation shifts each byte left by 1, *not*
48  * rolling over into the next byte
49  */
50 static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
51 {
52         unative_t vv;
53
54         vv = (v << 1) & NBYTES(0xfe);
55         return vv;
56 }
57
58 /*
59  * The MASK() operation returns 0xFF in any byte for which the high
60  * bit is 1, 0x00 for any byte for which the high bit is 0.
61  */
62 static inline __attribute_const__ unative_t MASK(unative_t v)
63 {
64         unative_t vv;
65
66         vv = v & NBYTES(0x80);
67         vv = (vv << 1) - (vv >> 7); /* Overflow on the top bit is OK */
68         return vv;
69 }
70
71
72 void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs)
73 {
74         uint8_t **dptr = (uint8_t **)ptrs;
75         uint8_t *p, *q;
76         int d, z, z0;
77
78         unative_t wd0, wq0, wp0, w10, w20;
79
80         z0 = disks - 3;         /* Highest data disk */
81         p = dptr[z0+1];         /* XOR parity */
82         q = dptr[z0+2];         /* RS syndrome */
83
84         for ( d = 0 ; d < bytes ; d += NSIZE*1 ) {
85                 wq0 = wp0 = *(unative_t *)&dptr[z0][d+0*NSIZE];
86                 for ( z = z0-1 ; z >= 0 ; z-- ) {
87                         wd0 = *(unative_t *)&dptr[z][d+0*NSIZE];
88                         wp0 ^= wd0;
89                         w20 = MASK(wq0);
90                         w10 = SHLBYTE(wq0);
91                         w20 &= NBYTES(0x1d);
92                         w10 ^= w20;
93                         wq0 = w10 ^ wd0;
94                 }
95                 *(unative_t *)&p[d+NSIZE*0] = wp0;
96                 *(unative_t *)&q[d+NSIZE*0] = wq0;
97         }
98 }
99