Rename rotate_left to the more friendly rotatel

This commit is contained in:
Danny Robson 2013-03-11 20:41:19 +11:00
parent 011ac70a9d
commit 990d5ab806
4 changed files with 30 additions and 3 deletions

View File

@ -35,7 +35,8 @@ const uint8_t BITMASK_8BITS = 0xFF;
template <typename T> template <typename T>
T rotate_left (const T &value, size_t magnitude) { T
rotatel (const T &value, size_t magnitude) {
magnitude %= sizeof (T) * 8; magnitude %= sizeof (T) * 8;
return (value << magnitude) | (value >> sizeof (value) * 8 - magnitude); return (value << magnitude) | (value >> sizeof (value) * 8 - magnitude);
} }

5
test/.gitignore vendored
View File

@ -1,13 +1,13 @@
/*.log
/*.trs
/adler* /adler*
/backtrace* /backtrace*
/bitwise
/checksum* /checksum*
/float* /float*
/hton* /hton*
/ip* /ip*
/json /json
/json-check* /json-check*
/*.log
/maths* /maths*
/matrix* /matrix*
/option /option
@ -15,4 +15,5 @@
/range* /range*
/region* /region*
/signal* /signal*
/*.trs
/version* /version*

View File

@ -9,6 +9,7 @@ AM_LDFLAGS = $(COMMON_LDFLAGS)
TEST_BIN = \ TEST_BIN = \
backtrace \ backtrace \
bitwise \
checksum \ checksum \
float \ float \
hton \ hton \
@ -31,6 +32,9 @@ backtrace_CPPFLAGS = $(COMMON_CXXFLAGS)
backtrace_LDADD = $(builddir)/../libutil.la backtrace_LDADD = $(builddir)/../libutil.la
backtrace_SOURCES = backtrace.cpp backtrace_SOURCES = backtrace.cpp
bitwise_LDADD = $(builddir)/../libutil.la
bitwise_SOURCES = bitwise.cpp
checksum_LDADD = $(builddir)/../libutil.la checksum_LDADD = $(builddir)/../libutil.la
checksum_SOURCES = checksum.cpp checksum_SOURCES = checksum.cpp

21
test/bitwise.cpp Normal file
View File

@ -0,0 +1,21 @@
#include "../bitwise.hpp"
int
main (int, char**) {
CHECK_EQ (rotatel (uint8_t (0x0F), 0), 0x0F);
CHECK_EQ (rotatel (uint8_t (0x0F), 4), 0xF0);
CHECK_EQ (rotatel (uint8_t (0xF0), 4), 0x0F);
CHECK_EQ (rotatel (uint8_t (0x0F), 8), 0x0F);
CHECK_EQ (rotatel (uint16_t (0xABCD), 0), 0xABCD);
CHECK_EQ (rotatel (uint16_t (0xABCD), 4), 0xBCDA);
CHECK_EQ (rotatel (uint16_t (0xABCD), 8), 0xCDAB);
CHECK_EQ (rotatel (uint16_t (0xABCD), 12), 0xDABC);
CHECK_EQ (rotatel (uint16_t (0xABCD), 16), 0xABCD);
CHECK_EQ (rotatel (uint32_t (0x12345670), 12), 0x45670123);
CHECK_EQ (rotatel (uint64_t (0x1234567890ABCDEF), 12), 0x4567890ABCDEF123);
return 0;
}