mirror of
https://github.com/vanilla-wiiu/vanilla.git
synced 2025-01-22 08:11:47 -05:00
use significantly faster bit reversing algorithm
This commit is contained in:
parent
72d889ebbc
commit
e1e9128fb1
4 changed files with 174 additions and 12 deletions
|
@ -22,10 +22,16 @@ endif()
|
||||||
install(TARGETS vanilla)
|
install(TARGETS vanilla)
|
||||||
|
|
||||||
if (VANILLA_BUILD_TESTS)
|
if (VANILLA_BUILD_TESTS)
|
||||||
add_executable(bittest
|
function(add_test TEST_NAME TEST_FILES)
|
||||||
test/bittest.c
|
add_executable(${TEST_NAME}
|
||||||
)
|
${TEST_FILES}
|
||||||
|
)
|
||||||
|
|
||||||
target_link_libraries(bittest PRIVATE vanilla m)
|
target_link_libraries(${TEST_NAME} PRIVATE vanilla m)
|
||||||
target_include_directories(bittest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
add_test(bittest "test/bittest.c")
|
||||||
|
add_test(reversebittest "test/reversebit.c")
|
||||||
|
add_test(reversebitstresstest "test/reversebitstresstest.c")
|
||||||
endif()
|
endif()
|
70
lib/test/reversebit.c
Normal file
70
lib/test/reversebit.c
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
/**
|
||||||
|
* Small unit test for ensuring our SPS/PPS bit writing functions are accurate
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
int test1()
|
||||||
|
{
|
||||||
|
const unsigned int control = 0b0000001111001100;
|
||||||
|
const unsigned int expected = 0b0000000011001111;
|
||||||
|
|
||||||
|
unsigned int test = reverse_bits(control, 10);
|
||||||
|
if (test != expected) {
|
||||||
|
printf("FAIL (got %x, expected %x)\n", test, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("SUCCESS\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test2()
|
||||||
|
{
|
||||||
|
const unsigned int control = 0b10110;
|
||||||
|
const unsigned int expected = 0b01101;
|
||||||
|
|
||||||
|
unsigned int test = reverse_bits(control, 5);
|
||||||
|
if (test != expected) {
|
||||||
|
printf("FAIL (got %x, expected %x)\n", test, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("SUCCESS\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test3()
|
||||||
|
{
|
||||||
|
const unsigned int control = 0b110011101010011;
|
||||||
|
const unsigned int expected = 0b110010101110011;
|
||||||
|
|
||||||
|
unsigned int test = reverse_bits(control, 15);
|
||||||
|
if (test != expected) {
|
||||||
|
printf("FAIL (got %x, expected %x)\n", test, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("SUCCESS\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
if (test1()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (test2()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (test3()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
80
lib/test/reversebitstresstest.c
Normal file
80
lib/test/reversebitstresstest.c
Normal file
|
@ -0,0 +1,80 @@
|
||||||
|
/**
|
||||||
|
* Small unit test for ensuring our SPS/PPS bit writing functions are accurate
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <sys/time.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
int test1()
|
||||||
|
{
|
||||||
|
const unsigned int control = 0b0000001111001100;
|
||||||
|
const unsigned int expected = 0b0000000011001111;
|
||||||
|
|
||||||
|
unsigned int test = reverse_bits(control, 10);
|
||||||
|
if (test != expected) {
|
||||||
|
printf("FAIL (got %x, expected %x)\n", test, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test2()
|
||||||
|
{
|
||||||
|
const unsigned int control = 0b10110;
|
||||||
|
const unsigned int expected = 0b01101;
|
||||||
|
|
||||||
|
unsigned int test = reverse_bits(control, 5);
|
||||||
|
if (test != expected) {
|
||||||
|
printf("FAIL (got %x, expected %x)\n", test, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test3()
|
||||||
|
{
|
||||||
|
const unsigned int control = 0b110011101010011;
|
||||||
|
const unsigned int expected = 0b110010101110011;
|
||||||
|
|
||||||
|
unsigned int test = reverse_bits(control, 15);
|
||||||
|
if (test != expected) {
|
||||||
|
printf("FAIL (got %x, expected %x)\n", test, expected);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
struct timeval tv_start, tv_end;
|
||||||
|
|
||||||
|
gettimeofday(&tv_start, NULL);
|
||||||
|
|
||||||
|
for (size_t i = 0; i < 100000000; i++) {
|
||||||
|
if (test1()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (test2()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (test3()) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
gettimeofday(&tv_end, NULL);
|
||||||
|
|
||||||
|
time_t t = (tv_end.tv_sec * 1000000 + tv_end.tv_usec) - (tv_start.tv_sec * 1000000 + tv_start.tv_usec);
|
||||||
|
|
||||||
|
printf("Took %lu microseconds\n", t);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
20
lib/util.c
20
lib/util.c
|
@ -83,13 +83,19 @@ size_t get_millis()
|
||||||
return (s * 1000) + ms;
|
return (s * 1000) + ms;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int reverse_bits(unsigned int b, int bit_count)
|
uint32_t reverse_bits(uint32_t b, int bit_count)
|
||||||
{
|
{
|
||||||
unsigned int result = 0;
|
uint32_t mask = 0b11111111111111110000000000000000;
|
||||||
|
b = (b & mask) >> 16 | (b & ~mask) << 16;
|
||||||
|
mask = 0b11111111000000001111111100000000;
|
||||||
|
b = (b & mask) >> 8 | (b & ~mask) << 8;
|
||||||
|
mask = 0b11110000111100001111000011110000;
|
||||||
|
b = (b & mask) >> 4 | (b & ~mask) << 4;
|
||||||
|
mask = 0b11001100110011001100110011001100;
|
||||||
|
b = (b & mask) >> 2 | (b & ~mask) << 2;
|
||||||
|
mask = 0b10101010101010101010101010101010;
|
||||||
|
b = (b & mask) >> 1 | (b & ~mask) << 1;
|
||||||
|
|
||||||
for (int i = 0; i < bit_count; i++) {
|
b >>= 32 - bit_count;
|
||||||
result |= ((b >> i) & 1) << (bit_count - 1 -i );
|
return b;
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue