1 // gismo_test.cpp example testing cricial features for G+Smo integration
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 #include "common.hpp"
7 // enable posit arithmetic exceptions
8 #define POSIT_THROW_ARITHMETIC_EXCEPTION 1
9 // enable special posit format emission
10 #define POSIT_ROUNDING_ERROR_FREE_IO_FORMAT 1
11 #include <universal/number/posit/posit.hpp>
12 
main(int argc,char ** argv)13 int main(int argc, char** argv)
14 try {
15     using namespace sw::universal;
16 
17 	// Test reading posit from std::istringstream
18     posit<32,2> p;
19 
20     std::string str = "3.1415926535897932384626433832795028841971693993751058209749445923078164062";
21     std::istringstream lnstream;
22     lnstream.unsetf(std::ios_base::skipws);
23     lnstream.clear();
24     lnstream.str(str);
25     lnstream >> std::ws >> p;
26 	std::cout << "IEEE float/double format, parsed into a posit<32,2>: " << p << '\n';
27 
28 	lnstream.clear();
29 	str = "32.2x40000000p";
30 	lnstream.str(str);
31 	lnstream >> std::ws >> p;
32 	std::cout << "posit format: " << std::setw(25) << str << "- parsed into a posit<32,2>: " << p << '\n';
33 
34 	lnstream.clear();
35 	str = "32.2x80000000p";
36 	lnstream.str(str);
37 	lnstream >> std::ws >> p;
38 	std::cout << "posit format: " << std::setw(25) << str << "- parsed into a posit<32,2>: " << p << '\n';
39 
40 	lnstream.clear();
41 	str = "64.3x8000000000000000p";
42 	lnstream.str(str);
43 	lnstream >> std::ws >> p;  // testing that we are NOT truncating the most significant bits
44 	std::cout << "posit format: " << std::setw(25) << str << "- parsed into a posit<32,2>: " << p << " <---- should have the most significant 32bits of the 64.3 posit\n";
45 	std::cout << "pretty posit: " << pretty_print(p) << '\n';
46 
47 	std::cout << "Bitblock patterns\n";
48 	bitblock<1> one; one.set(0, true); str = to_hex(one); std::cout << "one  : \"" << one << "\"    value : " << str << '\n';
49 	bitblock<2> two; two.set(1, true); str = to_hex(two); std::cout << "two  : \"" << two << "\"   value : " << str << '\n';
50 	bitblock<3> three; three.set(2, true); str = to_hex(three); std::cout << "three: \"" << three << "\"  value : " << str << '\n';
51 	bitblock<4> four; four.set(3, true); str = to_hex(four); std::cout << "four : \"" << four << "\" value : " << str << '\n';
52 
53 	p.setzero();
54 	std::cout << "posit value     0: " << p << '\n';
55 	p.setnar();
56 	std::cout << "posit value   NaR: " << p << '\n';
57 
58     return EXIT_SUCCESS;
59 }
60 catch (char const* msg) {
61     std::cerr << "Caught exception: " << msg << std::endl;
62     return EXIT_FAILURE;
63 }
64 catch (const sw::universal::posit_arithmetic_exception& err) {
65 	std::cerr << "Uncaught posit arithmetic exception: " << err.what() << std::endl;
66 	return EXIT_FAILURE;
67 }
68 catch (const sw::universal::quire_exception& err) {
69 	std::cerr << "Uncaught quire exception: " << err.what() << std::endl;
70 	return EXIT_FAILURE;
71 }
72 catch (const sw::universal::posit_internal_exception& err) {
73 	std::cerr << "Uncaught posit internal exception: " << err.what() << std::endl;
74 	return EXIT_FAILURE;
75 }
76 catch (const std::runtime_error& err) {
77 	std::cerr << "Uncaught runtime exception: " << err.what() << std::endl;
78 	return EXIT_FAILURE;
79 }
80 catch (...) {
81     std::cerr << "Caught unknown exception" << std::endl;
82     return EXIT_FAILURE;
83 }
84