-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbcdh.h
200 lines (177 loc) · 5.72 KB
/
bcdh.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
*
* Copyright (c) 2010-2015 Voidware Ltd. All Rights Reserved.
*
* This file contains Original Code and/or Modifications of Original Code as
* defined in and that are subject to the Voidware Public Source Licence version
* 1.0 (the 'Licence'). You may not use this file except in compliance with the
* Licence or with expressly written permission from Voidware. Please obtain a
* copy of the Licence at http://www.voidware.com/legal/vpsl1.txt and read it
* before using this file.
*
* The Original Code and all software distributed under the Licence are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
* OR IMPLIED, AND VOIDWARE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING
* WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Please see the Licence for the specific language governing rights and
* limitations under the Licence.
*
*/
#ifndef __bcdh_h__
#define __bcdh_h__
#include "bcdfloath.h"
#include "bcd.h"
struct BCDh
{
// Constructors
BCDh() {}
BCDh(int4 v) : _v(v) {}
BCDh(uint4 v) : _v(v) {}
BCDh(const BCDFloatDataH& bf) : _v(bf) {}
BCDh(const BCDFloatData& bf) : _v(bf) {}
BCDh(const BCD& b) : _v(b._v) {}
int exponent() const { return _v.exp(); }
void setExponent(int v) { _v.exp(v); }
int digit(int n) const { return _v._d[n]; }
void digit(int n, int v) { _v._d[n] = v; }
BCD asBCD() const
{
BCD v;
_v.asBCD(&v._v);
return v;
}
// Arithmetic
friend BCDh operator+(const BCDh& a, const BCDh& b)
{
BCDh c;
BCDFloatH::add(&a._v, &b._v, &c._v);
return c;
}
friend BCDh operator-(const BCDh& a, const BCDh& b)
{
BCDh c;
BCDFloatH::sub(&a._v, &b._v, &c._v);
return c;
}
friend BCDh operator*(const BCDh& a, const BCDh& b)
{
BCDh c;
BCDFloatH::mul(&a._v, &b._v, &c._v);
return c;
}
friend BCDh operator/(const BCDh& a, const BCDh& b)
{
BCDh c;
BCDFloatH::div(&a._v, &b._v, &c._v);
return c;
}
void operator+=(const BCDh& b)
{
BCDh c;
BCDFloatH::add(&_v, &b._v, &c._v);
_v = c._v;
}
void operator-=(const BCDh& b)
{
BCDh c;
BCDFloatH::sub(&_v, &b._v, &c._v);
_v = c._v;
}
void operator*=(const BCDh& b)
{
BCDh c;
BCDFloatH::mul(&_v, &b._v, &c._v);
_v = c._v;
}
void operator/=(const BCDh& b)
{
BCDh c;
BCDFloatH::div(&_v, &b._v, &c._v);
_v = c._v;
}
BCDh operator-() const
{
BCDh c(*this);
c.negate();
return c;
}
void operator++() { *this += 1; }
void operator--() { *this -= 1; }
static BCDh epsilon(int n)
{
BCDh v;
BCDFloatH::epsilon(n, &v._v);
return v;
}
friend int4 itrunc(const BCDh& a)
{ return BCDFloatH::itrunc(&a._v); }
friend BCDh trunc(const BCDh& a)
{
/* truncate towards zero.
* trunc(2.1) = 2.
* trunc(-2.1) = -2
*/
if (a.isSpecial()) return a;
BCDh t;
BCDFloatH::trunc(&a._v, &t._v);
return t;
}
friend BCDh fabs(const BCDh& a) { return (a.isNeg()) ? -a : a; }
friend BCDh frac(const BCDh& a)
{
if (a.isSpecial()) return a;
return a - trunc(a);
}
bool isZero() const { return _v.isZero(); }
bool isNeg() const { return _v.neg(); }
bool isSpecial() const
{ return _v.isSpecial(); }
bool isInf() const
{ return _v.isInf(); }
bool isNan() const
{ return _v.isNan(); }
bool isInteger() const { return _v.isInteger(); }
void negate() { _v.negate(); }
// Comparison
friend bool operator==(const BCDh& a, const BCDh& b)
{ return BCDFloatH::equal(&a._v, &b._v); }
friend bool operator!=(const BCDh& a, const BCDh& b)
{ return !BCDFloatH::equal(&a._v, &b._v); }
friend bool operator<(const BCDh& a, const BCDh& b)
{ return BCDFloatH::lt(&a._v, &b._v); }
friend bool operator<=(const BCDh& a, const BCDh& b)
{ return BCDFloatH::le(&a._v, &b._v); }
friend bool operator>(const BCDh& a, const BCDh& b)
{ return BCDFloatH::gt(&a._v, &b._v); }
friend bool operator>=(const BCDh& a, const BCDh& b)
{ return BCDFloatH::ge(&a._v, &b._v); }
BCDFloatH _v;
};
inline bool operator==(const BCDFloatDataH& a, const BCDFloatDataH& b)
{
return *(BCDh*)&a == *(BCDh*)&b;
}
inline bool operator<(const BCDFloatDataH& a, const BCDFloatDataH& b)
{
return *(BCDh*)&a < *(BCDh*)&b;
}
inline bool operator<=(const BCDFloatDataH& a, const BCDFloatDataH& b)
{
return *(BCDh*)&a <= *(BCDh*)&b;
}
inline bool operator>(const BCDFloatDataH& a, const BCDFloatDataH& b)
{
return *(BCDh*)&a > *(BCDh*)&b;
}
inline bool operator>=(const BCDFloatDataH& a, const BCDFloatDataH& b)
{
return *(BCDh*)&a >= *(BCDh*)&b;
}
inline BCDh operator-(const BCDFloatDataH& a, const BCDFloatDataH& b)
{
return *(BCDh*)&a - *(BCDh*)&b;
}
#endif // __bcdh_h__