diff options
author | lloyd <[email protected]> | 2007-05-31 03:25:19 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2007-05-31 03:25:19 +0000 |
commit | 55608e7dd1aa593944f967f2549564e4f42b654e (patch) | |
tree | ec2ec03a762a6dac82eb608487d5394370135624 /include/bit_ops.h | |
parent | 22ecdc45a0efa4c444d0b7010b7cd743aeb68c57 (diff) |
Write functions to handle loading and saving words a block at a time, taking into
account endian differences.
The current code does not take advantage of the knowledge of which endianness
we are running on; an optimization suggested by Yves Jerschow is to use (unsafe)
casts to speed up the load/store operations. This turns out to provide large
performance increases (30% or more) in some cases.
Even without the unsafe casts, this version seems to average a few percent
faster, probably because the longer loading loops have been partially or
fully unrolled.
This also makes the code implementing low-level algorithms like ciphers and
hashes a bit more succint.
Diffstat (limited to 'include/bit_ops.h')
-rw-r--r-- | include/bit_ops.h | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/include/bit_ops.h b/include/bit_ops.h index 34f7365b2..0636cac41 100644 --- a/include/bit_ops.h +++ b/include/bit_ops.h @@ -59,6 +59,146 @@ u32bit low_bit(u64bit); u32bit significant_bytes(u64bit); u32bit hamming_weight(u64bit); +/************************************************* +* Endian-Specific Word Loading Operations * +*************************************************/ +template<typename T> +inline T load_be(const byte in[], u32bit off) + { + in += off * sizeof(T); + T out = 0; + for(u32bit j = 0; j != sizeof(T); j++) + out = (out << 8) | in[j]; + return out; + } + +template<typename T> +inline T load_le(const byte in[], u32bit off) + { + in += off * sizeof(T); + T out = 0; + for(u32bit j = 0; j != sizeof(T); j++) + out = (out << 8) | in[sizeof(T)-1-j]; + return out; + } + +template<> +inline u32bit load_be<u32bit>(const byte in[], u32bit off) + { + in += off * sizeof(u32bit); + return make_u32bit(in[0], in[1], in[2], in[3]); + } + +template<> +inline u32bit load_le<u32bit>(const byte in[], u32bit off) + { + in += off * sizeof(u32bit); + return make_u32bit(in[3], in[2], in[1], in[0]); + } + +template<> +inline u64bit load_be<u64bit>(const byte in[], u32bit off) + { + in += off * sizeof(u64bit); + return make_u64bit(in[0], in[1], in[2], in[3], + in[4], in[5], in[6], in[7]); + } + +template<> +inline u64bit load_le<u64bit>(const byte in[], u32bit off) + { + in += off * sizeof(u64bit); + return make_u64bit(in[7], in[6], in[5], in[4], + in[3], in[2], in[1], in[0]); + } + +/************************************************* +* Endian-Specific Word Storing Operations * +*************************************************/ +inline void store_be(u16bit in, byte out[2]) + { + out[0] = get_byte(0, in); + out[1] = get_byte(1, in); + } + +inline void store_le(u16bit in, byte out[2]) + { + out[0] = get_byte(1, in); + out[1] = get_byte(0, in); + } + +inline void store_be(u32bit in, byte out[4]) + { + out[0] = get_byte(0, in); + out[1] = get_byte(1, in); + out[2] = get_byte(2, in); + out[3] = get_byte(3, in); + } + +inline void store_le(u32bit in, byte out[4]) + { + out[0] = get_byte(3, in); + out[1] = get_byte(2, in); + out[2] = get_byte(1, in); + out[3] = get_byte(0, in); + } + +inline void store_be(u64bit in, byte out[8]) + { + out[0] = get_byte(0, in); + out[1] = get_byte(1, in); + out[2] = get_byte(2, in); + out[3] = get_byte(3, in); + out[4] = get_byte(4, in); + out[5] = get_byte(5, in); + out[6] = get_byte(6, in); + out[7] = get_byte(7, in); + } + +inline void store_le(u64bit in, byte out[8]) + { + out[0] = get_byte(7, in); + out[1] = get_byte(6, in); + out[2] = get_byte(5, in); + out[3] = get_byte(4, in); + out[4] = get_byte(3, in); + out[5] = get_byte(2, in); + out[6] = get_byte(1, in); + out[7] = get_byte(0, in); + } + +template<typename T> +inline void store_le(byte out[], T a, T b) + { + store_le(a, out + (0 * sizeof(T))); + store_le(b, out + (1 * sizeof(T))); + } + +template<typename T> +inline void store_be(byte out[], T a, T b) + { + store_be(a, out + (0 * sizeof(T))); + store_be(b, out + (1 * sizeof(T))); + } + +template<typename T> +inline void store_le(byte out[], T a, T b, T c, T d) + { + store_le(a, out + (0 * sizeof(T))); + store_le(b, out + (1 * sizeof(T))); + store_le(c, out + (2 * sizeof(T))); + store_le(d, out + (3 * sizeof(T))); + } + +template<typename T> +inline void store_be(byte out[], T a, T b, T c, T d) + { + store_be(a, out + (0 * sizeof(T))); + store_be(b, out + (1 * sizeof(T))); + store_be(c, out + (2 * sizeof(T))); + store_be(d, out + (3 * sizeof(T))); + } + } #endif |