Avril 4 years ago
parent 30016c2f7c
commit b64ab59240
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -4,9 +4,9 @@
test: test:
gcc -c src/*.c -Iinclude -Wall -pedantic --std=gnu11 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 gcc -o $@ *.o
./$@
clean: clean:
rm -f test rm -f test
rm -f *.o rm -f *.o

@ -12,7 +12,21 @@ struct panicinfo {
}; };
void _do_panic(struct panicinfo pi, const char* fmt, ...) __attribute__((noreturn)); void _do_panic(struct panicinfo pi, const char* fmt, ...) __attribute__((noreturn));
#ifdef __cplusplus
extern "C++" {
#include <utility>
template<typename... Args>
__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>(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__) #define panic(fmt, ...) _do_panic( (struct panicinfo){.file = __FILE__, .function = __func__, .line = __LINE__}, fmt __VA_OPT__(,) __VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -0,0 +1 @@
reinterpret.hpp

@ -3,6 +3,15 @@
#include <panic.h> #include <panic.h>
#ifdef __cplusplus
#define restrict __restrict__
#include <cstddef>
#include <cstdint>
#else
#include <stddef.h>
#include <stdint.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
template<typename T> template<typename T>
struct span { struct span {
@ -14,17 +23,17 @@ struct span {
auto bytes = size_bytes(); auto bytes = size_bytes();
//if (len_b % sizeof(U) != 0) panic("Cannot reinterpret T to U due to unmatch sizing constraints."); //if (len_b % sizeof(U) != 0) panic("Cannot reinterpret T to U due to unmatch sizing constraints.");
return span<U>(ptr, bytes / sizeof(U)); return span<U>((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]; 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]; return ptr[i];
} }
@ -43,7 +52,7 @@ private:
extern "C" { extern "C" {
#endif #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); float* bytes_to_float(uint8_t* ptr, size_t ptr_sz, size_t* restrict nsize);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -15,7 +15,6 @@ extern "C" {
#endif #endif
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

@ -1,7 +1,16 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <shuffle3.h> #include <shuffle3.h>
#include <panic.h> #include <panic.h>
#include <reinterpret.h>
_Static_assert(sizeof(float)==sizeof(uint32_t), "float is not 32 bits");
/*
static void shuffle_file(const char* filename) static void shuffle_file(const char* filename)
{ {
panic("unimplemented"); panic("unimplemented");
@ -10,10 +19,33 @@ static void shuffle_file(const char* filename)
static void unshuffle_file(const char* filename) static void unshuffle_file(const char* filename)
{ {
panic("unimplemented"); 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<outsz;i++) {
printf(" %lu\n", string_as_longs[i]);
}
printf("OK!\n\n");
float* string_as_floats = bytes_to_float((uint8_t*)string, string_sz, &outsz);
printf("%s (%lu) -> f32 %lu\n", string, string_sz, outsz);
for (register int i=0;i<outsz;i++) {
printf(" %f\n", string_as_floats[i]);
}
printf("OK!\n\n");
} }
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
do_test();
return 0; return 0;
} }

@ -7,7 +7,7 @@ static inline T* bytes_to_t(std::uint8_t* ptr, std::size_t ptr_sz, std::size_t*
span<uint8_t> bytes(ptr, ptr_sz); span<uint8_t> bytes(ptr, ptr_sz);
auto tout = bytes.reinterpret<T>(); auto tout = bytes.reinterpret<T>();
*nsize = tout.size(); *nsize = tout.size();
return longs.as_ptr(); return tout.as_ptr();
} }
extern "C" { extern "C" {

Loading…
Cancel
Save