From 6917758aa6ff5d46880f7bb0ba21856066791a6d Mon Sep 17 00:00:00 2001 From: SamerKhshiboun Date: Sun, 17 Mar 2024 21:12:27 +0200 Subject: [PATCH 1/2] add byte manipulation functions --- .../include/rsutils/byte-manipulation.h | 15 ++++++++ third-party/rsutils/src/byte-manipulation.cpp | 35 +++++++++++++++++++ .../number/byte-array/test-byte-array.cpp | 21 +++++++++++ 3 files changed, 71 insertions(+) create mode 100644 third-party/rsutils/include/rsutils/byte-manipulation.h create mode 100644 third-party/rsutils/src/byte-manipulation.cpp create mode 100644 unit-tests/rsutils/number/byte-array/test-byte-array.cpp diff --git a/third-party/rsutils/include/rsutils/byte-manipulation.h b/third-party/rsutils/include/rsutils/byte-manipulation.h new file mode 100644 index 0000000000..3cb96fd61d --- /dev/null +++ b/third-party/rsutils/include/rsutils/byte-manipulation.h @@ -0,0 +1,15 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2024 Intel Corporation. All Rights Reserved. + +#pragma once + +#include + +namespace rsutils +{ + namespace number + { + uint8_t reverse_bits(uint8_t b); + void reverse_byte_array_bits(uint8_t* byte_array, int size); + } +} diff --git a/third-party/rsutils/src/byte-manipulation.cpp b/third-party/rsutils/src/byte-manipulation.cpp new file mode 100644 index 0000000000..e9319bdea7 --- /dev/null +++ b/third-party/rsutils/src/byte-manipulation.cpp @@ -0,0 +1,35 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2024 Intel Corporation. All Rights Reserved. + +#include + +namespace rsutils +{ + namespace number + { + // A helper function that takes a byte and reverses its bits + // e.g. intput: 00110101 output: 10101100 + + // First the left four bits are swapped with the right four bits. + // Then all adjacent pairs are swapped and then all adjacent single bits. + // This results in a reversed order. + uint8_t reverse_bits(uint8_t b) + { + b = (b & 0xF0) >> 4 | (b & 0x0F) << 4; + b = (b & 0xCC) >> 2 | (b & 0x33) << 2; + b = (b & 0xAA) >> 1 | (b & 0x55) << 1; + return b; + } + + // A helper function that takes a byte rray of a given size, + // and reverses the bits order of for each byte of this array + // e.g. intput: byteArray = {byte0=00110101, byte1=11110000}, size = 2 + // output: byteArray [ byte0=10101100, byte1=00001111] + void reverse_byte_array_bits(uint8_t* byte_array, int size) { + for (int i = 0; i < size; ++i) + { + byte_array[i] = reverse_bits(byte_array[i]); + } + } + } +} diff --git a/unit-tests/rsutils/number/byte-array/test-byte-array.cpp b/unit-tests/rsutils/number/byte-array/test-byte-array.cpp new file mode 100644 index 0000000000..1852d81d5e --- /dev/null +++ b/unit-tests/rsutils/number/byte-array/test-byte-array.cpp @@ -0,0 +1,21 @@ +// License: Apache 2.0. See LICENSE file in root directory. +// Copyright(c) 2023 Intel Corporation. All Rights Reserved. + +//#cmake:dependencies rsutils + +#include +#include +#include + +using byte = uint8_t; +using bytearray = std::vector< byte >; + + +namespace { + + +TEST_CASE( "test1" ) +{ +} + +} From 8ddb40a4e492d3fe06794e8022a81bc44003b062 Mon Sep 17 00:00:00 2001 From: SamerKhshiboun Date: Sun, 17 Mar 2024 22:14:58 +0200 Subject: [PATCH 2/2] update byte functions --- .../rsutils/{ => number}/byte-manipulation.h | 0 third-party/rsutils/src/byte-manipulation.cpp | 6 +- .../number/byte-array/test-byte-array.cpp | 56 +++++++++++++++++-- 3 files changed, 53 insertions(+), 9 deletions(-) rename third-party/rsutils/include/rsutils/{ => number}/byte-manipulation.h (100%) diff --git a/third-party/rsutils/include/rsutils/byte-manipulation.h b/third-party/rsutils/include/rsutils/number/byte-manipulation.h similarity index 100% rename from third-party/rsutils/include/rsutils/byte-manipulation.h rename to third-party/rsutils/include/rsutils/number/byte-manipulation.h diff --git a/third-party/rsutils/src/byte-manipulation.cpp b/third-party/rsutils/src/byte-manipulation.cpp index e9319bdea7..76284703a7 100644 --- a/third-party/rsutils/src/byte-manipulation.cpp +++ b/third-party/rsutils/src/byte-manipulation.cpp @@ -1,7 +1,7 @@ // License: Apache 2.0. See LICENSE file in root directory. // Copyright(c) 2024 Intel Corporation. All Rights Reserved. -#include +#include namespace rsutils { @@ -21,8 +21,8 @@ namespace rsutils return b; } - // A helper function that takes a byte rray of a given size, - // and reverses the bits order of for each byte of this array + // A helper function that takes a byte array of a given size, + // and reverses the bits order of for each byte in this array // e.g. intput: byteArray = {byte0=00110101, byte1=11110000}, size = 2 // output: byteArray [ byte0=10101100, byte1=00001111] void reverse_byte_array_bits(uint8_t* byte_array, int size) { diff --git a/unit-tests/rsutils/number/byte-array/test-byte-array.cpp b/unit-tests/rsutils/number/byte-array/test-byte-array.cpp index 1852d81d5e..b7d896c040 100644 --- a/unit-tests/rsutils/number/byte-array/test-byte-array.cpp +++ b/unit-tests/rsutils/number/byte-array/test-byte-array.cpp @@ -1,21 +1,65 @@ // License: Apache 2.0. See LICENSE file in root directory. -// Copyright(c) 2023 Intel Corporation. All Rights Reserved. +// Copyright(c) 2024 Intel Corporation. All Rights Reserved. //#cmake:dependencies rsutils #include #include -#include - -using byte = uint8_t; -using bytearray = std::vector< byte >; +using namespace rsutils::number; namespace { -TEST_CASE( "test1" ) +TEST_CASE( "test reversing single byte - case 1" ) +{ + uint8_t byte = 0x55; // b'01010101 + uint8_t reversed_byte = 0xAA; // b'10101010 + CHECK(reverse_bits(byte) == reversed_byte); // little-endian +} + +TEST_CASE("test reversing single byte - case 2") +{ + uint8_t byte = 0x1; // b'00000001 + uint8_t reversed_byte = 0x80; // b'10000000 + CHECK(reverse_bits(byte) == reversed_byte); +} + +TEST_CASE("test reversing single byte - case 3") +{ + uint8_t byte = 0x0; // b'00000000 + uint8_t reversed_byte = 0x0; // b'00000000 + CHECK(reverse_bits(byte) == reversed_byte); +} + +TEST_CASE("test reversing single byte - case 4") +{ + uint8_t byte = 0xFF; // b'11111111 + uint8_t reversed_byte = 0xFF; // b'11111111 + CHECK(reverse_bits(byte) == reversed_byte); +} + +TEST_CASE("test reversing array of bytes of size 1") { + std::vector bytes_arr = { 0x55 }; // { b'01010101 } + uint8_t reversed_byte_at_0 = 0xAA; // b'10101010 + reverse_byte_array_bits(bytes_arr.data(), 1); + CHECK(bytes_arr[0] == reversed_byte_at_0); // little-endian } +TEST_CASE("test reversing array of bytes of size 4") +{ + std::vector bytes_arr = { 0x55, 0x01, 0x80, 0xF0 }; + // bytes_arr = { b'01010101, b'00000001, b'10000000, b'11110000 } + std::vector expected_reversed_bytes = { 0xAA, 0x80, 0x01, 0xF }; + // expected_reversed_bytes = { b'10101010, b'10000000, b'00000001, b'00001111} + + reverse_byte_array_bits(bytes_arr.data(), static_cast(bytes_arr.size())); + for (int i = 0; i < bytes_arr.size(); ++i) + { + CHECK(bytes_arr[i] == expected_reversed_bytes[i]); + } +} + + }