-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRealEncapsulation.h
592 lines (484 loc) · 17.9 KB
/
RealEncapsulation.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/*
RealLib, a library for efficient exact real computation
Copyright (C) 2006 Branimir Lambov
This library is licensed under the Apache License, Version 2.0 (the "License");
you may not use this library except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
RealEncapsulation.h
Encapsulates the different kinds of Estimates that can be used
in the system so that RealObjects can refer to them in an uniform way.
Classes:
Encapsulation - an Estimate object with each method amounting to a
switch on g_WorkingPrecision to choose a MachineEstimate or
Estimate implementation
*/
#ifndef FILE_REAL_Encapsulation_H
#define FILE_REAL_Encapsulation_H
#define MachineEstimatePrecision 4
#define UsingMachinePrecision g_WorkingPrecision == MachineEstimatePrecision
#define REALLIB_ENCAPPTR_ALLOCS_MEMORY
// non-memory allocating version does not work with arrays!
// arrays should be redone to use the classes mechanism (almost as efficient, less error prone)
#include <stdlib.h>
#include <limits.h>
#include <assert.h>
#include <malloc.h>
#include <exception>
#include "defs.h"
#include "LongFloat.h"
#include "RealEstimate.h"
#include "MachineEstimate.h"
namespace RealLib {
typedef long long UserInt;
// class Encapsulation's definitions start here
class Encapsulation;
// operations
static inline Encapsulation UnaryMinus (const Encapsulation &arg, UserInt user);
static inline Encapsulation recip(const Encapsulation &arg, UserInt user);
static inline Encapsulation Plus (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
static inline Encapsulation Minus (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
static inline Encapsulation Multiply (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
static inline Encapsulation Divide (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
// fast multiplication
static inline Encapsulation Multiply (const Encapsulation &lhs, UserInt rhs);
// and division
static inline Encapsulation Divide (const Encapsulation &lhs, UserInt rhs);
static inline
std::ostream& operator <<(std::ostream &os, const Encapsulation &e);
class Encapsulation {
private:
public:
// assuming MachineEstimate is as big as MachineEstimateBaseType
MachineEstimateBaseType Storage[(sizeof (Estimate) + sizeof(MachineEstimateBaseType)-1)
/sizeof(MachineEstimateBaseType)];
Estimate& rwEstimate()
{ return *((Estimate*)(Storage)); }
const Estimate& roEstimate() const
{ return *((const Estimate*)(Storage)); }
MachineEstimate& rwMachineEstimate()
{ return *((MachineEstimate*)(Storage)); }
const MachineEstimate& roMachineEstimate() const
{ return *((const MachineEstimate*)(Storage)); }
//MachineEstimate m_mach;
//Estimate m_est;
public:
Encapsulation(const Estimate &est)
{ assert(!(UsingMachinePrecision));
new (Storage) Estimate(est); }
Encapsulation(const MachineEstimate &mach)
{ assert(UsingMachinePrecision);
new (Storage) MachineEstimate(mach); }
Encapsulation(const Encapsulation &rhs)
{ if (UsingMachinePrecision)
new (Storage) MachineEstimate(rhs.roMachineEstimate());
else new (Storage) Estimate(rhs.roEstimate()); }
Encapsulation& operator=(const Encapsulation &rhs)
{ if (UsingMachinePrecision)
rwMachineEstimate() = rhs.roMachineEstimate();
else rwEstimate() = rhs.roEstimate();
return *this; }
~Encapsulation()
{ if (UsingMachinePrecision)
(rwMachineEstimate()).~MachineEstimate();
else (rwEstimate()).~Estimate(); }
public:
static void BeginComputation()
{ if (UsingMachinePrecision)
MachineEstimate::BeginComputation(); }
static void FinishComputation()
{ if (UsingMachinePrecision)
MachineEstimate::FinishComputation(); }
bool IsValueValid() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).IsValueValid();
else return true; }
class Computation {
public:
Computation()
{ BeginComputation(); }
~Computation()
{ FinishComputation(); }
};
Encapsulation(double v = 0.0)
{ if (UsingMachinePrecision)
new (Storage) MachineEstimate(v);
else new (Storage) Estimate(v); }
Encapsulation(const char *val)
{ if (UsingMachinePrecision)
new (Storage) MachineEstimate(val);
else new (Storage) Estimate(val); }
// error functions
Encapsulation GetError() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).GetError();
else return (roEstimate()).GetError(); }
Encapsulation& SetError(const Encapsulation &err)
{ if (UsingMachinePrecision)
(rwMachineEstimate()).SetError(err.roMachineEstimate());
else (rwEstimate()).SetError(err.roEstimate());
return *this; }
Encapsulation& AddError(const Encapsulation &err)
{ if (UsingMachinePrecision)
(rwMachineEstimate()).AddError(err.roMachineEstimate());
else (rwEstimate()).AddError(err.roEstimate());
return *this; }
// a lower bound on the correct binary digits
// uses the exponents of the value and error to calculate it quickly
i32 GetRelativeError() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).GetRelativeError();
else return (roEstimate()).GetRelativeError(); }
// get a rough estimate of the precision
// used to determine the length of the approximations to functions
u32 GetPrecision() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).GetPrecision();
else return (roEstimate()).GetPrecision(); }
Encapsulation& SetPrecision(u32 prec)
;// { m_Value.SetPrecision(prec);
// return *this; }
// comparisons
// these come in two flavors, strong (true if real is in relation to rhs)
bool IsPositive() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).IsPositive();
else return (roEstimate()).IsPositive(); }
bool IsNegative() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).IsNegative();
else return (roEstimate()).IsNegative(); }
bool IsNonZero() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).IsNonZero();
else return (roEstimate()).IsNonZero(); }
// left like this... maybe should be changed to use appropriate versions.
/*
bool operator < (const Encapsulation &rhs) const
{ return (*this - rhs).IsNegative(); }
bool operator > (const Encapsulation &rhs) const
{ return (*this - rhs).IsPositive(); }
bool operator != (const Encapsulation &rhs) const
{ return (*this - rhs).IsNonZero(); }
*/
bool weak_IsPositive() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_IsPositive();
else return (roEstimate()).weak_IsPositive(); }
bool weak_IsNegative() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_IsNegative();
else return (roEstimate()).weak_IsNegative(); }
bool weak_lt(const Encapsulation &rhs) const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_lt(rhs.roMachineEstimate());
else return (roEstimate()).weak_lt(rhs.roEstimate()); }
bool weak_eq(const Encapsulation &rhs) const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_eq(rhs.roMachineEstimate());
else return (roEstimate()).weak_eq(rhs.roEstimate()); }
bool weak_gt(const Encapsulation &rhs) const
{ return rhs.weak_lt(*this); }
bool weak_le(const Encapsulation &rhs) const
{ return !weak_gt(rhs); }
bool weak_ne(const Encapsulation &rhs) const
{ return !weak_eq(rhs); }
bool weak_ge(const Encapsulation &rhs) const
{ return !weak_lt(rhs); }
// among the weak operations is also rounding
// the returned Encapsulation is assumed exact
// only to be used on periodic functions!
Encapsulation weak_round() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_round();
else return (roEstimate()).weak_round(); }
// weak normalize, i.e. return an exponent such that
// a >> a.weak_normalize()
// is in the range [0.5, 1).
i32 weak_normalize() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_normalize();
else return (roEstimate()).weak_normalize(); }
// weak conversion
double weak_AsDouble() const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_AsDouble();
else return (roEstimate()).weak_AsDouble(); }
// output
char *weak_AsDecimal(char *buffer, u32 buflen) const
{ if (UsingMachinePrecision)
return (roMachineEstimate()).weak_AsDecimal(buffer, buflen);
else return (roEstimate()).weak_AsDecimal(buffer, buflen); }
friend Encapsulation UnaryMinus (const Encapsulation &arg, UserInt user);
friend Encapsulation recip(const Encapsulation &arg, UserInt user);
friend Encapsulation Plus (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
friend Encapsulation Minus (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
friend Encapsulation Multiply (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
friend Encapsulation Divide (const Encapsulation &lhs, const Encapsulation &rhs, UserInt user);
// fast multiplication
friend Encapsulation Multiply (const Encapsulation &lhs, UserInt rhs);
// and division
friend Encapsulation Divide (const Encapsulation &lhs, UserInt rhs);
// binary shift
Encapsulation operator << (i32 howmuch) const
{ if (UsingMachinePrecision)
return (roMachineEstimate()) << howmuch;
else return (roEstimate()) << howmuch; }
Encapsulation operator >> (i32 howmuch) const
{ return *this << -howmuch; }
/*
Encapsulation& operator += (const Encapsulation &rhs)
{ return *this = *this + rhs; }
Encapsulation& operator -= (const Encapsulation &rhs)
{ return *this = *this - rhs; }
Encapsulation& operator *= (const Encapsulation &rhs)
{ return *this = *this * rhs; }
Encapsulation& operator /= (const Encapsulation &rhs)
{ return *this = *this / rhs; }
Encapsulation& operator >>= (i32 rhs)
{ return *this = *this >> rhs; }
Encapsulation& operator <<= (i32 rhs)
{ return *this = *this << rhs; }
Encapsulation& operator *= (i32 rhs)
{ return *this = *this * rhs; }
Encapsulation& operator /= (i32 rhs)
{ return *this = *this / rhs; }*/
// should probably be somewhere else
// conversion to string
// char *AsDecimal(char *buffer, u32 buflen);
friend
std::ostream& operator <<(std::ostream &os, const Encapsulation &e);
};
class EncapsulationPointer {
private:
Encapsulation *ptr;
#ifndef REALLIB_ENCAPPTR_ALLOCS_MEMORY
#ifndef REALLIB_MACHINEESTIMATE_ALIGNMENT_REQUIRED
MachineEstimateBaseType Storage[(sizeof (Estimate) + sizeof(MachineEstimateBaseType)-1)
/sizeof(MachineEstimateBaseType)];
#else
MachineEstimateBaseType Storage[(sizeof (Estimate) + sizeof(MachineEstimateBaseType)-1)
/sizeof(MachineEstimateBaseType)+1];
#endif
#endif
explicit EncapsulationPointer(Encapsulation *p)
: ptr(p) { assert(p); };
public:
static EncapsulationPointer
FromPointer(Encapsulation *p)
{ return EncapsulationPointer(p); }
// copy protection
//EncapsulationPointer(const EncapsulationPointer &rhs);
public:
EncapsulationPointer()
: ptr(NULL) {}
EncapsulationPointer(const Encapsulation &val)
#ifdef REALLIB_ENCAPPTR_ALLOCS_MEMORY
#ifdef REALLIB_MACHINEESTIMATE_ALIGNMENT_REQUIRED
: ptr((Encapsulation*)_mm_malloc(sizeof(Encapsulation), sizeof(MachineEstimateBaseType)))
{
if (!ptr) throw std::bad_alloc();
else new (ptr) Encapsulation(val);
}
#else
: ptr(new Encapsulation(val))
{}
#endif
#else
#ifdef REALLIB_MACHINEESTIMATE_ALIGNMENT_REQUIRED
: ptr((Encapsulation*)(((ptrdiff_t)Storage)+15 & ~15))
#else
: ptr((Encapsulation*)Storage)
#endif
{
new(ptr) Encapsulation(val);
}
#endif
EncapsulationPointer(unsigned ArraySize)
#ifdef REALLIB_MACHINEESTIMATE_ALIGNMENT_REQUIRED
: ptr((Encapsulation*)_mm_malloc(sizeof(Encapsulation)*ArraySize, sizeof(MachineEstimateBaseType)))
{
if (!ptr) throw std::bad_alloc();
else for (int i=0;i<ArraySize;++i) {
new (ptr+i) Encapsulation;
}
}
#else
: ptr(new Encapsulation[ArraySize])
{}
#endif
~EncapsulationPointer()
{}
EncapsulationPointer& operator=(const EncapsulationPointer &rhs)
#ifdef REALLIB_ENCAPPTR_ALLOCS_MEMORY
{ ptr = rhs.ptr; return *this; }
#else
{
//*ptr = *rhs.ptr; return *this; }
if (!ptr)
#ifdef REALLIB_MACHINEESTIMATE_ALIGNMENT_REQUIRED
ptr = ((Encapsulation*)(((ptrdiff_t)Storage)+15 & ~15));
#else
ptr = ((Encapsulation*)Storage);
#endif
if (rhs.ptr)
*ptr = *rhs.ptr;
else ptr = NULL;
return *this; }
#endif
void Release()
{
#ifdef REALLIB_ENCAPPTR_ALLOCS_MEMORY
#ifdef REALLIB_MACHINEESTIMATE_ALIGNMENT_REQUIRED
assert(ptr);
ptr->~Encapsulation();
_mm_free(ptr);
#else
delete ptr;
#endif
#else
ptr->~Encapsulation();
#endif
ptr = NULL;
}
void ReleaseArray(unsigned ArraySize)
{
#ifdef REALLIB_MACHINEESTIMATE_ALIGNMENT_REQUIRED
assert(ptr);
for (int i=0;i<ArraySize;++i)
ptr[i].~Encapsulation();
_mm_free(ptr);
ptr = NULL;
#else
delete [] ptr;
ptr = NULL;
#endif
}
operator bool()
{ return !!ptr; }
Encapsulation& operator *() { return *ptr; }
const Encapsulation& operator *() const { return *ptr; }
typedef Encapsulation *EncPtr;
operator EncPtr() { return ptr; }
EncapsulationPointer operator+ (i32 v)
{ return EncapsulationPointer(ptr+v); }
};
template <class DEST, class SRC>
static inline DEST explicit_cast(SRC arg)
{ return DEST(arg); }
#ifdef _MSC_VER
template <>
static inline
Estimate explicit_cast<Estimate>(const Encapsulation &arg)
{ return arg.roEstimate(); }
#else
template <>
inline
Estimate explicit_cast<Estimate>(Encapsulation arg)
{ return arg.roEstimate(); }
#endif
#ifdef _MSC_VER
template <>
static inline
MachineEstimate explicit_cast<MachineEstimate>(const Encapsulation &arg)
{ return arg.roMachineEstimate(); }
#else
template <>
inline
MachineEstimate explicit_cast<MachineEstimate>(Encapsulation arg)
{ return arg.roMachineEstimate(); }
#endif
/*
template <>
Estimate& ExplicitConvert()
{ return (roEstimate()); }
template <>
MachineEstimate& ExplicitConvert()
{ return (roMachineEstimate()); }*/
/*
// shorthands
static inline
Encapsulation operator * (i32 lhs, const Encapsulation &rhs)
{ return rhs * lhs; }
/*{ if (UsingMachinePrecision)
return rhs.roMachineEstimate() * lhs;
else return rhs.roEstimate() * lhs; }*/
/*
static inline
Encapsulation operator / (i32 lhs, const Encapsulation &rhs)
{ return recip(rhs) * lhs; }
/*{ if (UsingMachinePrecision)
return recip(rhs.roMachineEstimate()) * lhs;
else return recip(rhs.roEstimate()) * lhs; }*/
// operations
static inline
Encapsulation UnaryMinus (const Encapsulation &arg, UserInt i)
{ if (UsingMachinePrecision)
return -arg.roMachineEstimate();
else return -arg.roEstimate(); }
static inline
Encapsulation recip(const Encapsulation &arg, UserInt i)
{ if (UsingMachinePrecision)
return recip(arg.roMachineEstimate());
else return recip(arg.roEstimate()); }
static inline
Encapsulation Plus (const Encapsulation &lhs, const Encapsulation &rhs, UserInt i)
{ if (UsingMachinePrecision)
return lhs.roMachineEstimate() + rhs.roMachineEstimate();
else return lhs.roEstimate() + rhs.roEstimate(); }
static inline
Encapsulation Minus (const Encapsulation &lhs, const Encapsulation &rhs, UserInt i)
{ if (UsingMachinePrecision)
return lhs.roMachineEstimate() - rhs.roMachineEstimate();
else return lhs.roEstimate() - rhs.roEstimate(); }
static inline
Encapsulation Multiply (const Encapsulation &lhs, const Encapsulation &rhs, UserInt i)
{ if (UsingMachinePrecision)
return lhs.roMachineEstimate() * rhs.roMachineEstimate();
else return lhs.roEstimate() * rhs.roEstimate(); }
static inline
Encapsulation Divide (const Encapsulation &lhs, const Encapsulation &rhs, UserInt i)
{ if (UsingMachinePrecision)
return lhs.roMachineEstimate() / rhs.roMachineEstimate();
else return lhs.roEstimate() / rhs.roEstimate(); }
// fast multiplication
static inline
Encapsulation Multiply (const Encapsulation &lhs, UserInt rhs)
{ if (UsingMachinePrecision)
return lhs.roMachineEstimate() * i32(rhs);
else return lhs.roEstimate() * i32(rhs); }
// and division
static inline
Encapsulation Divide (const Encapsulation &lhs, UserInt rhs)
{ if (UsingMachinePrecision)
return lhs.roMachineEstimate() / i32(rhs);
else return lhs.roEstimate() / i32(rhs); }
// C++-style output
static inline
std::ostream& operator <<(std::ostream &os, const Encapsulation &e)
{ if (UsingMachinePrecision)
return os << e.roMachineEstimate();
else return os << e.roEstimate(); }
// array interface
template <class TYPE, long offset = sizeof (TYPE)>
// GCC does not seem to support the above
//template <class TYPE, long offset>
class ArrayInterface {
char *arr;
long count;
public:
ArrayInterface(TYPE *p, int c)
: arr((char*)p), count(c) {}
long size() { return count; }
TYPE& operator[] (int index)
{ return *((TYPE*)(arr + index*offset)); }
};
} // namespace
#endif