From 79c9bbab80b2533da9905f76eb0ed91306d22b00 Mon Sep 17 00:00:00 2001 From: Federico Rossi Date: Thu, 10 Aug 2023 16:06:54 +0200 Subject: [PATCH] Fixed build bug when using explicit ahead template specification for posit for short/unsigned operators --- examples/99_debugger/99_debugger.cpp | 4 ++++ include/posit.h | 4 ++-- tests/test_init.cpp | 15 +++++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/examples/99_debugger/99_debugger.cpp b/examples/99_debugger/99_debugger.cpp index 0cce1ed..1f93f17 100644 --- a/examples/99_debugger/99_debugger.cpp +++ b/examples/99_debugger/99_debugger.cpp @@ -1,5 +1,9 @@ #include "posit.h" +namespace posit { +template class Posit; +} + float convert32(const float x) { posit::Posit y(x); const int32_t z = *(int32_t*)&y; diff --git a/include/posit.h b/include/posit.h index d5bf441..78bb811 100644 --- a/include/posit.h +++ b/include/posit.h @@ -576,12 +576,12 @@ namespace posit constexpr operator float() const { return to_backend(); } constexpr operator double() const { return to_backend(); } - constexpr operator short int() const { return to_backend();} + constexpr operator short int() const { return (int)to_backend();} constexpr operator int() const { return to_backend(); } constexpr operator long() const { return to_backend(); } constexpr operator long long int() const { return to_backend();} - constexpr operator short unsigned int() const { return to_backend();} + constexpr operator short unsigned int() const { return (unsigned int)to_backend();} constexpr operator unsigned int() const { return to_backend();} constexpr operator long unsigned int() const { return to_backend();} constexpr operator long long unsigned int() const { return to_backend();} diff --git a/tests/test_init.cpp b/tests/test_init.cpp index 94af172..000c88d 100644 --- a/tests/test_init.cpp +++ b/tests/test_init.cpp @@ -1,6 +1,7 @@ #define CATCH_CONFIG_MAIN #include #include +#include TEST_CASE("Initialize with deep initalization", "[short]") { using P = posit::Posit; @@ -20,3 +21,17 @@ TEST_CASE("Initialize with integer conversion", "[short]") { REQUIRE(a.v == (1<<14)); } + +TEST_CASE("Initialize with big integers","[short]") { + uint64_t bigNum = uint64_t(INT32_MAX)*2; + posit::Posit x(bigNum); + REQUIRE(x.v == 2143289344); + + uint32_t bigNum32 = uint32_t(INT16_MAX)*2; + posit::Posit y(bigNum),z(bigNum32); + REQUIRE(y.v == 32704); + REQUIRE(z.v == 31744); + + posit::Posit w(bigNum); + REQUIRE(w.v == 127); +}