Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ident "$Id: mktables.c,v 1.2 2002/12/12 22:41:27 hpa Exp $" |
| 2 | /* ----------------------------------------------------------------------- * |
| 3 | * |
| 4 | * Copyright 2002 H. Peter Anvin - All Rights Reserved |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, Inc., 53 Temple Place Ste 330, |
| 9 | * Bostom MA 02111-1307, USA; either version 2 of the License, or |
| 10 | * (at your option) any later version; incorporated herein by reference. |
| 11 | * |
| 12 | * ----------------------------------------------------------------------- */ |
| 13 | |
| 14 | /* |
| 15 | * mktables.c |
| 16 | * |
| 17 | * Make RAID-6 tables. This is a host user space program to be run at |
| 18 | * compile time. |
| 19 | */ |
| 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <string.h> |
| 23 | #include <inttypes.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | static uint8_t gfmul(uint8_t a, uint8_t b) |
| 28 | { |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 29 | uint8_t v = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 31 | while (b) { |
| 32 | if (b & 1) |
| 33 | v ^= a; |
| 34 | a = (a << 1) ^ (a & 0x80 ? 0x1d : 0); |
| 35 | b >>= 1; |
| 36 | } |
| 37 | |
| 38 | return v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static uint8_t gfpow(uint8_t a, int b) |
| 42 | { |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 43 | uint8_t v = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 45 | b %= 255; |
| 46 | if (b < 0) |
| 47 | b += 255; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 49 | while (b) { |
| 50 | if (b & 1) |
| 51 | v = gfmul(v, a); |
| 52 | a = gfmul(a, a); |
| 53 | b >>= 1; |
| 54 | } |
| 55 | |
| 56 | return v; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | int main(int argc, char *argv[]) |
| 60 | { |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 61 | int i, j, k; |
| 62 | uint8_t v; |
| 63 | uint8_t exptbl[256], invtbl[256]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 65 | printf("#include \"raid6.h\"\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 67 | /* Compute multiplication table */ |
| 68 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
| 69 | "raid6_gfmul[256][256] =\n" |
| 70 | "{\n"); |
| 71 | for (i = 0; i < 256; i++) { |
| 72 | printf("\t{\n"); |
| 73 | for (j = 0; j < 256; j += 8) { |
| 74 | printf("\t\t"); |
| 75 | for (k = 0; k < 8; k++) |
| 76 | printf("0x%02x, ", gfmul(i, j+k)); |
| 77 | printf("\n"); |
| 78 | } |
| 79 | printf("\t},\n"); |
| 80 | } |
| 81 | printf("};\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 83 | /* Compute power-of-2 table (exponent) */ |
| 84 | v = 1; |
| 85 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
| 86 | "raid6_gfexp[256] =\n" |
| 87 | "{\n"); |
| 88 | for (i = 0; i < 256; i += 8) { |
| 89 | printf("\t"); |
| 90 | for (j = 0; j < 8; j++) { |
| 91 | exptbl[i+j] = v; |
| 92 | printf("0x%02x, ", v); |
| 93 | v = gfmul(v, 2); |
| 94 | if (v == 1) |
| 95 | v = 0; /* For entry 255, not a real entry */ |
| 96 | } |
| 97 | printf("\n"); |
| 98 | } |
| 99 | printf("};\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 101 | /* Compute inverse table x^-1 == x^254 */ |
| 102 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
| 103 | "raid6_gfinv[256] =\n" |
| 104 | "{\n"); |
| 105 | for (i = 0; i < 256; i += 8) { |
| 106 | printf("\t"); |
| 107 | for (j = 0; j < 8; j++) { |
| 108 | v = gfpow(i+j, 254); |
| 109 | invtbl[i+j] = v; |
| 110 | printf("0x%02x, ", v); |
| 111 | } |
| 112 | printf("\n"); |
| 113 | } |
| 114 | printf("};\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 116 | /* Compute inv(2^x + 1) (exponent-xor-inverse) table */ |
| 117 | printf("\nconst u8 __attribute__((aligned(256)))\n" |
| 118 | "raid6_gfexi[256] =\n" |
| 119 | "{\n"); |
| 120 | for (i = 0; i < 256; i += 8) { |
| 121 | printf("\t"); |
| 122 | for (j = 0; j < 8; j++) |
| 123 | printf("0x%02x, ", invtbl[exptbl[i+j]^1]); |
| 124 | printf("\n"); |
| 125 | } |
| 126 | printf("};\n\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | |
Oliver Pinter | 54212cf | 2008-02-06 01:39:47 -0800 | [diff] [blame^] | 128 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | } |