Add unaligned floating point types and supporting infrastructure.

This commit is contained in:
Beman
2013-05-26 08:25:10 -04:00
parent a7ba65f830
commit dff7c1254e
9 changed files with 270 additions and 18 deletions
+80
View File
@@ -16,6 +16,8 @@
//----------------------------------------------------------------------------//
#define _SCL_SECURE_NO_WARNINGS
#include <boost/endian/detail/disable_warnings.hpp>
#include <boost/endian/types.hpp>
@@ -234,6 +236,17 @@ namespace
VERIFY(little_float32.data() == reinterpret_cast<const char *>(&little_float32));
VERIFY(little_float64.data() == reinterpret_cast<const char *>(&little_float64));
big_float32un_t big_float32un;
big_float64un_t big_float64un;
little_float32un_t little_float32un;
little_float64un_t little_float64un;
VERIFY(big_float32un.data() == reinterpret_cast<const char *>(&big_float32un));
VERIFY(big_float64un.data() == reinterpret_cast<const char *>(&big_float64un));
VERIFY(little_float32un.data() == reinterpret_cast<const char *>(&little_float32un));
VERIFY(little_float64un.data() == reinterpret_cast<const char *>(&little_float64un));
VERIFY(big_8.data() == reinterpret_cast<const char *>(&big_8));
VERIFY(big_16.data() == reinterpret_cast<const char *>(&big_16));
VERIFY(big_24.data() == reinterpret_cast<const char *>(&big_24));
@@ -318,6 +331,11 @@ namespace
VERIFY_SIZE(sizeof( little_float32_t ), 4 );
VERIFY_SIZE(sizeof( little_float64_t ), 8 );
VERIFY_SIZE(sizeof( big_float32un_t ), 4 );
VERIFY_SIZE(sizeof( big_float64un_t ), 8 );
VERIFY_SIZE(sizeof( little_float32un_t ), 4 );
VERIFY_SIZE(sizeof( little_float64un_t ), 8 );
VERIFY_SIZE( sizeof( big_8_t ), 1 );
VERIFY_SIZE( sizeof( big_16_t ), 2 );
VERIFY_SIZE( sizeof( big_24_t ), 3 );
@@ -486,6 +504,18 @@ namespace
native_u64_t v31;
};
struct big_float_struct
{
int16_t v0;
big_float32_t v1;
};
struct big_unaligned_float_struct
{
int16_t v0;
big_float32un_t v1;
};
// aligned test cases
struct big_aligned_struct
@@ -514,6 +544,8 @@ namespace
VERIFY_SIZE( sizeof(native_u_struct), 39 );
VERIFY_SIZE( sizeof(big_aligned_struct), 24 );
VERIFY_SIZE( sizeof(little_aligned_struct), 24 );
VERIFY_SIZE( sizeof(big_float_struct), 8 );
VERIFY_SIZE( sizeof(big_unaligned_float_struct), 6 );
if ( saved_err_count == err_count )
{
@@ -526,6 +558,7 @@ namespace
void check_representation_and_range_and_ops()
{
// aligned floating point types
float big_float32_expected = std::numeric_limits<float>::max();
boost::endian::big_endian(big_float32_expected);
big_float32_t big_float32(std::numeric_limits<float>::max());
@@ -560,6 +593,52 @@ namespace
VERIFY_VALUE_AND_OPS( little_float64_t, double, std::numeric_limits<double>::max() );
VERIFY_VALUE_AND_OPS( little_float64_t, double, std::numeric_limits<double>::min() );
// unaligned floating point types
float big_float32un_expected = std::numeric_limits<float>::max();
boost::endian::big_endian(big_float32un_expected);
big_float32un_t big_float32un(std::numeric_limits<float>::max());
VERIFY(std::memcmp(big_float32un.data(),
reinterpret_cast<const char*>(&big_float32un_expected), sizeof(float)) == 0);
float little_float32un_expected = std::numeric_limits<float>::max();
boost::endian::little_endian(little_float32un_expected);
little_float32un_t little_float32un(std::numeric_limits<float>::max());
VERIFY(std::memcmp(little_float32un.data(),
reinterpret_cast<const char*>(&little_float32un_expected), sizeof(float)) == 0);
double big_float64un_expected = std::numeric_limits<double>::max();
boost::endian::big_endian(big_float64un_expected);
big_float64un_t big_float64un(std::numeric_limits<double>::max());
VERIFY(std::memcmp(big_float64un.data(),
reinterpret_cast<const char*>(&big_float64un_expected), sizeof(double)) == 0);
double little_float64un_expected = std::numeric_limits<double>::max();
boost::endian::little_endian(little_float64un_expected);
little_float64un_t little_float64un(std::numeric_limits<double>::max());
VERIFY(std::memcmp(little_float64un.data(),
reinterpret_cast<const char*>(&little_float64un_expected), sizeof(double)) == 0);
VERIFY_VALUE_AND_OPS( big_float32un_t, float, std::numeric_limits<float>::max() );
VERIFY_VALUE_AND_OPS( big_float32un_t, float, std::numeric_limits<float>::min() );
VERIFY_VALUE_AND_OPS( big_float64un_t, double, std::numeric_limits<double>::max() );
VERIFY_VALUE_AND_OPS( big_float64un_t, double, std::numeric_limits<double>::min() );
VERIFY_VALUE_AND_OPS( little_float32un_t, float, std::numeric_limits<float>::max() );
VERIFY_VALUE_AND_OPS( little_float32un_t, float, std::numeric_limits<float>::min() );
VERIFY_VALUE_AND_OPS( little_float64un_t, double, std::numeric_limits<double>::max() );
VERIFY_VALUE_AND_OPS( little_float64un_t, double, std::numeric_limits<double>::min() );
float a = 1.0F;
big_float32_t b(1.0F);
big_float32un_t c(1.0F);
little_float32_t d(1.0F);
little_float32un_t e(1.0F);
VERIFY(a==b);
VERIFY(a==c);
VERIFY(a==d);
VERIFY(a==e);
// unaligned integer types
VERIFY_BIG_REPRESENTATION( big_8_t );
VERIFY_VALUE_AND_OPS( big_8_t, int_least8_t, 0x7f );
VERIFY_VALUE_AND_OPS( big_8_t, int_least8_t, -0x80 );
@@ -728,6 +807,7 @@ namespace
VERIFY_NATIVE_REPRESENTATION( native_u64_t );
VERIFY_VALUE_AND_OPS( native_u64_t, uint_least64_t, 0xffffffffffffffffULL );
// aligned integer types
VERIFY_BIG_REPRESENTATION( big_int16_t );
VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, 0x7fff );
VERIFY_VALUE_AND_OPS( big_int16_t, int_least16_t, -0x8000 );