1 // quire_32_2.cpp: test suite runner for dot product and fused dot product functionality tests for fast specialized posit<32,2>
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 // Configure the posit template environment
13 // first: enable fast specialized posit<32,2>
14 //#define POSIT_FAST_SPECIALIZATION // turns on all fast specializations
15 #define POSIT_FAST_POSIT_32_2 1
16 // second: enable posit arithmetic exceptions
17 #define POSIT_THROW_ARITHMETIC_EXCEPTION 1
18 #include <universal/number/posit/posit.hpp>
19 #include <universal/verification/posit_test_suite.hpp>
20 #include <universal/verification/posit_test_randoms.hpp>
21
22 /// Standard posit with nbits = 32 have es = 2 exponent bits.
23
24 template<size_t nbits, size_t es>
Verify()25 int Verify() {
26 int nrOfFailedTests = 0;
27
28 return nrOfFailedTests;
29 }
30
main()31 int main()
32 try {
33 using namespace sw::universal;
34
35 constexpr size_t RND_TEST_CASES = 500000;
36
37 constexpr size_t nbits = 32;
38 constexpr size_t es = 2;
39
40 int nrOfFailedTestCases = 0;
41 bool bReportIndividualTestCases = false;
42 std::string tag = " quire<32,2>";
43
44 #if POSIT_FAST_POSIT_32_2
45 std::cout << "Fast specialization quire<32,2> configuration tests\n";
46 #else
47 std::cout << "Standard quire<32,2> configuration tests\n";
48 #endif
49
50 quire<nbits, es> q;
51 std::cout << dynamic_range<nbits,es>() << "\n\n";
52
53 // special cases
54 std::cout << "Special case tests\n";
55 std::string test = "Initialize to zero: ";
56 q = 0;
57 nrOfFailedTestCases += ReportCheck(tag, test, q.iszero());
58
59 // logic tests
60 // cout << "Logic operator tests " << endl;
61 // nrOfFailedTestCases += ReportTestResult( VerifyPositLogicEqual <nbits, es>(), tag, " == (native) ");
62
63 // conversion tests
64 std::cout << "Assignment/conversion tests\n";
65 posit<nbits, es> p(SpecificValue::minpos);
66 q = p;
67
68 // arithmetic tests
69 std::cout << "Arithmetic tests " << RND_TEST_CASES << " randoms each\n";
70 nrOfFailedTestCases += ReportTestResult(VerifyBinaryOperatorThroughRandoms<nbits, es>(bReportIndividualTestCases, OPCODE_ADD, RND_TEST_CASES), tag, "addition (native) ");
71 nrOfFailedTestCases += ReportTestResult(VerifyBinaryOperatorThroughRandoms<nbits, es>(bReportIndividualTestCases, OPCODE_MUL, RND_TEST_CASES), tag, "multiplication (native) ");
72
73 // elementary function tests
74 // cout << "Elementary function tests " << endl;
75
76 return (nrOfFailedTestCases > 0 ? EXIT_FAILURE : EXIT_SUCCESS);
77 }
78 catch (char const* msg) {
79 std::cerr << msg << std::endl;
80 return EXIT_FAILURE;
81 }
82 catch (const sw::universal::posit_arithmetic_exception& err) {
83 std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl;
84 return EXIT_FAILURE;
85 }
86 catch (const sw::universal::quire_exception& err) {
87 std::cerr << "Uncaught quire exception: " << err.what() << std::endl;
88 return EXIT_FAILURE;
89 }
90 catch (const sw::universal::posit_internal_exception& err) {
91 std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl;
92 return EXIT_FAILURE;
93 }
94 catch (const std::runtime_error& err) {
95 std::cerr << "Uncaught runtime exception: " << err.what() << std::endl;
96 return EXIT_FAILURE;
97 }
98 catch (...) {
99 std::cerr << "Caught unknown exception" << std::endl;
100 return EXIT_FAILURE;
101 }
102
103