-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ab8d914
commit 30ccb75
Showing
5 changed files
with
117 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,34 @@ | ||
// | ||
// Created by Federico Rossi on 29/07/23. | ||
// | ||
|
||
#include "posit.h" | ||
#include <cstdint> | ||
|
||
float convert32(const float x) { | ||
posit::Posit<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan> y(x); | ||
const uint32_t z = *(uint32_t*)&y; | ||
std::cout << z << std::endl; | ||
return *(posit::Posit<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan>*)&z; | ||
} | ||
|
||
float convert16(const float x) { | ||
posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithNan> y(x); | ||
const int16_t z = *(int16_t*)&y; | ||
std::cout << z << std::endl; | ||
return *(posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithNan>*)&z; | ||
} | ||
|
||
typedef float (*F)(const float); | ||
|
||
template <float (*F)(float)> | ||
void calc(float x, int bits) { | ||
std::cout << "input = " << x << ", converted back and forth with " << bits | ||
<< " bits = " << F(x) | ||
<< ", ratio = " << std::abs(F(x)/double(x)) << std::endl; | ||
} | ||
|
||
int main() { | ||
uint64_t bigNum = uint64_t(INT32_MAX)*2; | ||
std::cout << bigNum << " " << INT32_MAX << std::endl; | ||
posit::Posit<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan> x(bigNum); | ||
std::cout << (uint64_t)x << std::endl; | ||
return 0; | ||
std::cout << "Using cppposit library" << std::endl << std::endl; | ||
|
||
calc<&convert32>(1.75911e-33, 32); | ||
calc<&convert32>(-1.15356e-33f, 32); | ||
calc<&convert16>(6.40675e-27, 16); | ||
calc<&convert16>(-8.76183e-37, 16); | ||
calc<&convert16>(1.4013e-45, 16); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
include(FetchContent) | ||
# declare Catch2 | ||
FetchContent_Declare(Catch2 # name of the content | ||
GIT_REPOSITORY https://github.com/catchorg/Catch2.git # the repository | ||
GIT_TAG v2.13.7 # the tag | ||
) | ||
|
||
# make available | ||
FetchContent_MakeAvailable(Catch2) | ||
|
||
|
||
add_executable(test_init test_init.cpp) | ||
target_link_libraries(test_init PRIVATE Catch2::Catch2) | ||
|
||
add_executable(test_subnormals test_subnormals.cpp) | ||
target_link_libraries(test_subnormals PRIVATE Catch2::Catch2) | ||
|
||
|
||
add_test( | ||
NAME test_init | ||
COMMAND test_init --success | ||
) | ||
|
||
add_test( | ||
NAME test_subnormals | ||
COMMAND test_subnormals --success | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#define CATCH_CONFIG_MAIN | ||
#include <catch2/catch.hpp> | ||
#include <posit.h> | ||
|
||
TEST_CASE("Initialize with deep initalization", "[short]") { | ||
using P = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithInfs>; | ||
P a = P::from_sraw(1 << 14); | ||
REQUIRE(a.v == (1<<14)); | ||
} | ||
|
||
TEST_CASE("Initialize with float conversion", "[short]") { | ||
using P = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithInfs>; | ||
P a(1.0f); | ||
REQUIRE(a.v == (1<<14)); | ||
} | ||
|
||
TEST_CASE("Initialize with integer conversion", "[short]") { | ||
using P = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithInfs>; | ||
P a(1); | ||
REQUIRE(a.v == (1<<14)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#define CATCH_CONFIG_MAIN | ||
#include <catch2/catch.hpp> | ||
#include <posit.h> | ||
|
||
using P = posit::Posit<int32_t, 32, 2, uint32_t, posit::PositSpec::WithNan>; | ||
using P16 = posit::Posit<int16_t, 16, 2, uint32_t, posit::PositSpec::WithNan>; | ||
|
||
double convert32(const double x) { | ||
P y(x); | ||
const uint32_t z = *(uint32_t*)&y; | ||
return *(P*)&z; | ||
} | ||
|
||
float convert16(const float x) { | ||
P16 y(x); | ||
const uint16_t z = *(uint16_t*)&y; | ||
return *(P16*)&z; | ||
} | ||
|
||
|
||
TEST_CASE("Test 32,2 minposit", "[short]") { | ||
float a = convert32(1.75911e-33f); | ||
float b = convert32(-1.15356e-33f); | ||
REQUIRE(P(a).v == 7u); | ||
REQUIRE(P(b).v == 4294967290u); | ||
} | ||
|
||
TEST_CASE("Test 16,2 minposit", "[short]") { | ||
float a = convert32(6.40675e-27f); | ||
float b = convert32(-8.76183e-37f); | ||
REQUIRE(P16(a).v == 1u); | ||
REQUIRE(P16(b).v == -1u); | ||
} |