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 * int$#.c
15 *
16 * $#-way unrolled portable integer math RAID-6 instruction set
17 *
18 * This file is postprocessed using unroll.awk
19 */
20
21#include <linux/raid/pq.h>
22
23/*
24 * This is the C data type to use
25 */
26
27/* Change this from BITS_PER_LONG if there is something better... */
28#if BITS_PER_LONG == 64
29# define NBYTES(x) ((x) * 0x0101010101010101UL)
30# define NSIZE  8
31# define NSHIFT 3
32# define NSTRING "64"
33typedef u64 unative_t;
34#else
35# define NBYTES(x) ((x) * 0x01010101U)
36# define NSIZE  4
37# define NSHIFT 2
38# define NSTRING "32"
39typedef u32 unative_t;
40#endif
41
42
43
44/*
45 * These sub-operations are separate inlines since they can sometimes be
46 * specially optimized using architecture-specific hacks.
47 */
48
49/*
50 * The SHLBYTE() operation shifts each byte left by 1, *not*
51 * rolling over into the next byte
52 */
53static inline __attribute_const__ unative_t SHLBYTE(unative_t v)
54{
55	unative_t vv;
56
57	vv = (v << 1) & NBYTES(0xfe);
58	return vv;
59}
60
61/*
62 * The MASK() operation returns 0xFF in any byte for which the high
63 * bit is 1, 0x00 for any byte for which the high bit is 0.
64 */
65static inline __attribute_const__ unative_t MASK(unative_t v)
66{
67	unative_t vv;
68
69	vv = v & NBYTES(0x80);
70	vv = (vv << 1) - (vv >> 7); /* Overflow on the top bit is OK */
71	return vv;
72}
73
74
75static void raid6_int$#_gen_syndrome(int disks, size_t bytes, void **ptrs)
76{
77	u8 **dptr = (u8 **)ptrs;
78	u8 *p, *q;
79	int d, z, z0;
80
81	unative_t wd$$, wq$$, wp$$, w1$$, w2$$;
82
83	z0 = disks - 3;		/* Highest data disk */
84	p = dptr[z0+1];		/* XOR parity */
85	q = dptr[z0+2];		/* RS syndrome */
86
87	for ( d = 0 ; d < bytes ; d += NSIZE*$# ) {
88		wq$$ = wp$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
89		for ( z = z0-1 ; z >= 0 ; z-- ) {
90			wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
91			wp$$ ^= wd$$;
92			w2$$ = MASK(wq$$);
93			w1$$ = SHLBYTE(wq$$);
94			w2$$ &= NBYTES(0x1d);
95			w1$$ ^= w2$$;
96			wq$$ = w1$$ ^ wd$$;
97		}
98		*(unative_t *)&p[d+NSIZE*$$] = wp$$;
99		*(unative_t *)&q[d+NSIZE*$$] = wq$$;
100	}
101}
102
103static void raid6_int$#_xor_syndrome(int disks, int start, int stop,
104				     size_t bytes, void **ptrs)
105{
106	u8 **dptr = (u8 **)ptrs;
107	u8 *p, *q;
108	int d, z, z0;
109
110	unative_t wd$$, wq$$, wp$$, w1$$, w2$$;
111
112	z0 = stop;		/* P/Q right side optimization */
113	p = dptr[disks-2];	/* XOR parity */
114	q = dptr[disks-1];	/* RS syndrome */
115
116	for ( d = 0 ; d < bytes ; d += NSIZE*$# ) {
117		/* P/Q data pages */
118		wq$$ = wp$$ = *(unative_t *)&dptr[z0][d+$$*NSIZE];
119		for ( z = z0-1 ; z >= start ; z-- ) {
120			wd$$ = *(unative_t *)&dptr[z][d+$$*NSIZE];
121			wp$$ ^= wd$$;
122			w2$$ = MASK(wq$$);
123			w1$$ = SHLBYTE(wq$$);
124			w2$$ &= NBYTES(0x1d);
125			w1$$ ^= w2$$;
126			wq$$ = w1$$ ^ wd$$;
127		}
128		/* P/Q left side optimization */
129		for ( z = start-1 ; z >= 0 ; z-- ) {
130			w2$$ = MASK(wq$$);
131			w1$$ = SHLBYTE(wq$$);
132			w2$$ &= NBYTES(0x1d);
133			wq$$ = w1$$ ^ w2$$;
134		}
135		*(unative_t *)&p[d+NSIZE*$$] ^= wp$$;
136		*(unative_t *)&q[d+NSIZE*$$] ^= wq$$;
137	}
138
139}
140
141const struct raid6_calls raid6_intx$# = {
142	raid6_int$#_gen_syndrome,
143	raid6_int$#_xor_syndrome,
144	NULL,			/* always valid */
145	"int" NSTRING "x$#",
146	0
147};
148