From b64ab5924049d34bee555b55b4792a55947610ca Mon Sep 17 00:00:00 2001 From: Avril Date: Tue, 24 Nov 2020 14:24:30 +0000 Subject: [PATCH] skel --- lean/Makefile | 4 ++-- lean/include/panic.h | 14 ++++++++++++++ lean/include/reinterpret.h | 1 + lean/include/reinterpret.hpp | 21 +++++++++++++++------ lean/include/shuffle3.h | 1 - lean/src/main.c | 32 ++++++++++++++++++++++++++++++++ lean/src/reniterpret.cpp | 2 +- 7 files changed, 65 insertions(+), 10 deletions(-) create mode 120000 lean/include/reinterpret.h diff --git a/lean/Makefile b/lean/Makefile index c1e4120..2ab6ad0 100644 --- a/lean/Makefile +++ b/lean/Makefile @@ -4,9 +4,9 @@ test: gcc -c src/*.c -Iinclude -Wall -pedantic --std=gnu11 - g++ -c src/*.cpp -Iinclude -Wall -pedantic --std=gnu11 + g++ -c src/*.cpp -Iinclude -Wall -pedantic --std=gnu++11 gcc -o $@ *.o - + ./$@ clean: rm -f test rm -f *.o diff --git a/lean/include/panic.h b/lean/include/panic.h index f20773d..dff2f82 100644 --- a/lean/include/panic.h +++ b/lean/include/panic.h @@ -12,7 +12,21 @@ struct panicinfo { }; void _do_panic(struct panicinfo pi, const char* fmt, ...) __attribute__((noreturn)); + +#ifdef __cplusplus +extern "C++" { +#include + template + __attribute__((noreturn)) inline void _real_panic(const char* file, const char* function, int line, const char* fmt, const Args&... args) + { + panicinfo i = { file, function, line }; + _do_panic(i, fmt, std::forward(args)...); + } +#define panic(fmt, ...) _real_panic(__FILE__, __func__, __LINE__, fmt __VA_OPT__(,) __VA_ARGS__) +} +#else #define panic(fmt, ...) _do_panic( (struct panicinfo){.file = __FILE__, .function = __func__, .line = __LINE__}, fmt __VA_OPT__(,) __VA_ARGS__) +#endif #ifdef __cplusplus } diff --git a/lean/include/reinterpret.h b/lean/include/reinterpret.h new file mode 120000 index 0000000..5a88a2e --- /dev/null +++ b/lean/include/reinterpret.h @@ -0,0 +1 @@ +reinterpret.hpp \ No newline at end of file diff --git a/lean/include/reinterpret.hpp b/lean/include/reinterpret.hpp index a6feb09..4e75669 100644 --- a/lean/include/reinterpret.hpp +++ b/lean/include/reinterpret.hpp @@ -3,6 +3,15 @@ #include +#ifdef __cplusplus +#define restrict __restrict__ +#include +#include +#else +#include +#include +#endif + #ifdef __cplusplus template struct span { @@ -14,17 +23,17 @@ struct span { auto bytes = size_bytes(); //if (len_b % sizeof(U) != 0) panic("Cannot reinterpret T to U due to unmatch sizing constraints."); - return span(ptr, bytes / sizeof(U)); + return span((U*)ptr, bytes / sizeof(U)); } - inline const T& operator[](std::size_t idx) const + inline const T& operator[](std::size_t i) const { - if (idx >= len) panic("Out of bounds access"); + if (i >= len) panic("Out of bounds access"); return ptr[i]; } - inline T& operator[](std::size_t idx) + inline T& operator[](std::size_t i) { - if(idx >= len) panic("Out of bounds access"); + if(i >= len) panic("Out of bounds access"); return ptr[i]; } @@ -43,7 +52,7 @@ private: extern "C" { #endif -uint64_t* bytes_to_long(uint8_t* ptr, size_t ptr_sz, size_t* restrict_ nsize); +uint64_t* bytes_to_long(uint8_t* ptr, size_t ptr_sz, size_t* restrict nsize); float* bytes_to_float(uint8_t* ptr, size_t ptr_sz, size_t* restrict nsize); #ifdef __cplusplus } diff --git a/lean/include/shuffle3.h b/lean/include/shuffle3.h index 43dfb02..75f72e2 100644 --- a/lean/include/shuffle3.h +++ b/lean/include/shuffle3.h @@ -15,7 +15,6 @@ extern "C" { #endif - #ifdef __cplusplus } #endif diff --git a/lean/src/main.c b/lean/src/main.c index c1c7a46..3197400 100644 --- a/lean/src/main.c +++ b/lean/src/main.c @@ -1,7 +1,16 @@ +#include +#include + +#include + #include #include +#include +_Static_assert(sizeof(float)==sizeof(uint32_t), "float is not 32 bits"); + +/* static void shuffle_file(const char* filename) { panic("unimplemented"); @@ -10,10 +19,33 @@ static void shuffle_file(const char* filename) static void unshuffle_file(const char* filename) { panic("unimplemented"); +}*/ + +static void do_test() +{ + char* string = "Hello world.. how are you?????"; + size_t string_sz = strlen(string); + size_t outsz; + + uint64_t* string_as_longs = bytes_to_long((uint8_t*)string, string_sz, &outsz); + + printf("%s (%lu) -> u64 %lu\n", string, string_sz, outsz); + for (register int i=0;i f32 %lu\n", string, string_sz, outsz); + for (register int i=0;i bytes(ptr, ptr_sz); auto tout = bytes.reinterpret(); *nsize = tout.size(); - return longs.as_ptr(); + return tout.as_ptr(); } extern "C" {