1 // posit_2_0.cpp: test suite runner for specialized 2-bit posits based on look-up tables
2 //
3 // Copyright (C) 2017-2021 Stillwater Supercomputing, Inc.
4 //
5 // This file is part of the universal numbers project, which is released under an MIT Open Source license.
6 #if defined(_MSC_VER)
7 #pragma warning(disable : 5045) // Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified
8 #pragma warning(disable : 4514) // unreferenced inline function has been removed
9 #pragma warning(disable : 4820) // bytes padding added after data member
10 #pragma warning(disable : 4710) // function not inlined
11 #endif
12 // enable fast specialized posit<2,0>
13 //#define POSIT_FAST_SPECIALIZATION
14 #define POSIT_FAST_POSIT_2_0 1
15 // enable posit arithmetic exceptions
16 #define POSIT_THROW_ARITHMETIC_EXCEPTION 1
17 #include <universal/number/posit/posit.hpp>
18 #include <universal/verification/posit_test_suite.hpp>
19
20 /*
21 posit of size nbits = 2 without exponent bits, i.e. es = 0.
22 */
23
main()24 int main()
25 try {
26 using namespace sw::universal;
27
28 // no randoms, 2-bit posits can be done exhaustively
29
30 constexpr size_t nbits = 2;
31 constexpr size_t es = 0;
32
33 int nrOfFailedTestCases = 0;
34 bool bReportIndividualTestCases = true;
35 std::string tag = " posit<2,0>";
36
37 #if defined(POSIT_FAST_POSIT_2_0)
38 std::cout << "Fast specialization posit<2,0> configuration tests\n";;
39 #else
40 std::cout << "Reference posit<2,0> configuration tests\n";
41 #endif
42
43 posit<nbits,es> p;
44 std::cout << dynamic_range(p) << "\n\n";
45
46 // special cases
47 std::cout << "Special case tests\n";;
48 std::string test = "Initialize to zero: ";
49 p = 0;
50 nrOfFailedTestCases += ReportCheck(tag, test, p.iszero());
51 test = "Initialize to NAN";
52 p = NAN;
53 nrOfFailedTestCases += ReportCheck(tag, test, p.isnar());
54 test = "Initialize to INFINITY";
55 p = INFINITY;
56 nrOfFailedTestCases += ReportCheck(tag, test, p.isnar());
57 test = "sign is true";
58 p = -1.0f;
59 nrOfFailedTestCases += ReportCheck(tag, test, p.sign());
60 test = "is negative";
61 nrOfFailedTestCases += ReportCheck(tag, test, p.isneg());
62 test = "sign is false";
63 p = +1.0f;
64 nrOfFailedTestCases += ReportCheck(tag, test, !p.sign());
65 test = "is positive";
66 nrOfFailedTestCases += ReportCheck(tag, test, p.ispos());
67
68 // logic tests
69 std::cout << "Logic operator tests\n";
70 nrOfFailedTestCases += ReportTestResult(VerifyPositLogicEqual <nbits, es>(), tag, " == ");
71 nrOfFailedTestCases += ReportTestResult(VerifyPositLogicNotEqual <nbits, es>(), tag, " != ");
72 nrOfFailedTestCases += ReportTestResult(VerifyPositLogicLessThan <nbits, es>(), tag, " < ");
73 nrOfFailedTestCases += ReportTestResult(VerifyPositLogicLessOrEqualThan <nbits, es>(), tag, " <= ");
74 nrOfFailedTestCases += ReportTestResult(VerifyPositLogicGreaterThan <nbits, es>(), tag, " > ");
75 nrOfFailedTestCases += ReportTestResult(VerifyPositLogicGreaterOrEqualThan<nbits, es>(), tag, " >= ");
76
77 // conversion tests
78 std::cout << "Assignment/conversion tests\n";
79 nrOfFailedTestCases += ReportTestResult(VerifyIntegerConversion<nbits, es>(bReportIndividualTestCases), tag, "integer assign ");
80 nrOfFailedTestCases += ReportTestResult(VerifyConversion <nbits, es>(bReportIndividualTestCases), tag, "float assign ");
81
82 // arithmetic tests
83 std::cout << "Arithmetic tests\n";
84 nrOfFailedTestCases += ReportTestResult(VerifyAddition <nbits, es>(bReportIndividualTestCases), tag, "add ");
85 nrOfFailedTestCases += ReportTestResult(VerifySubtraction <nbits, es>(bReportIndividualTestCases), tag, "subtract ");
86 nrOfFailedTestCases += ReportTestResult(VerifyMultiplication <nbits, es>(bReportIndividualTestCases), tag, "multiply ");
87 nrOfFailedTestCases += ReportTestResult(VerifyDivision <nbits, es>(bReportIndividualTestCases), tag, "divide ");
88 nrOfFailedTestCases += ReportTestResult(VerifyNegation <nbits, es>(bReportIndividualTestCases), tag, "negate ");
89 nrOfFailedTestCases += ReportTestResult(VerifyReciprocation <nbits, es>(bReportIndividualTestCases), tag, "reciprocate ");
90
91 return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
92 }
93 catch (char const* msg) {
94 std::cerr << msg << std::endl;
95 return EXIT_FAILURE;
96 }
97 catch (const sw::universal::posit_arithmetic_exception& err) {
98 std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl;
99 return EXIT_FAILURE;
100 }
101 catch (const sw::universal::quire_exception& err) {
102 std::cerr << "Uncaught quire exception: " << err.what() << std::endl;
103 return EXIT_FAILURE;
104 }
105 catch (const sw::universal::posit_internal_exception& err) {
106 std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl;
107 return EXIT_FAILURE;
108 }
109 catch (const std::runtime_error& err) {
110 std::cerr << "Uncaught runtime exception: " << err.what() << std::endl;
111 return EXIT_FAILURE;
112 }
113 catch (...) {
114 std::cerr << "Caught unknown exception" << std::endl;
115 return EXIT_FAILURE;
116 }
117