mirror of
https://github.com/bkaradzic/bx.git
synced 2026-02-18 04:53:06 +01:00
Fixed issue when writing float in opposite CPU endianess.
This commit is contained in:
@@ -46,7 +46,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template <typename Ty>
|
||||
inline Ty toLittleEndian(const Ty _in)
|
||||
inline Ty toLittleEndian(Ty _in)
|
||||
{
|
||||
#if BX_CPU_ENDIAN_BIG
|
||||
return endianSwap(_in);
|
||||
@@ -56,7 +56,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template <typename Ty>
|
||||
inline Ty toBigEndian(const Ty _in)
|
||||
inline Ty toBigEndian(Ty _in)
|
||||
{
|
||||
#if BX_CPU_ENDIAN_LITTLE
|
||||
return endianSwap(_in);
|
||||
@@ -66,7 +66,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template <typename Ty>
|
||||
inline Ty toHostEndian(const Ty _in, bool _fromLittleEndian)
|
||||
inline Ty toHostEndian(Ty _in, bool _fromLittleEndian)
|
||||
{
|
||||
#if BX_CPU_ENDIAN_LITTLE
|
||||
return _fromLittleEndian ? _in : endianSwap(_in);
|
||||
|
||||
@@ -273,7 +273,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
int32_t read(ReaderI* _reader, Ty& _value, Error* _err)
|
||||
inline int32_t read(ReaderI* _reader, Ty& _value, Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
BX_STATIC_ASSERT(isTriviallyCopyable<Ty>() );
|
||||
@@ -281,7 +281,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
int32_t readHE(ReaderI* _reader, Ty& _value, bool _fromLittleEndian, Error* _err)
|
||||
inline int32_t readHE(ReaderI* _reader, Ty& _value, bool _fromLittleEndian, Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
BX_STATIC_ASSERT(isTriviallyCopyable<Ty>() );
|
||||
@@ -329,7 +329,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
int32_t write(WriterI* _writer, const Ty& _value, Error* _err)
|
||||
inline int32_t write(WriterI* _writer, const Ty& _value, Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
BX_STATIC_ASSERT(isTriviallyCopyable<Ty>() );
|
||||
@@ -337,7 +337,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
int32_t writeLE(WriterI* _writer, const Ty& _value, Error* _err)
|
||||
inline int32_t writeLE(WriterI* _writer, const Ty& _value, Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
BX_STATIC_ASSERT(isTriviallyCopyable<Ty>() );
|
||||
@@ -346,8 +346,14 @@ namespace bx
|
||||
return result;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int32_t writeLE(WriterI* _writer, const float& _value, Error* _err)
|
||||
{
|
||||
return writeLE(_writer, floatToBits(_value), _err);
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
int32_t writeBE(WriterI* _writer, const Ty& _value, Error* _err)
|
||||
inline int32_t writeBE(WriterI* _writer, const Ty& _value, Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
BX_STATIC_ASSERT(isTriviallyCopyable<Ty>() );
|
||||
@@ -356,6 +362,12 @@ namespace bx
|
||||
return result;
|
||||
}
|
||||
|
||||
template<>
|
||||
inline int32_t writeBE(WriterI* _writer, const float& _value, Error* _err)
|
||||
{
|
||||
return writeBE(_writer, floatToBits(_value), _err);
|
||||
}
|
||||
|
||||
inline int64_t skip(SeekerI* _seeker, int64_t _offset)
|
||||
{
|
||||
return _seeker->seek(_offset, Whence::Current);
|
||||
@@ -392,7 +404,7 @@ namespace bx
|
||||
}
|
||||
|
||||
template<typename Ty>
|
||||
int32_t peek(ReaderSeekerI* _reader, Ty& _value, Error* _err)
|
||||
inline int32_t peek(ReaderSeekerI* _reader, Ty& _value, Error* _err)
|
||||
{
|
||||
BX_ERROR_SCOPE(_err);
|
||||
BX_STATIC_ASSERT(isTriviallyCopyable<Ty>() );
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "endian.h"
|
||||
#include "error.h"
|
||||
#include "filepath.h"
|
||||
#include "math.h"
|
||||
#include "string.h"
|
||||
#include "uint32_t.h"
|
||||
|
||||
|
||||
31
tests/readerwriter_test.cpp
Normal file
31
tests/readerwriter_test.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Copyright 2010-2019 Branimir Karadzic. All rights reserved.
|
||||
* License: https://github.com/bkaradzic/bx#license-bsd-2-clause
|
||||
*/
|
||||
|
||||
#include "test.h"
|
||||
#include <bx/readerwriter.h>
|
||||
|
||||
TEST_CASE("writeLE", "")
|
||||
{
|
||||
bx::SizerWriter writer;
|
||||
|
||||
bx::Error err;
|
||||
|
||||
int32_t total = bx::writeLE(&writer, 1.0f, &err);
|
||||
|
||||
REQUIRE(err.isOk() );
|
||||
REQUIRE(total == 4);
|
||||
}
|
||||
|
||||
TEST_CASE("writeBE", "")
|
||||
{
|
||||
bx::SizerWriter writer;
|
||||
|
||||
bx::Error err;
|
||||
|
||||
int32_t total = bx::writeBE(&writer, 1.0f, &err);
|
||||
|
||||
REQUIRE(err.isOk() );
|
||||
REQUIRE(total == 4);
|
||||
}
|
||||
Reference in New Issue
Block a user