diff options
author | Jack Lloyd <[email protected]> | 2018-09-19 11:56:34 -0400 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2018-10-01 12:54:01 -0400 |
commit | 2230dd96311c8db3294bf10cf58d739061d26102 (patch) | |
tree | e6a42b2c11f6d158cd70243e0bb401f351e3ab94 /src/lib | |
parent | 8eb900731da945d276f723534af09592213ee582 (diff) |
Make a few simple functions constexpr
This is primarily just to verify that C++11 constexpr works.
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/block/aes/aes.cpp | 18 | ||||
-rw-r--r-- | src/lib/utils/bit_ops.h | 2 | ||||
-rw-r--r-- | src/lib/utils/ct_utils.h | 20 | ||||
-rw-r--r-- | src/lib/utils/loadstor.h | 10 | ||||
-rw-r--r-- | src/lib/utils/rotate.h | 4 | ||||
-rw-r--r-- | src/lib/utils/rounding.h | 7 |
6 files changed, 29 insertions, 32 deletions
diff --git a/src/lib/block/aes/aes.cpp b/src/lib/block/aes/aes.cpp index c025530a4..0c18e93ee 100644 --- a/src/lib/block/aes/aes.cpp +++ b/src/lib/block/aes/aes.cpp @@ -99,15 +99,15 @@ const uint8_t SD[256] = { 0x17, 0x2B, 0x04, 0x7E, 0xBA, 0x77, 0xD6, 0x26, 0xE1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0C, 0x7D }; -inline uint8_t xtime(uint8_t s) { return static_cast<uint8_t>(s << 1) ^ ((s >> 7) * 0x1B); } -inline uint8_t xtime4(uint8_t s) { return xtime(xtime(s)); } -inline uint8_t xtime8(uint8_t s) { return xtime(xtime(xtime(s))); } - -inline uint8_t xtime3(uint8_t s) { return xtime(s) ^ s; } -inline uint8_t xtime9(uint8_t s) { return xtime8(s) ^ s; } -inline uint8_t xtime11(uint8_t s) { return xtime8(s) ^ xtime(s) ^ s; } -inline uint8_t xtime13(uint8_t s) { return xtime8(s) ^ xtime4(s) ^ s; } -inline uint8_t xtime14(uint8_t s) { return xtime8(s) ^ xtime4(s) ^ xtime(s); } +inline constexpr uint8_t xtime(uint8_t s) { return static_cast<uint8_t>(s << 1) ^ ((s >> 7) * 0x1B); } +inline constexpr uint8_t xtime4(uint8_t s) { return xtime(xtime(s)); } +inline constexpr uint8_t xtime8(uint8_t s) { return xtime(xtime(xtime(s))); } + +inline constexpr uint8_t xtime3(uint8_t s) { return xtime(s) ^ s; } +inline constexpr uint8_t xtime9(uint8_t s) { return xtime8(s) ^ s; } +inline constexpr uint8_t xtime11(uint8_t s) { return xtime8(s) ^ xtime(s) ^ s; } +inline constexpr uint8_t xtime13(uint8_t s) { return xtime8(s) ^ xtime4(s) ^ s; } +inline constexpr uint8_t xtime14(uint8_t s) { return xtime8(s) ^ xtime4(s) ^ xtime(s); } inline uint32_t SE_word(uint32_t x) { diff --git a/src/lib/utils/bit_ops.h b/src/lib/utils/bit_ops.h index c7e401492..a9924b232 100644 --- a/src/lib/utils/bit_ops.h +++ b/src/lib/utils/bit_ops.h @@ -22,7 +22,7 @@ namespace Botan { * @return true iff arg is 2^n for some n > 0 */ template<typename T> -inline bool is_power_of_2(T arg) +inline constexpr bool is_power_of_2(T arg) { return ((arg != 0 && arg != 1) && ((arg & (arg-1)) == 0)); } diff --git a/src/lib/utils/ct_utils.h b/src/lib/utils/ct_utils.h index f4f881871..0303d2a6c 100644 --- a/src/lib/utils/ct_utils.h +++ b/src/lib/utils/ct_utils.h @@ -78,14 +78,14 @@ inline void unpoison(T& p) /* Mask generation */ template<typename T> -inline T expand_top_bit(T a) +inline constexpr T expand_top_bit(T a) { static_assert(std::is_unsigned<T>::value, "unsigned integer type required"); return static_cast<T>(0) - (a >> (sizeof(T)*8-1)); } template<typename T> -inline T is_zero(T x) +inline constexpr T is_zero(T x) { static_assert(std::is_unsigned<T>::value, "unsigned integer type required"); return expand_top_bit<T>(~x & (x - 1)); @@ -100,51 +100,51 @@ inline T is_zero(T x) * select. */ template<typename T> -inline T expand_mask(T x) +inline constexpr T expand_mask(T x) { static_assert(std::is_unsigned<T>::value, "unsigned integer type required"); return ~is_zero(x); } template<typename T> -inline T select(T mask, T from0, T from1) +inline constexpr T select(T mask, T from0, T from1) { static_assert(std::is_unsigned<T>::value, "unsigned integer type required"); return static_cast<T>((from0 & mask) | (from1 & ~mask)); } template<typename T> -inline T select2(T mask0, T val0, T mask1, T val1, T val2) +inline constexpr T select2(T mask0, T val0, T mask1, T val1, T val2) { return select<T>(mask0, val0, select<T>(mask1, val1, val2)); } template<typename T> -inline T select3(T mask0, T val0, T mask1, T val1, T mask2, T val2, T val3) +inline constexpr T select3(T mask0, T val0, T mask1, T val1, T mask2, T val2, T val3) { return select2<T>(mask0, val0, mask1, val1, select<T>(mask2, val2, val3)); } template<typename PredT, typename ValT> -inline ValT val_or_zero(PredT pred_val, ValT val) +inline constexpr ValT val_or_zero(PredT pred_val, ValT val) { return select(CT::expand_mask<ValT>(pred_val), val, static_cast<ValT>(0)); } template<typename T> -inline T is_equal(T x, T y) +inline constexpr T is_equal(T x, T y) { return is_zero<T>(x ^ y); } template<typename T> -inline T is_less(T a, T b) +inline constexpr T is_less(T a, T b) { return expand_top_bit<T>(a ^ ((a^b) | ((a-b)^a))); } template<typename T> -inline T is_lte(T a, T b) +inline constexpr T is_lte(T a, T b) { return CT::is_less(a, b) | CT::is_equal(a, b); } diff --git a/src/lib/utils/loadstor.h b/src/lib/utils/loadstor.h index 70ad20591..2fb1543b0 100644 --- a/src/lib/utils/loadstor.h +++ b/src/lib/utils/loadstor.h @@ -36,7 +36,7 @@ namespace Botan { * @param input the value to extract from * @return byte byte_num of input */ -template<typename T> inline uint8_t get_byte(size_t byte_num, T input) +template<typename T> inline constexpr uint8_t get_byte(size_t byte_num, T input) { return static_cast<uint8_t>( input >> (((~byte_num)&(sizeof(T)-1)) << 3) @@ -49,7 +49,7 @@ template<typename T> inline uint8_t get_byte(size_t byte_num, T input) * @param i1 the second byte * @return i0 || i1 */ -inline uint16_t make_uint16(uint8_t i0, uint8_t i1) +inline constexpr uint16_t make_uint16(uint8_t i0, uint8_t i1) { return static_cast<uint16_t>((static_cast<uint16_t>(i0) << 8) | i1); } @@ -62,7 +62,7 @@ inline uint16_t make_uint16(uint8_t i0, uint8_t i1) * @param i3 the fourth byte * @return i0 || i1 || i2 || i3 */ -inline uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3) +inline constexpr uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3) { return ((static_cast<uint32_t>(i0) << 24) | (static_cast<uint32_t>(i1) << 16) | @@ -82,8 +82,8 @@ inline uint32_t make_uint32(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3) * @param i7 the eighth byte * @return i0 || i1 || i2 || i3 || i4 || i5 || i6 || i7 */ -inline uint64_t make_uint64(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3, - uint8_t i4, uint8_t i5, uint8_t i6, uint8_t i7) +inline constexpr uint64_t make_uint64(uint8_t i0, uint8_t i1, uint8_t i2, uint8_t i3, + uint8_t i4, uint8_t i5, uint8_t i6, uint8_t i7) { return ((static_cast<uint64_t>(i0) << 56) | (static_cast<uint64_t>(i1) << 48) | diff --git a/src/lib/utils/rotate.h b/src/lib/utils/rotate.h index 4bb76c9ed..d927b93b7 100644 --- a/src/lib/utils/rotate.h +++ b/src/lib/utils/rotate.h @@ -18,7 +18,7 @@ namespace Botan { * @return input rotated left by ROT bits */ template<size_t ROT, typename T> -inline T rotl(T input) +inline constexpr T rotl(T input) { static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant"); return static_cast<T>((input << ROT) | (input >> (8*sizeof(T) - ROT))); @@ -30,7 +30,7 @@ inline T rotl(T input) * @return input rotated right by ROT bits */ template<size_t ROT, typename T> -inline T rotr(T input) +inline constexpr T rotr(T input) { static_assert(ROT > 0 && ROT < 8*sizeof(T), "Invalid rotation constant"); return static_cast<T>((input >> ROT) | (input << (8*sizeof(T) - ROT))); diff --git a/src/lib/utils/rounding.h b/src/lib/utils/rounding.h index a03e3a4ee..ae0aedf07 100644 --- a/src/lib/utils/rounding.h +++ b/src/lib/utils/rounding.h @@ -34,12 +34,9 @@ inline size_t round_up(size_t n, size_t align_to) * @return n rounded down to a multiple of align_to */ template<typename T> -inline T round_down(T n, T align_to) +inline constexpr T round_down(T n, T align_to) { - if(align_to == 0) - return n; - - return (n - (n % align_to)); + return (align_to == 0) ? n : (n - (n % align_to)); } /** |