diff options
author | SmallJoker <SmallJoker@users.noreply.github.com> | 2018-12-13 11:20:57 +0100 |
---|---|---|
committer | Loïc Blot <nerzhul@users.noreply.github.com> | 2018-12-13 11:20:57 +0100 |
commit | 839e935ba0572c592a791cc4dd4df4a9f6d2d260 (patch) | |
tree | 6a7d59e1804879dc24e9e4b1e9ce0294d681bedd /src/util/serialize.h | |
parent | 8471d027b9896af74d2c1ab3b0068af4953e5ca0 (diff) | |
download | minetest-839e935ba0572c592a791cc4dd4df4a9f6d2d260.tar.gz minetest-839e935ba0572c592a791cc4dd4df4a9f6d2d260.tar.bz2 minetest-839e935ba0572c592a791cc4dd4df4a9f6d2d260.zip |
Network: Send IEEE floats (#7768)
Diffstat (limited to 'src/util/serialize.h')
-rw-r--r-- | src/util/serialize.h | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/src/util/serialize.h b/src/util/serialize.h index 89bc0b097..016491a2c 100644 --- a/src/util/serialize.h +++ b/src/util/serialize.h @@ -22,6 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "irrlichttypes_bloated.h" #include "exceptions.h" // for SerializationError #include "debug.h" // for assert +#include "ieee_float.h" #include "config.h" #if HAVE_ENDIAN_H @@ -60,6 +61,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #define LONG_STRING_MAX_LEN (64 * 1024 * 1024) +extern FloatType g_serialize_f32_type; + #if HAVE_ENDIAN_H // use machine native byte swapping routines // Note: memcpy below is optimized out by modern compilers @@ -188,6 +191,25 @@ inline f32 readF1000(const u8 *data) return (f32)readS32(data) / FIXEDPOINT_FACTOR; } +inline f32 readF32(const u8 *data) +{ + u32 u = readU32(data); + + switch (g_serialize_f32_type) { + case FLOATTYPE_SYSTEM: { + f32 f; + memcpy(&f, &u, 4); + return f; + } + case FLOATTYPE_SLOW: + return u32Tof32Slow(u); + case FLOATTYPE_UNKNOWN: // First initialization + g_serialize_f32_type = getFloatSerializationType(); + return readF32(data); + } + throw SerializationError("readF32: Unreachable code"); +} + inline video::SColor readARGB8(const u8 *data) { video::SColor p(readU32(data)); @@ -245,6 +267,15 @@ inline v3f readV3F1000(const u8 *data) return p; } +inline v3f readV3F32(const u8 *data) +{ + v3f p; + p.X = (float)readF32(&data[0]); + p.Y = (float)readF32(&data[4]); + p.Z = (float)readF32(&data[8]); + return p; +} + /////////////// write routines //////////////// inline void writeU8(u8 *data, u8 i) @@ -259,7 +290,7 @@ inline void writeS8(u8 *data, s8 i) inline void writeS16(u8 *data, s16 i) { - writeU16(data, (u16)i); + writeU16(data, (u16)i); } inline void writeS32(u8 *data, s32 i) @@ -278,6 +309,23 @@ inline void writeF1000(u8 *data, f32 i) writeS32(data, i * FIXEDPOINT_FACTOR); } +inline void writeF32(u8 *data, f32 i) +{ + switch (g_serialize_f32_type) { + case FLOATTYPE_SYSTEM: { + u32 u; + memcpy(&u, &i, 4); + return writeU32(data, u); + } + case FLOATTYPE_SLOW: + return writeU32(data, f32Tou32Slow(i)); + case FLOATTYPE_UNKNOWN: // First initialization + g_serialize_f32_type = getFloatSerializationType(); + return writeF32(data, i); + } + throw SerializationError("writeF32: Unreachable code"); +} + inline void writeARGB8(u8 *data, video::SColor p) { writeU32(data, p.color); @@ -322,6 +370,13 @@ inline void writeV3F1000(u8 *data, v3f p) writeF1000(&data[8], p.Z); } +inline void writeV3F32(u8 *data, v3f p) +{ + writeF32(&data[0], p.X); + writeF32(&data[4], p.Y); + writeF32(&data[8], p.Z); +} + //// //// Iostream wrapper for data read/write //// @@ -351,12 +406,14 @@ MAKE_STREAM_READ_FXN(s16, S16, 2); MAKE_STREAM_READ_FXN(s32, S32, 4); MAKE_STREAM_READ_FXN(s64, S64, 8); MAKE_STREAM_READ_FXN(f32, F1000, 4); +MAKE_STREAM_READ_FXN(f32, F32, 4); MAKE_STREAM_READ_FXN(v2s16, V2S16, 4); MAKE_STREAM_READ_FXN(v3s16, V3S16, 6); MAKE_STREAM_READ_FXN(v2s32, V2S32, 8); MAKE_STREAM_READ_FXN(v3s32, V3S32, 12); MAKE_STREAM_READ_FXN(v2f, V2F1000, 8); MAKE_STREAM_READ_FXN(v3f, V3F1000, 12); +MAKE_STREAM_READ_FXN(v3f, V3F32, 12); MAKE_STREAM_READ_FXN(video::SColor, ARGB8, 4); MAKE_STREAM_WRITE_FXN(u8, U8, 1); @@ -368,12 +425,14 @@ MAKE_STREAM_WRITE_FXN(s16, S16, 2); MAKE_STREAM_WRITE_FXN(s32, S32, 4); MAKE_STREAM_WRITE_FXN(s64, S64, 8); MAKE_STREAM_WRITE_FXN(f32, F1000, 4); +MAKE_STREAM_WRITE_FXN(f32, F32, 4); MAKE_STREAM_WRITE_FXN(v2s16, V2S16, 4); MAKE_STREAM_WRITE_FXN(v3s16, V3S16, 6); MAKE_STREAM_WRITE_FXN(v2s32, V2S32, 8); MAKE_STREAM_WRITE_FXN(v3s32, V3S32, 12); MAKE_STREAM_WRITE_FXN(v2f, V2F1000, 8); MAKE_STREAM_WRITE_FXN(v3f, V3F1000, 12); +MAKE_STREAM_WRITE_FXN(v3f, V3F32, 12); MAKE_STREAM_WRITE_FXN(video::SColor, ARGB8, 4); //// |