some optimizations
@@ -10,6 +10,7 @@ | ||
10 | 10 | #ifndef HAMIGAKI_CHECKSUM_SHA256_HPP |
11 | 11 | #define HAMIGAKI_CHECKSUM_SHA256_HPP |
12 | 12 | |
13 | +#include <boost/detail/endian.hpp> | |
13 | 14 | #include <boost/array.hpp> |
14 | 15 | #include <boost/assert.hpp> |
15 | 16 | #include <boost/cstdint.hpp> |
@@ -41,6 +42,40 @@ | ||
41 | 42 | #endif |
42 | 43 | } |
43 | 44 | |
45 | +#if (defined(_MSC_VER) || defined(__MWERKS__)) && defined(_M_IX86) | |
46 | +inline word swap_word(word n) | |
47 | +{ | |
48 | + __asm | |
49 | + { | |
50 | + mov eax, n | |
51 | + bswap eax | |
52 | + } | |
53 | +} | |
54 | +#elif defined(__GNUC__) && defined(__i486__) | |
55 | +inline word swap_word(word n) | |
56 | +{ | |
57 | + __asm__("bswap %0" : "=r"(n) : "0"(n)); | |
58 | + return n; | |
59 | +} | |
60 | +#else | |
61 | + #define HAMIGAKI_CHECKSUM_NO_SWAP_WORD | |
62 | +#endif | |
63 | + | |
64 | +inline void copy_word(boost::uint8_t* dst, word n) | |
65 | +{ | |
66 | +#if defined(BOOST_BIG_ENDIAN) | |
67 | + std::memcpy(dst, &n, sizeof(word)); | |
68 | +#elif !defined(HAMIGAKI_CHECKSUM_NO_SWAP_WORD) | |
69 | + *reinterpret_cast<word*>(dst) = swap_word(n); | |
70 | +#else | |
71 | + dst[0] = static_cast<boost::uint8_t>(n >> 24); | |
72 | + dst[1] = static_cast<boost::uint8_t>(n >> 16); | |
73 | + dst[2] = static_cast<boost::uint8_t>(n >> 8); | |
74 | + dst[3] = static_cast<boost::uint8_t>(n ); | |
75 | +#endif | |
76 | +} | |
77 | +#undef HAMIGAKI_CHECKSUM_NO_SWAP_WORD | |
78 | + | |
44 | 79 | class sha256_impl |
45 | 80 | { |
46 | 81 | public: |
@@ -106,38 +141,14 @@ | ||
106 | 141 | value_type output() |
107 | 142 | { |
108 | 143 | value_type tmp; |
109 | - tmp[ 0] = static_cast<boost::uint8_t>(h_[0] >> 24); | |
110 | - tmp[ 1] = static_cast<boost::uint8_t>(h_[0] >> 16); | |
111 | - tmp[ 2] = static_cast<boost::uint8_t>(h_[0] >> 8); | |
112 | - tmp[ 3] = static_cast<boost::uint8_t>(h_[0] ); | |
113 | - tmp[ 4] = static_cast<boost::uint8_t>(h_[1] >> 24); | |
114 | - tmp[ 5] = static_cast<boost::uint8_t>(h_[1] >> 16); | |
115 | - tmp[ 6] = static_cast<boost::uint8_t>(h_[1] >> 8); | |
116 | - tmp[ 7] = static_cast<boost::uint8_t>(h_[1] ); | |
117 | - tmp[ 8] = static_cast<boost::uint8_t>(h_[2] >> 24); | |
118 | - tmp[ 9] = static_cast<boost::uint8_t>(h_[2] >> 16); | |
119 | - tmp[10] = static_cast<boost::uint8_t>(h_[2] >> 8); | |
120 | - tmp[11] = static_cast<boost::uint8_t>(h_[2] ); | |
121 | - tmp[12] = static_cast<boost::uint8_t>(h_[3] >> 24); | |
122 | - tmp[13] = static_cast<boost::uint8_t>(h_[3] >> 16); | |
123 | - tmp[14] = static_cast<boost::uint8_t>(h_[3] >> 8); | |
124 | - tmp[15] = static_cast<boost::uint8_t>(h_[3] ); | |
125 | - tmp[16] = static_cast<boost::uint8_t>(h_[4] >> 24); | |
126 | - tmp[17] = static_cast<boost::uint8_t>(h_[4] >> 16); | |
127 | - tmp[18] = static_cast<boost::uint8_t>(h_[4] >> 8); | |
128 | - tmp[19] = static_cast<boost::uint8_t>(h_[4] ); | |
129 | - tmp[20] = static_cast<boost::uint8_t>(h_[5] >> 24); | |
130 | - tmp[21] = static_cast<boost::uint8_t>(h_[5] >> 16); | |
131 | - tmp[22] = static_cast<boost::uint8_t>(h_[5] >> 8); | |
132 | - tmp[23] = static_cast<boost::uint8_t>(h_[5] ); | |
133 | - tmp[24] = static_cast<boost::uint8_t>(h_[6] >> 24); | |
134 | - tmp[25] = static_cast<boost::uint8_t>(h_[6] >> 16); | |
135 | - tmp[26] = static_cast<boost::uint8_t>(h_[6] >> 8); | |
136 | - tmp[27] = static_cast<boost::uint8_t>(h_[6] ); | |
137 | - tmp[28] = static_cast<boost::uint8_t>(h_[7] >> 24); | |
138 | - tmp[29] = static_cast<boost::uint8_t>(h_[7] >> 16); | |
139 | - tmp[30] = static_cast<boost::uint8_t>(h_[7] >> 8); | |
140 | - tmp[31] = static_cast<boost::uint8_t>(h_[7] ); | |
144 | + copy_word(&tmp.elems[ 0], h_[0]); | |
145 | + copy_word(&tmp.elems[ 4], h_[1]); | |
146 | + copy_word(&tmp.elems[ 8], h_[2]); | |
147 | + copy_word(&tmp.elems[12], h_[3]); | |
148 | + copy_word(&tmp.elems[16], h_[4]); | |
149 | + copy_word(&tmp.elems[20], h_[5]); | |
150 | + copy_word(&tmp.elems[24], h_[6]); | |
151 | + copy_word(&tmp.elems[28], h_[7]); | |
141 | 152 | return tmp; |
142 | 153 | } |
143 | 154 |
@@ -177,7 +188,7 @@ | ||
177 | 188 | |
178 | 189 | static word k(word t) |
179 | 190 | { |
180 | - const word table[] = | |
191 | + static const word table[] = | |
181 | 192 | { |
182 | 193 | 0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, |
183 | 194 | 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, |