btrfs-progs: convert: move init_btrfs
[btrfs-progs-unstable/devel.git] / raid6.c
blob833df5f305771ee2ffd17f14fad7a2b8e46bcddd
1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright 2002-2004 H. Peter Anvin - All Rights Reserved
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.
11 * ----------------------------------------------------------------------- */
14 * Added helpers for unaligned native int access
18 * raid6int1.c
20 * 1-way unrolled portable integer math RAID-6 instruction set
22 * This file was postprocessed using unroll.pl and then ported to userspace
24 #include <stdint.h>
25 #include <unistd.h>
26 #include "kerncompat.h"
27 #include "ctree.h"
28 #include "disk-io.h"
31 * This is the C data type to use
34 /* Change this from BITS_PER_LONG if there is something better... */
35 #if BITS_PER_LONG == 64
36 # define NBYTES(x) ((x) * 0x0101010101010101UL)
37 # define NSIZE 8
38 # define NSHIFT 3
39 typedef uint64_t unative_t;
40 #define put_unaligned_native(val,p) put_unaligned_64((val),(p))
41 #define get_unaligned_native(p) get_unaligned_64((p))
42 #else
43 # define NBYTES(x) ((x) * 0x01010101U)
44 # define NSIZE 4
45 # define NSHIFT 2
46 typedef uint32_t unative_t;
47 #define put_unaligned_native(val,p) put_unaligned_32((val),(p))
48 #define get_unaligned_native(p) get_unaligned_32((p))
49 #endif
52 * These sub-operations are separate inlines since they can sometimes be
53 * specially optimized using architecture-specific hacks.
57 * The SHLBYTE() operation shifts each byte left by 1, *not*
58 * rolling over into the next byte
60 static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
62 unative_t vv;
64 vv = (v << 1) & NBYTES(0xfe);
65 return vv;
69 * The MASK() operation returns 0xFF in any byte for which the high
70 * bit is 1, 0x00 for any byte for which the high bit is 0.
72 static inline __attribute_const__ unative_t MASK(unative_t v)
74 unative_t vv;
76 vv = v & NBYTES(0x80);
77 vv = (vv << 1) - (vv >> 7); /* Overflow on the top bit is OK */
78 return vv;
82 void raid6_gen_syndrome(int disks, size_t bytes, void **ptrs)
84 uint8_t **dptr = (uint8_t **)ptrs;
85 uint8_t *p, *q;
86 int d, z, z0;
88 unative_t wd0, wq0, wp0, w10, w20;
90 z0 = disks - 3; /* Highest data disk */
91 p = dptr[z0+1]; /* XOR parity */
92 q = dptr[z0+2]; /* RS syndrome */
94 for ( d = 0 ; d < bytes ; d += NSIZE*1 ) {
95 wq0 = wp0 = get_unaligned_native(&dptr[z0][d+0*NSIZE]);
96 for ( z = z0-1 ; z >= 0 ; z-- ) {
97 wd0 = get_unaligned_native(&dptr[z][d+0*NSIZE]);
98 wp0 ^= wd0;
99 w20 = MASK(wq0);
100 w10 = SHLBYTE(wq0);
101 w20 &= NBYTES(0x1d);
102 w10 ^= w20;
103 wq0 = w10 ^ wd0;
105 put_unaligned_native(wp0, &p[d+NSIZE*0]);
106 put_unaligned_native(wq0, &q[d+NSIZE*0]);