From ec8e316be19a5544549adf86a47e8c300fb7a931 Mon Sep 17 00:00:00 2001 From: Alex Zolotarev Date: Mon, 7 Mar 2011 03:55:46 +0000 Subject: [PATCH] Added bits::ror for cyclic right bit shift --- coding/bit_shift.hpp | 26 ++++++ coding/coding.pro | 118 ++++++++++++------------- coding/coding_tests/bit_shift_test.cpp | 25 ++++++ coding/coding_tests/coding_tests.pro | 47 +++++----- 4 files changed, 134 insertions(+), 82 deletions(-) create mode 100644 coding/bit_shift.hpp create mode 100644 coding/coding_tests/bit_shift_test.cpp diff --git a/coding/bit_shift.hpp b/coding/bit_shift.hpp new file mode 100644 index 0000000000..134241a8da --- /dev/null +++ b/coding/bit_shift.hpp @@ -0,0 +1,26 @@ +#pragma once + +#include "../std/limits.hpp" + +namespace bits +{ + template + struct bits_of + { + enum + { + value = sizeof(T) * CHAR_BIT + }; + }; + + template + T ror(T val, unsigned int n) + { + enum + { + bits = bits_of::value + }; + n = n % bits; + return (val >> n) | (val << (bits - n)); + } +} diff --git a/coding/coding.pro b/coding/coding.pro index 5ff0e3a9ca..ed831031be 100644 --- a/coding/coding.pro +++ b/coding/coding.pro @@ -9,64 +9,64 @@ DEPENDENCIES = bzip2 zlib base include($$ROOT_DIR/common.pri) SOURCES += \ - hex.cpp \ - file_reader.cpp \ - file_writer.cpp \ - lodepng.cpp \ - file_container.cpp \ - bzip2_compressor.cpp \ - gzip_compressor.cpp \ - timsort/timsort.cpp \ - + hex.cpp \ + file_reader.cpp \ + file_writer.cpp \ + lodepng.cpp \ + file_container.cpp \ + bzip2_compressor.cpp \ + gzip_compressor.cpp \ + timsort/timsort.cpp \ HEADERS += \ - internal/xmlparser.h \ - internal/expat_impl.h \ - internal/file_data.hpp \ - internal/file64_api.hpp \ - parse_xml.hpp \ - varint.hpp \ - mm_vector.hpp \ - mm_bit_vector.hpp \ - mm_base.hpp \ - endianness.hpp \ - byte_stream.hpp \ - var_serial_vector.hpp \ - hex.hpp \ - mm_compact_trie.hpp \ - mm_compact_tree.hpp \ - compact_trie_builder.hpp \ - compact_tree_builder.hpp \ - bit_vector_builder.hpp \ - dd_vector.hpp \ - dd_bit_vector.hpp \ - dd_base.hpp \ - strutil.hpp \ - writer.hpp \ - write_to_sink.hpp \ - reader.hpp \ - dd_bit_rank_directory.hpp \ - dd_compact_tree.hpp \ - dd_compact_trie.hpp \ - diff.hpp \ - diff_patch_common.hpp \ - source.hpp \ - lodepng.hpp \ - lodepng_io.hpp \ - lodepng_io_private.hpp \ - var_record_reader.hpp \ - file_sort.hpp \ - file_reader.hpp \ - file_writer.hpp \ - reader_cache.hpp \ - buffer_reader.hpp \ - streams.hpp \ - streams_sink.hpp \ - streams_common.hpp \ - file_container.hpp \ - polymorph_reader.hpp \ - coder.hpp \ - coder_util.hpp \ - bzip2_compressor.hpp \ - gzip_compressor.hpp \ - timsort/timsort.hpp \ + internal/xmlparser.h \ + internal/expat_impl.h \ + internal/file_data.hpp \ + internal/file64_api.hpp \ + parse_xml.hpp \ + varint.hpp \ + mm_vector.hpp \ + mm_bit_vector.hpp \ + mm_base.hpp \ + endianness.hpp \ + byte_stream.hpp \ + var_serial_vector.hpp \ + hex.hpp \ + mm_compact_trie.hpp \ + mm_compact_tree.hpp \ + compact_trie_builder.hpp \ + compact_tree_builder.hpp \ + bit_vector_builder.hpp \ + dd_vector.hpp \ + dd_bit_vector.hpp \ + dd_base.hpp \ + strutil.hpp \ + writer.hpp \ + write_to_sink.hpp \ + reader.hpp \ + dd_bit_rank_directory.hpp \ + dd_compact_tree.hpp \ + dd_compact_trie.hpp \ + diff.hpp \ + diff_patch_common.hpp \ + source.hpp \ + lodepng.hpp \ + lodepng_io.hpp \ + lodepng_io_private.hpp \ + var_record_reader.hpp \ + file_sort.hpp \ + file_reader.hpp \ + file_writer.hpp \ + reader_cache.hpp \ + buffer_reader.hpp \ + streams.hpp \ + streams_sink.hpp \ + streams_common.hpp \ + file_container.hpp \ + polymorph_reader.hpp \ + coder.hpp \ + coder_util.hpp \ + bzip2_compressor.hpp \ + gzip_compressor.hpp \ + timsort/timsort.hpp \ + bit_shift.hpp \ diff --git a/coding/coding_tests/bit_shift_test.cpp b/coding/coding_tests/bit_shift_test.cpp new file mode 100644 index 0000000000..77c7f17078 --- /dev/null +++ b/coding/coding_tests/bit_shift_test.cpp @@ -0,0 +1,25 @@ +#include "../../base/SRC_FIRST.hpp" +#include "../../testing/testing.hpp" + +#include "../../std/limits.hpp" + +#include "../bit_shift.hpp" + + +UNIT_TEST(BitShift) +{ + TEST_EQUAL(INT_MIN, bits::ror(1, 1), ()); + uint8_t ui8 = 1; + TEST_EQUAL(0x80U, bits::ror(ui8, 1), ()); + uint16_t ui16 = 1; + TEST_EQUAL(0x8000U, bits::ror(ui16, 1), ()); + uint32_t ui32 = 1; + TEST_EQUAL(0x80000000U, bits::ror(ui32, 1), ()); + uint64_t ui64 = 1; + TEST_EQUAL(0x8000000000000000U, bits::ror(ui64, 1), ()); + + uint16_t v = 0x58b1; + TEST_EQUAL(0x2b16, bits::ror(v, 3), ()); + TEST_EQUAL(v, bits::ror(v, 32), ()); + TEST_EQUAL(v, bits::ror(v, 0), ()); +} diff --git a/coding/coding_tests/coding_tests.pro b/coding/coding_tests/coding_tests.pro index aec87644e9..14a2d24573 100644 --- a/coding/coding_tests/coding_tests.pro +++ b/coding/coding_tests/coding_tests.pro @@ -10,28 +10,29 @@ DEPENDENCIES = coding base bzip2 zlib include($$ROOT_DIR/common.pri) SOURCES += ../../testing/testingmain.cpp \ - endianness_test.cpp \ - varint_test.cpp \ - mm_bit_vector_test.cpp \ - mm_compact_trie_test.cpp \ - mem_file_reader_test.cpp \ - mem_file_writer_test.cpp \ - var_serial_vector_test.cpp \ - hex_test.cpp \ - dd_vector_test.cpp \ - diff_test.cpp \ - png_decoder_test.cpp \ - reader_test.cpp \ - writer_test.cpp \ - var_record_reader_test.cpp \ - file_sort_test.cpp \ - reader_cache_test.cpp \ - file_container_test.cpp \ - strutil_test.cpp \ - bzip2_test.cpp \ - gzip_test.cpp \ - coder_util_test.cpp \ + endianness_test.cpp \ + varint_test.cpp \ + mm_bit_vector_test.cpp \ + mm_compact_trie_test.cpp \ + mem_file_reader_test.cpp \ + mem_file_writer_test.cpp \ + var_serial_vector_test.cpp \ + hex_test.cpp \ + dd_vector_test.cpp \ + diff_test.cpp \ + png_decoder_test.cpp \ + reader_test.cpp \ + writer_test.cpp \ + var_record_reader_test.cpp \ + file_sort_test.cpp \ + reader_cache_test.cpp \ + file_container_test.cpp \ + strutil_test.cpp \ + bzip2_test.cpp \ + gzip_test.cpp \ + coder_util_test.cpp \ + bit_shift_test.cpp \ HEADERS += \ - reader_test.hpp \ - coder_test.hpp \ + reader_test.hpp \ + coder_test.hpp \