diff --git a/sprout/bit/clrsb.hpp b/sprout/bit/clrsb.hpp new file mode 100644 index 00000000..fea2f41e --- /dev/null +++ b/sprout/bit/clrsb.hpp @@ -0,0 +1,50 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_CLRSB_HPP +#define SPROUT_BIT_CLRSB_HPP + +#include +#include +#include + +namespace sprout { + namespace detail { +# if SPROUT_USE_BUILTIN_BIT_OPERATION + inline SPROUT_CONSTEXPR int + clrsb(unsigned x) { + return __builtin_clrsb(x); + } + inline SPROUT_CONSTEXPR int + clrsb(unsigned long x) { + return __builtin_clrsbl(x); + } + inline SPROUT_CONSTEXPR int + clrsb(unsigned long long x) { + return __builtin_clrsbll(x); + } +# endif + template + inline SPROUT_CONSTEXPR int + clrsb(Integral x) { + return sprout::clz(~x); + } + } // namespace detail + // + // clrsb + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + int + >::type + clrsb(Integral x) { + return sprout::detail::clrsb(x); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_CLRSB_HPP diff --git a/sprout/bit/clz.hpp b/sprout/bit/clz.hpp index 0524fe94..c2f5a3f0 100644 --- a/sprout/bit/clz.hpp +++ b/sprout/bit/clz.hpp @@ -16,52 +16,59 @@ namespace sprout { namespace detail { # if SPROUT_USE_BUILTIN_BIT_OPERATION inline SPROUT_CONSTEXPR int - clz(unsigned n) { - return __builtin_clz(n); + clz_non0(unsigned x) { + return __builtin_clz(x); } inline SPROUT_CONSTEXPR int - clz(unsigned long n) { - return __builtin_clzl(n); + clz_non0(unsigned long x) { + return __builtin_clzl(x); } inline SPROUT_CONSTEXPR int - clz(unsigned long long n) { - return __builtin_clzll(n); + clz_non0(unsigned long long x) { + return __builtin_clzll(x); } # endif - template + template inline SPROUT_CONSTEXPR int - clz_impl(T n, T m = T(1) << (CHAR_BIT * sizeof(T) - 1)) { - return m == 0 || n & m ? 0 - : 1 + sprout::detail::clz_impl(n, static_cast(m >> 1)) + clz_non0_impl(Integral x, Integral m = Integral(1) << (CHAR_BIT * sizeof(Integral) - 1)) { + return m == 0 || x & m ? 0 + : 1 + sprout::detail::clz_non0_impl(x, static_cast(m >> 1)) ; } - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_unsigned::value, + std::is_unsigned::value, int >::type - clz(T n) { - return sprout::detail::clz_impl(static_cast(n)); + clz_non0(Integral x) { + return sprout::detail::clz_non0_impl(static_cast(x)); } - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_signed::value, + std::is_signed::value, int >::type - clz(T n) { - return sprout::detail::clz(static_cast::type>(n)); + clz_non0(Integral x) { + return sprout::detail::clz_non0(static_cast::type>(x)); + } + template + inline SPROUT_CONSTEXPR int + clz(Integral x) { + return x == 0 ? static_cast(sizeof(x) * CHAR_BIT) + : sprout::detail::clz_non0(x) + ; } } // namespace detail // // clz // - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_integral::value, + std::is_integral::value, int >::type - clz(T n) { - return sprout::detail::clz(n); + clz(Integral x) { + return sprout::detail::clz(x); } } // namespace sprout diff --git a/sprout/bit/cntl0.hpp b/sprout/bit/cntl0.hpp new file mode 100644 index 00000000..e61f7c2f --- /dev/null +++ b/sprout/bit/cntl0.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_CNTL0_HPP +#define SPROUT_BIT_CNTL0_HPP + +#include +#include +#include + +namespace sprout { + // + // cntl0 + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + int + >::type + cntl0(Integral x) SPROUT_NOEXCEPT { + return sprout::clz(x); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_CNTL0_HPP diff --git a/sprout/bit/cntl1.hpp b/sprout/bit/cntl1.hpp new file mode 100644 index 00000000..f96fc130 --- /dev/null +++ b/sprout/bit/cntl1.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_CNTL1_HPP +#define SPROUT_BIT_CNTL1_HPP + +#include +#include +#include + +namespace sprout { + // + // cntl1 + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + int + >::type + cntl1(Integral x) SPROUT_NOEXCEPT { + return sprout::clrsb(x); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_CNTL0_HPP diff --git a/sprout/bit/cntt0.hpp b/sprout/bit/cntt0.hpp new file mode 100644 index 00000000..a70ebde6 --- /dev/null +++ b/sprout/bit/cntt0.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_CNTT0_HPP +#define SPROUT_BIT_CNTT0_HPP + +#include +#include +#include + +namespace sprout { + // + // cntt0 + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + int + >::type + cntt0(Integral x) SPROUT_NOEXCEPT { + return sprout::ctz(x); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_CNTT0_HPP diff --git a/sprout/bit/cntt1.hpp b/sprout/bit/cntt1.hpp new file mode 100644 index 00000000..e4d84140 --- /dev/null +++ b/sprout/bit/cntt1.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_CNTT1_HPP +#define SPROUT_BIT_CNTT1_HPP + +#include +#include +#include + +namespace sprout { + // + // cntt1 + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + int + >::type + cntt1(Integral x) SPROUT_NOEXCEPT { + return sprout::ctz(~x); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_CNTT1_HPP diff --git a/sprout/bit/ctz.hpp b/sprout/bit/ctz.hpp index 534d3cb2..faa8db1f 100644 --- a/sprout/bit/ctz.hpp +++ b/sprout/bit/ctz.hpp @@ -8,6 +8,7 @@ #ifndef SPROUT_BIT_CTZ_HPP #define SPROUT_BIT_CTZ_HPP +#include #include #include @@ -15,47 +16,54 @@ namespace sprout { namespace detail { # if SPROUT_USE_BUILTIN_BIT_OPERATION inline SPROUT_CONSTEXPR int - ctz(unsigned n) { - return __builtin_ctz(n); + ctz_non0(unsigned x) { + return __builtin_ctz(x); } inline SPROUT_CONSTEXPR int - ctz(unsigned long n) { - return __builtin_ctzl(n); + ctz_non0(unsigned long x) { + return __builtin_ctzl(x); } inline SPROUT_CONSTEXPR int - ctz(unsigned long long n) { - return __builtin_ctzll(n); + ctz_non0(unsigned long long x) { + return __builtin_ctzll(x); } # endif - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_unsigned::value, + std::is_unsigned::value, int >::type - ctz(T n) { - return n & 1 ? 0 - : 1 + sprout::detail::ctz(static_cast(n >> 1)) + ctz_non0(Integral x) { + return x & 1 ? 0 + : 1 + sprout::detail::ctz_non0(static_cast(x >> 1)) ; } - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_signed::value, + std::is_signed::value, int >::type - ctz(T n) { - return sprout::detail::ctz(static_cast::type>(n)); + ctz_non0(Integral x) { + return sprout::detail::ctz_non0(static_cast::type>(x)); + } + template + inline SPROUT_CONSTEXPR int + ctz(Integral x) { + return x == 0 ? static_cast(sizeof(x) * CHAR_BIT) + : sprout::detail::ctz_non0(x) + ; } } // namespace detail // // ctz // - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_integral::value, + std::is_integral::value, int >::type - ctz(T n) { - return sprout::detail::ctz(n); + ctz(Integral x) { + return sprout::detail::ctz(x); } } // namespace sprout diff --git a/sprout/bit/operation.hpp b/sprout/bit/operation.hpp index 7a369d5c..0c544ca0 100644 --- a/sprout/bit/operation.hpp +++ b/sprout/bit/operation.hpp @@ -9,9 +9,20 @@ #define SPROUT_BIT_OPERATION_HPP #include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include +#include #include #include #include diff --git a/sprout/bit/rotate.hpp b/sprout/bit/rotate.hpp index 1bb81f03..6c5230fe 100644 --- a/sprout/bit/rotate.hpp +++ b/sprout/bit/rotate.hpp @@ -8,34 +8,34 @@ #ifndef SPROUT_BIT_ROTATE_HPP #define SPROUT_BIT_ROTATE_HPP -#include -#include #include #include +#include +#include namespace sprout { // // left_rotate // - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_integral::value, - T + std::is_integral::value, + Integral >::type - left_rotate(T x, std::size_t n) { - return (x << n) ^ (x >> (sizeof(T) * CHAR_BIT - n)); + left_rotate(Integral x, int s) { + return sprout::rotl(x, s); } // // right_rotate // - template + template inline SPROUT_CONSTEXPR typename std::enable_if< - std::is_integral::value, - T + std::is_integral::value, + Integral >::type - right_rotate(T x, std::size_t n) { - return (x >> n) ^ (x << (sizeof(T) * CHAR_BIT - n)); + right_rotate(Integral x, int s) { + return sprout::rotr(x, s); } } // namespace sprout diff --git a/sprout/bit/rotl.hpp b/sprout/bit/rotl.hpp new file mode 100644 index 00000000..bb2b2ab1 --- /dev/null +++ b/sprout/bit/rotl.hpp @@ -0,0 +1,30 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_ROTL_HPP +#define SPROUT_BIT_ROTL_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // rotl + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + Integral + >::type + rotl(Integral x, int s) SPROUT_NOEXCEPT { + return (x << s) | sprout::shlr(x, sizeof(x) * CHAR_BIT - s); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_ROTL_HPP diff --git a/sprout/bit/rotr.hpp b/sprout/bit/rotr.hpp new file mode 100644 index 00000000..55ebe970 --- /dev/null +++ b/sprout/bit/rotr.hpp @@ -0,0 +1,30 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_ROTR_HPP +#define SPROUT_BIT_ROTR_HPP + +#include +#include +#include +#include + +namespace sprout { + // + // rotr + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + Integral + >::type + rotr(Integral x, int s) SPROUT_NOEXCEPT { + return sprout::shlr(x, s) | (x << (sizeof(x) * CHAR_BIT - s)); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_ROTR_HPP diff --git a/sprout/bit/shal.hpp b/sprout/bit/shal.hpp new file mode 100644 index 00000000..b0103c89 --- /dev/null +++ b/sprout/bit/shal.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_SHAL_HPP +#define SPROUT_BIT_SHAL_HPP + +#include +#include +#include + +namespace sprout { + // + // shal + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + Integral + >::type + shal(Integral x, int s) SPROUT_NOEXCEPT { + return sprout::shll(x, s); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_SHAL_HPP diff --git a/sprout/bit/shar.hpp b/sprout/bit/shar.hpp new file mode 100644 index 00000000..0cd07677 --- /dev/null +++ b/sprout/bit/shar.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_SHAR_HPP +#define SPROUT_BIT_SHAR_HPP + +#include +#include + +namespace sprout { + // + // shar + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + Integral + >::type + shar(Integral x, int s) SPROUT_NOEXCEPT { + typedef typename std::make_signed::type type; + return static_cast(static_cast(x) >> s); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_SHAR_HPP diff --git a/sprout/bit/shll.hpp b/sprout/bit/shll.hpp new file mode 100644 index 00000000..70faa549 --- /dev/null +++ b/sprout/bit/shll.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_SHLL_HPP +#define SPROUT_BIT_SHLL_HPP + +#include +#include + +namespace sprout { + // + // shll + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + Integral + >::type + shll(Integral x, int s) SPROUT_NOEXCEPT { + typedef typename std::make_unsigned::type type; + return static_cast(static_cast(x) << s); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_SHLL_HPP diff --git a/sprout/bit/shlr.hpp b/sprout/bit/shlr.hpp new file mode 100644 index 00000000..7985a7de --- /dev/null +++ b/sprout/bit/shlr.hpp @@ -0,0 +1,29 @@ +/*============================================================================= + Copyright (c) 2011-2014 Bolero MURAKAMI + https://github.com/bolero-MURAKAMI/Sprout + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +=============================================================================*/ +#ifndef SPROUT_BIT_SHLR_HPP +#define SPROUT_BIT_SHLR_HPP + +#include +#include + +namespace sprout { + // + // shlr + // + template + inline SPROUT_CONSTEXPR typename std::enable_if< + std::is_integral::value, + Integral + >::type + shlr(Integral x, int s) SPROUT_NOEXCEPT { + typedef typename std::make_unsigned::type type; + return static_cast(static_cast(x) >> s); + } +} // namespace sprout + +#endif // #ifndef SPROUT_BIT_SHLR_HPP diff --git a/sprout/checksum/md5.hpp b/sprout/checksum/md5.hpp index 798af35a..e2edc555 100644 --- a/sprout/checksum/md5.hpp +++ b/sprout/checksum/md5.hpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include namespace sprout { static_assert(CHAR_BIT == 8, "CHAR_BIT == 8"); @@ -45,28 +45,28 @@ namespace sprout { public: SPROUT_CONSTEXPR std::uint32_t operator()(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d, std::uint32_t x, std::uint32_t t, std::uint32_t s) const { - return b + sprout::left_rotate(a + sprout::md5_detail::func_f(b, c, d) + x + t, s); + return b + sprout::rotl(a + sprout::md5_detail::func_f(b, c, d) + x + t, s); } }; struct round2_op { public: inline SPROUT_CONSTEXPR std::uint32_t operator()(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d, std::uint32_t x, std::uint32_t t, std::uint32_t s) const { - return b + sprout::left_rotate(a + sprout::md5_detail::func_g(b, c, d) + x + t, s); + return b + sprout::rotl(a + sprout::md5_detail::func_g(b, c, d) + x + t, s); } }; struct round3_op { public: inline SPROUT_CONSTEXPR std::uint32_t operator()(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d, std::uint32_t x, std::uint32_t t, std::uint32_t s) const { - return b + sprout::left_rotate(a + sprout::md5_detail::func_h(b, c, d) + x + t, s); + return b + sprout::rotl(a + sprout::md5_detail::func_h(b, c, d) + x + t, s); } }; struct round4_op { public: inline SPROUT_CONSTEXPR std::uint32_t operator()(std::uint32_t a, std::uint32_t b, std::uint32_t c, std::uint32_t d, std::uint32_t x, std::uint32_t t, std::uint32_t s) const { - return b + sprout::left_rotate(a + sprout::md5_detail::func_i(b, c, d) + x + t, s); + return b + sprout::rotl(a + sprout::md5_detail::func_i(b, c, d) + x + t, s); } }; diff --git a/sprout/checksum/sha1.hpp b/sprout/checksum/sha1.hpp index 08a96816..e4943207 100644 --- a/sprout/checksum/sha1.hpp +++ b/sprout/checksum/sha1.hpp @@ -23,7 +23,7 @@ #include #include #include -#include +#include #ifdef SPROUT_WORKAROUND_NOT_TERMINATE_RECURSIVE_CONSTEXPR_FUNCTION_TEMPLATE # include #endif @@ -61,7 +61,7 @@ namespace sprout { | (block_[i * 4 + 1] << 16) | (block_[i * 4 + 2] << 8) | (block_[i * 4 + 3]) - : sprout::left_rotate( + : sprout::rotl( calc_w(i - 3) ^ calc_w(i - 8) ^ calc_w(i - 14) ^ calc_w(i - 16), 1 ) @@ -98,9 +98,9 @@ namespace sprout { ) const { return process_block_1( - sprout::left_rotate(a, 5) + f + e + k + calc_w(i), + sprout::rotl(a, 5) + f + e + k + calc_w(i), a, - sprout::left_rotate(b, 30), + sprout::rotl(b, 30), c, d, i + 1 @@ -227,9 +227,9 @@ namespace sprout { ) const { return process_block_1( - sprout::left_rotate(a, 5) + f + e + k + calc_w(i), + sprout::rotl(a, 5) + f + e + k + calc_w(i), a, - sprout::left_rotate(b, 30), + sprout::rotl(b, 30), c, d, i + 1 @@ -388,7 +388,7 @@ namespace sprout { w[i] |= (block_[i * 4 + 3]); } for (std::size_t i = 16; i < 80; ++i) { - w[i] = sprout::left_rotate((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1); + w[i] = sprout::rotl((w[i - 3] ^ w[i - 8] ^ w[i - 14] ^ w[i - 16]), 1); } std::uint32_t a = h_[0]; std::uint32_t b = h_[1]; @@ -411,10 +411,10 @@ namespace sprout { f = b ^ c ^ d; k = 0xCA62C1D6; } - unsigned temp = sprout::left_rotate(a, 5) + f + e + k + w[i]; + unsigned temp = sprout::rotl(a, 5) + f + e + k + w[i]; e = d; d = c; - c = sprout::left_rotate(b, 30); + c = sprout::rotl(b, 30); b = a; a = temp; }