monotone

monotone Mtn Source Tree

Root/botan/bit_ops.h

1/*************************************************
2* Bit/Word Operations Header File *
3* (C) 1999-2008 The Botan Project *
4*************************************************/
5
6#ifndef BOTAN_BIT_OPS_H__
7#define BOTAN_BIT_OPS_H__
8
9#include <botan/types.h>
10
11namespace Botan {
12
13/*************************************************
14* Word Rotation Functions *
15*************************************************/
16template<typename T> inline T rotate_left(T input, u32bit rot)
17 {
18 return static_cast<T>((input << rot) | (input >> (8*sizeof(T)-rot)));;
19 }
20
21template<typename T> inline T rotate_right(T input, u32bit rot)
22 {
23 return static_cast<T>((input >> rot) | (input << (8*sizeof(T)-rot)));
24 }
25
26/*************************************************
27* Byte Swapping Functions *
28*************************************************/
29inline u16bit reverse_bytes(u16bit input)
30 {
31 return rotate_left(input, 8);
32 }
33
34inline u32bit reverse_bytes(u32bit input)
35 {
36 input = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
37 return rotate_left(input, 16);
38 }
39
40inline u64bit reverse_bytes(u64bit input)
41 {
42 u32bit hi = ((input >> 40) & 0x00FF00FF) | ((input >> 24) & 0xFF00FF00);
43 u32bit lo = ((input & 0xFF00FF00) >> 8) | ((input & 0x00FF00FF) << 8);
44 hi = (hi << 16) | (hi >> 16);
45 lo = (lo << 16) | (lo >> 16);
46 return (static_cast<u64bit>(lo) << 32) | hi;
47 }
48
49/*************************************************
50* XOR Arrays *
51*************************************************/
52inline void xor_buf(byte data[], const byte mask[], u32bit length)
53 {
54 while(length >= 8)
55 {
56 data[0] ^= mask[0]; data[1] ^= mask[1];
57 data[2] ^= mask[2]; data[3] ^= mask[3];
58 data[4] ^= mask[4]; data[5] ^= mask[5];
59 data[6] ^= mask[6]; data[7] ^= mask[7];
60 data += 8; mask += 8; length -= 8;
61 }
62 for(u32bit j = 0; j != length; ++j)
63 data[j] ^= mask[j];
64 }
65
66/*************************************************
67* XOR Arrays *
68*************************************************/
69inline void xor_buf(byte out[], const byte in[], const byte mask[], u32bit length)
70 {
71 while(length >= 8)
72 {
73 out[0] = in[0] ^ mask[0]; out[1] = in[1] ^ mask[1];
74 out[2] = in[2] ^ mask[2]; out[3] = in[3] ^ mask[3];
75 out[4] = in[4] ^ mask[4]; out[5] = in[5] ^ mask[5];
76 out[6] = in[6] ^ mask[6]; out[7] = in[7] ^ mask[7];
77 in += 8; out += 8; mask += 8; length -= 8;
78 }
79 for(u32bit j = 0; j != length; ++j)
80 out[j] = in[j] ^ mask[j];
81 }
82
83/*************************************************
84* Simple Bit Manipulation *
85*************************************************/
86bool power_of_2(u64bit);
87u32bit high_bit(u64bit);
88u32bit low_bit(u64bit);
89u32bit significant_bytes(u64bit);
90u32bit hamming_weight(u64bit);
91
92}
93
94#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status