diff options
author | Thomas Bertschinger <tahbertschinger@gmail.com> | 2024-01-15 23:41:02 -0700 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2024-01-16 01:47:05 -0500 |
commit | f5baaf48e3e82b1caf9f5cd1207d4d6feba3a2e5 (patch) | |
tree | 59f7b0e4667df7a9d3d5a45725f2aaab3e79b4c5 /c_src/raid/gf.h | |
parent | fb35dbfdc5a9446fbb856dae5542b23963e28b89 (diff) |
move Rust sources to top level, C sources into c_src
This moves the Rust sources out of rust_src/ and into the top level.
Running the bcachefs executable out of the development tree is now:
$ ./target/release/bcachefs command
or
$ cargo run --profile release -- command
instead of "./bcachefs command".
Building and installing is still:
$ make && make install
Signed-off-by: Thomas Bertschinger <tahbertschinger@gmail.com>
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
Diffstat (limited to 'c_src/raid/gf.h')
-rw-r--r-- | c_src/raid/gf.h | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/c_src/raid/gf.h b/c_src/raid/gf.h new file mode 100644 index 00000000..1702c287 --- /dev/null +++ b/c_src/raid/gf.h @@ -0,0 +1,137 @@ +/* + * Copyright (C) 2013 Andrea Mazzoleni + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#ifndef __RAID_GF_H +#define __RAID_GF_H + +/* + * Galois field operations. + * + * Basic range checks are implemented using BUG_ON(). + */ + +/* + * GF a*b. + */ +static __always_inline uint8_t mul(uint8_t a, uint8_t b) +{ + return gfmul[a][b]; +} + +/* + * GF 1/a. + * Not defined for a == 0. + */ +static __always_inline uint8_t inv(uint8_t v) +{ + BUG_ON(v == 0); /* division by zero */ + + return gfinv[v]; +} + +/* + * GF 2^a. + */ +static __always_inline uint8_t pow2(int v) +{ + BUG_ON(v < 0 || v > 254); /* invalid exponent */ + + return gfexp[v]; +} + +/* + * Gets the multiplication table for a specified value. + */ +static __always_inline const uint8_t *table(uint8_t v) +{ + return gfmul[v]; +} + +/* + * Gets the generator matrix coefficient for parity 'p' and disk 'd'. + */ +static __always_inline uint8_t A(int p, int d) +{ + return gfgen[p][d]; +} + +/* + * Dereference as uint8_t + */ +#define v_8(p) (*(uint8_t *)&(p)) + +/* + * Dereference as uint32_t + */ +#define v_32(p) (*(uint32_t *)&(p)) + +/* + * Dereference as uint64_t + */ +#define v_64(p) (*(uint64_t *)&(p)) + +/* + * Multiply each byte of a uint32 by 2 in the GF(2^8). + */ +static __always_inline uint32_t x2_32(uint32_t v) +{ + uint32_t mask = v & 0x80808080U; + + mask = (mask << 1) - (mask >> 7); + v = (v << 1) & 0xfefefefeU; + v ^= mask & 0x1d1d1d1dU; + return v; +} + +/* + * Multiply each byte of a uint64 by 2 in the GF(2^8). + */ +static __always_inline uint64_t x2_64(uint64_t v) +{ + uint64_t mask = v & 0x8080808080808080ULL; + + mask = (mask << 1) - (mask >> 7); + v = (v << 1) & 0xfefefefefefefefeULL; + v ^= mask & 0x1d1d1d1d1d1d1d1dULL; + return v; +} + +/* + * Divide each byte of a uint32 by 2 in the GF(2^8). + */ +static __always_inline uint32_t d2_32(uint32_t v) +{ + uint32_t mask = v & 0x01010101U; + + mask = (mask << 8) - mask; + v = (v >> 1) & 0x7f7f7f7fU; + v ^= mask & 0x8e8e8e8eU; + return v; +} + +/* + * Divide each byte of a uint64 by 2 in the GF(2^8). + */ +static __always_inline uint64_t d2_64(uint64_t v) +{ + uint64_t mask = v & 0x0101010101010101ULL; + + mask = (mask << 8) - mask; + v = (v >> 1) & 0x7f7f7f7f7f7f7f7fULL; + v ^= mask & 0x8e8e8e8e8e8e8e8eULL; + return v; +} + +#endif + |