Honeycomb  0.1
Component-Model Framework
Numeral.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
4 #include "Honey/Misc/Debug.h"
5 
6 namespace honey
7 {
9 namespace numeral_priv { template<class T> struct Info; }
12 class Float_;
13 class Double_;
14 class Quad_;
15 
17 template<class T> class Numeral;
18 
20 template<class T>
22 {
23  typedef numeral_priv::Info<T> Info;
24  Info _info;
25 
26 public:
28  typedef typename Info::Int Int;
30  typedef typename Info::Real Real;
32  typedef typename Info::Real_ Real_;
33 
35  static const int sizeBits = sizeof(T)*8;
36 
38  constexpr T min() const { return _info.min; }
40  constexpr T max() const { return _info.max; }
41 };
42 
44 template<class T>
46 {
47  typedef numeral_priv::Info<T> Info;
48  Info _info;
49 
50 public:
52  typedef typename Info::Int Int;
54  typedef typename Info::Real Real;
56  typedef typename Info::Real_ Real_;
57 
59  static const int sizeBits = sizeof(T)*8;
60 
62  constexpr T min() const { return _info.min(); }
64  constexpr T max() const { return _info.max(); }
66  constexpr T smallest() const { return _info.smallest(); }
68  constexpr T epsilon() const { return _info.epsilon(); }
70  constexpr T inf() const { return _info.inf(); }
72  constexpr T nan() const { return _info.nan(); }
73 };
74 
75 
77 template<class T>
78 constexpr Numeral<T> numeral() { return Numeral<T>(); }
80 template<class T>
81 constexpr Numeral<T> numeral(const T&) { return numeral<T>(); }
82 
86 
88 template<class To, class From, typename std::enable_if<std::is_signed<From>::value && std::is_unsigned<To>::value && sizeof(To) >= sizeof(From), int>::type=0>
89 To numeric_cast(From s) { assert(s >= 0); return static_cast<To>(s); }
91 template<class To, class From, typename std::enable_if<std::is_unsigned<From>::value && std::is_signed<To>::value && sizeof(To) >= sizeof(From), int>::type=0>
92 To numeric_cast(From u) { assert(static_cast<To>(u) >= 0); return static_cast<To>(u); }
94 template<class To, class From, typename std::enable_if<std::is_signed<From>::value == std::is_signed<To>::value && sizeof(To) >= sizeof(From), int>::type=0>
95 To numeric_cast(From i) { return static_cast<To>(i); }
97 template<class To, class From, typename std::enable_if<std::is_signed<From>::value && sizeof(To) < sizeof(From), int>::type=0>
98 To numeric_cast(From s) { assert(s >= static_cast<From>(numeral<To>().min()) && s <= static_cast<From>(numeral<To>().max())); return static_cast<To>(s); }
100 template<class To, class From, typename std::enable_if<std::is_unsigned<From>::value && sizeof(To) < sizeof(From), int>::type=0>
101 To numeric_cast(From u) { assert(u <= static_cast<From>(numeral<To>().max())); return static_cast<To>(u); }
103 
105 namespace numeral_priv
106 {
107  template<> struct Info<int8>
108  {
109  typedef int8 Int;
110  typedef float Real;
111  typedef Float_ Real_;
112  static const int8 min = -0x7F - 1;
113  static const int8 max = 0x7F;
114  };
115 
116  template<> struct Info<uint8>
117  {
118  typedef uint8 Int;
119  typedef float Real;
120  typedef Float_ Real_;
121  static const uint8 min = 0;
122  static const uint8 max = 0xFFU;
123  };
124 
125  template<> struct Info<int16>
126  {
127  typedef int16 Int;
128  typedef float Real;
129  typedef Float_ Real_;
130  static const int16 min = -0x7FFF - 1;
131  static const int16 max = 0x7FFF;
132  };
133 
134  template<> struct Info<uint16>
135  {
136  typedef uint16 Int;
137  typedef float Real;
138  typedef Float_ Real_;
139  static const uint16 min = 0;
140  static const uint16 max = 0xFFFFU;
141  };
142 
143  template<> struct Info<int32>
144  {
145  typedef int32 Int;
146  typedef float Real;
147  typedef Float_ Real_;
148  static const int32 min = -0x7FFFFFFF - 1;
149  static const int32 max = 0x7FFFFFFF;
150  };
151 
152  template<> struct Info<uint32>
153  {
154  typedef uint32 Int;
155  typedef float Real;
156  typedef Float_ Real_;
157  static const uint32 min = 0;
158  static const uint32 max = 0xFFFFFFFFU;
159  };
160 
161  template<> struct Info<int64>
162  {
163  typedef int64 Int;
164  typedef double Real;
165  typedef Double_ Real_;
166  static const int64 min = -0x7FFFFFFFFFFFFFFFL - 1;
167  static const int64 max = 0x7FFFFFFFFFFFFFFFL;
168  };
169 
170  template<> struct Info<uint64>
171  {
172  typedef uint64 Int;
173  typedef double Real;
174  typedef Double_ Real_;
175  static const uint64 min = 0;
176  static const uint64 max = 0xFFFFFFFFFFFFFFFFUL;
177  };
178 
179  template<> struct Info<float>
180  {
181  typedef int32 Int;
182  typedef float Real;
183  typedef Float_ Real_;
184 
185  constexpr float min() const { return -3.402823466e+38f; }
186  constexpr float max() const { return 3.402823466e+38f; }
187  constexpr float smallest() const { return 1.175494351e-38f; }
188  constexpr float epsilon() const { return 1.192092896e-07f; }
189  constexpr float one() const { return 1.f; }
190  constexpr float inf() const { return 1.f / (1.f - one()); }
191  constexpr float nan() const { return 0.f / (1.f - one()); }
192  };
193 
194  template<> struct Info<double>
195  {
196  typedef int64 Int;
197  typedef double Real;
198  typedef Double_ Real_;
199 
200  constexpr double min() const { return -1.7976931348623158e+308; }
201  constexpr double max() const { return 1.7976931348623158e+308; }
202  constexpr double smallest() const { return 2.2250738585072014e-308; }
203  constexpr double epsilon() const { return 2.2204460492503131e-016; }
204  constexpr double one() const { return 1.0; }
205  constexpr double inf() const { return 1.0 / (1.0 - one()); }
206  constexpr double nan() const { return 0.0 / (1.0 - one()); }
207  };
208 }
209 
210 template<> class Numeral<int8> : public NumeralInt<int8> {};
211 template<> class Numeral<uint8> : public NumeralInt<uint8> {};
212 template<> class Numeral<int16> : public NumeralInt<int16> {};
213 template<> class Numeral<uint16> : public NumeralInt<uint16> {};
214 template<> class Numeral<int32> : public NumeralInt<int32> {};
215 template<> class Numeral<uint32> : public NumeralInt<uint32> {};
216 template<> class Numeral<int64> : public NumeralInt<int64> {};
217 template<> class Numeral<uint64> : public NumeralInt<uint64> {};
218 template<> class Numeral<float> : public NumeralFloat<float> {};
219 template<> class Numeral<double> : public NumeralFloat<double> {};
222 }
223 
224 #include "Honey/Math/platform/Numeral.h"
225 
constexpr T smallest() const
Smallest representable value (close to zero)
Definition: Numeral.h:66
constexpr T min() const
Minimum possible value for type.
Definition: Numeral.h:62
unsigned int uint32
Definition: Core.h:16
static const int sizeBits
Size of type in bits.
Definition: Numeral.h:59
Info::Real Real
Real representation of type.
Definition: Numeral.h:54
Numeric type info for integer types.
Definition: Numeral.h:21
constexpr T epsilon() const
Smallest value such that 1.0 + epsilon != 1.0.
Definition: Numeral.h:68
constexpr T max() const
Maximum possible value for type.
Definition: Numeral.h:40
Info::Int Int
Integer representation of type.
Definition: Numeral.h:28
char int8
Definition: Core.h:11
unsigned long long uint64
Definition: Core.h:22
constexpr T min() const
Minimum possible value for type (negative for signed types)
Definition: Numeral.h:38
constexpr T max() const
Maximum possible value for type.
Definition: Numeral.h:64
unsigned char uint8
Definition: Core.h:12
int int32
Definition: Core.h:15
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
Info::Real_ Real_
Real operations and constants class.
Definition: Numeral.h:32
constexpr Numeral< T > numeral()
Get numeric type info safely from a static context.
Definition: Numeral.h:78
unsigned short uint16
Definition: Core.h:14
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
static const int sizeBits
Size of type in bits.
Definition: Numeral.h:35
Numeral< Real >::Real_ Real_
Operations and constants for Real type. See Float_, Double_.
Definition: Real.h:25
constexpr T inf() const
Infinity. ie. 1.0 / 0.0.
Definition: Numeral.h:70
long long int64
Definition: Core.h:21
Numeric type info for floating point types.
Definition: Numeral.h:45
Info::Real_ Real_
Real operations and constants class.
Definition: Numeral.h:56
Info::Real Real
Real representation of type.
Definition: Numeral.h:30
constexpr T nan() const
Not a number. ie. 0.0 / 0.0, sqrt(-1)
Definition: Numeral.h:72
short int16
Definition: Core.h:13
Global Honeycomb namespace.
Info::Int Int
Integer representation of type.
Definition: Numeral.h:52