1 #pragma once
2 // exceptions.hpp: exceptions for problems in bitblock calculations
3 //
4 // Copyright (C) 2017-2021 Stillwater Supercomputing, Inc.
5 //
6 // This file is part of the universal numbers project, which is released under an MIT Open Source license.
7 
8 #include <stdexcept>
9 #include <string>
10 
11 // this is a supporting class, and its exception behavior needs to be configure by the calling environment.
12 // this means that the value of BITBLOCK_THROW_ARITHMETIC_EXCEPTION must be set externally
13 // #define BITBLOCK_THROW_ARITHMETIC_EXCEPTION exception-config-of-calling-class
14 // TODO: do we want to support the use-case where two seperate classes that use bitblock<>
15 // but desire different exception behavior?
16 
17 namespace sw::universal {
18 
19 ///////////////////////////////////////////////////////////////////////////////////////////////////
20 /// BITBLOCK ARITHMETIC EXCEPTIONS
21 
22 // base class for bitblock arithmetic exceptions
23 struct bitblock_arithmetic_exception
24 	: public std::runtime_error
25 {
bitblock_arithmetic_exceptionsw::universal::bitblock_arithmetic_exception26 	bitblock_arithmetic_exception(const std::string& error) : std::runtime_error(std::string("bitblock arithmetic exception: ") + error) {};
27 };
28 
29 //////////////////////////////////////////////////////////////////////////////////////////////////
30 /// specialized exceptions to aid application level exception handling
31 
32 // is thrown when denominator is 0 in a division operator
33 struct bitblock_divide_by_zero
34 	: public bitblock_arithmetic_exception
35 {
bitblock_divide_by_zerosw::universal::bitblock_divide_by_zero36 	bitblock_divide_by_zero(const std::string& error = "bitblock divide by zero") : bitblock_arithmetic_exception(error) {}
37 };
38 
39 ///////////////////////////////////////////////////////////////////////////////////////////////////
40 /// BITBLOCK INTERNAL EXCEPTIONS
41 
42 // base class for bitblock internal exceptions
43 struct bitblock_internal_exception
44 	: public std::runtime_error
45 {
bitblock_internal_exceptionsw::universal::bitblock_internal_exception46 	bitblock_internal_exception(const std::string& error) : std::runtime_error(std::string("bitblock internal exception: ") + error) {};
47 };
48 
49 struct iteration_bound_too_large
50 	: bitblock_internal_exception
51 {
iteration_bound_too_largesw::universal::iteration_bound_too_large52 	iteration_bound_too_large(const std::string& error = "iteration bound is too large") : bitblock_internal_exception(error) {}
53 };
54 
55 struct round_off_all
56 	: bitblock_internal_exception
57 {
round_off_allsw::universal::round_off_all58 	round_off_all(const std::string& error = "cannot round off all bits") : bitblock_internal_exception(error) {}
59 };
60 
61 struct cut_off_leading_bit
62 	: bitblock_internal_exception
63 {
cut_off_leading_bitsw::universal::cut_off_leading_bit64 	cut_off_leading_bit(const std::string& error = "leading significant bit is cut off") : bitblock_internal_exception(error) {}
65 };
66 
67 ///////////////////////////////////////////////////////////////////////////////////////////////////
68 
69 } // namespace sw::universal
70