Merge pull request #48080 from aaronfranke/real-serialization
Binary serialization for reals
This commit is contained in:
+345
-178
File diff suppressed because it is too large
Load Diff
@@ -31,10 +31,18 @@
|
||||
#ifndef MARSHALLS_H
|
||||
#define MARSHALLS_H
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
#include "core/object/ref_counted.h"
|
||||
#include "core/typedefs.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
// uintr_t is only for pairing with real_t, and we only need it in here.
|
||||
#ifdef REAL_T_IS_DOUBLE
|
||||
typedef uint64_t uintr_t;
|
||||
#else
|
||||
typedef uint32_t uintr_t;
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Miscellaneous helpers for marshalling data types, and encoding
|
||||
* in an endian independent way
|
||||
@@ -50,6 +58,12 @@ union MarshallDouble {
|
||||
double d; ///< double
|
||||
};
|
||||
|
||||
// Behaves like one of the above, depending on compilation setting.
|
||||
union MarshallReal {
|
||||
uintr_t i;
|
||||
real_t r;
|
||||
};
|
||||
|
||||
static inline unsigned int encode_uint16(uint16_t p_uint, uint8_t *p_arr) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
*p_arr = p_uint & 0xFF;
|
||||
@@ -96,6 +110,24 @@ static inline unsigned int encode_double(double p_double, uint8_t *p_arr) {
|
||||
return sizeof(uint64_t);
|
||||
}
|
||||
|
||||
static inline unsigned int encode_uintr(uintr_t p_uint, uint8_t *p_arr) {
|
||||
for (size_t i = 0; i < sizeof(uintr_t); i++) {
|
||||
*p_arr = p_uint & 0xFF;
|
||||
p_arr++;
|
||||
p_uint >>= 8;
|
||||
}
|
||||
|
||||
return sizeof(uintr_t);
|
||||
}
|
||||
|
||||
static inline unsigned int encode_real(real_t p_real, uint8_t *p_arr) {
|
||||
MarshallReal mr;
|
||||
mr.r = p_real;
|
||||
encode_uintr(mr.i, p_arr);
|
||||
|
||||
return sizeof(uintr_t);
|
||||
}
|
||||
|
||||
static inline int encode_cstring(const char *p_string, uint8_t *p_data) {
|
||||
int len = 0;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user