-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.cc
More file actions
662 lines (558 loc) · 17.5 KB
/
basic.cc
File metadata and controls
662 lines (558 loc) · 17.5 KB
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
* MIT License
* Copyright (c) 2023 mxlol233 (mxlol233@outlook.com)
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
*LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "basic/apn.h"
#include "basic/exception.h"
#include "basic/file.h"
#include "basic/str.h"
#include "lex/base.h"
#include "src.h"
#include "tu.h"
namespace lps::basic {
FileVisitor::FileVisitor(
const char* start, const char* end,
const base::check_eof_callback_type& check_eof_callback, uint32_t file_id)
: base(start, end, std::move(check_eof_callback), file_id) {
eof_ = 0;
}
FileVisitor::~FileVisitor() {}
const char* FileVisitor::cur() const {
if (pos_ > len() || start_ > end_) {
return &eof_;
}
return start_ + pos_;
}
void FileVisitor::vertws_skipping() {
if (flg_skip_vertws_) {
skipping();
return;
}
vertws_skip(true);
skipping();
vertws_skip(false);
}
void FileVisitor::horzws_skipping() {
if (flg_skip_horzws_) {
skipping();
return;
}
horzws_skip(true);
skipping();
horzws_skip(false);
}
namespace apn {
namespace details {
static const FloatSemantics kSemIEEEhalf = {15, -14, 11, 16};
static const FloatSemantics kSemBFloat = {127, -126, 8, 16};
static const FloatSemantics kSemIEEEsingle = {127, -126, 24, 32};
static const FloatSemantics kSemIEEEdouble = {1023, -1022, 53, 64};
static const FloatSemantics kSemIEEEquad = {16383, -16382, 113, 128};
static const FloatSemantics kSemBogus = {0, 0, 0, 0};
const FloatSemantics& FloatBase::Semantics2FloatSemantics(
FloatBase::Semantics s) {
switch (s) {
case Semantics::kIEEEhalf:
return IEEEhalf();
case Semantics::kBFloat:
return BFloat();
case Semantics::kIEEEsingle:
return IEEEsingle();
case Semantics::kIEEEdouble:
return IEEEdouble();
case Semantics::kIEEEquad:
return IEEEquad();
default:
LPS_ERROR(kTag, "Unrecognised floating semantics");
}
LPS_ERROR(kTag, "Unrecognised floating semantics");
static FloatSemantics empty;
return empty;
}
FloatBase::Semantics FloatBase::FloatSemantics2Semantics(
const FloatSemantics& Sem) {
if (&Sem == &FloatBase::IEEEhalf())
return Semantics::kIEEEhalf;
if (&Sem == &FloatBase::BFloat())
return Semantics::kBFloat;
if (&Sem == &FloatBase::IEEEsingle())
return Semantics::kIEEEsingle;
if (&Sem == &FloatBase::IEEEdouble())
return Semantics::kIEEEdouble;
if (&Sem == &FloatBase::IEEEquad())
return Semantics::kIEEEquad;
LPS_ERROR(kTag, "Unknown floating semantics");
return Semantics::kUnknown;
}
const FloatSemantics& FloatBase::IEEEhalf() {
return kSemIEEEhalf;
}
const FloatSemantics& FloatBase::BFloat() {
return kSemBFloat;
}
const FloatSemantics& FloatBase::IEEEsingle() {
return kSemIEEEsingle;
}
const FloatSemantics& FloatBase::IEEEdouble() {
return kSemIEEEdouble;
}
const FloatSemantics& FloatBase::IEEEquad() {
return kSemIEEEquad;
}
IEEEFloat& IEEEFloat::operator=(IEEEFloat&& other) {
free_significand();
sem_ = other.sem_;
significand_ = other.significand_;
exp_ = other.exp_;
category_ = other.category_;
sign_ = other.sign_;
other.sem_ = &kSemBogus;
return *this;
}
} // namespace details
void Int::knuth_div(uint32_t* u, uint32_t* v, uint32_t* q, uint32_t* r,
unsigned m, unsigned n) {
LPS_CHECK_ERROR(kTag, u, "must provide dividend");
LPS_CHECK_ERROR(kTag, v, "must provide divisor");
LPS_CHECK_ERROR(kTag, q, "must provide quotient");
LPS_CHECK_ERROR(kTag, u != v && u != q && v != q,
"must use different memory");
LPS_CHECK_ERROR(kTag, n > 1, "n must be > 1");
// b denotes the base of the number system. In our case b is 2^32.
const uint64_t b = static_cast<uint64_t>(1) << 32;
// The DEBUG macros here tend to be spam in the debug output if you're not
// debugging this code. Disable them unless KNUTH_DEBUG is defined.
// D1. [Normalize.] Set d = b / (v[n-1] + 1) and multiply all the digits of
// u and v by d. Note that we have taken Knuth's advice here to use a power
// of 2 value for d such that d * v[n-1] >= b/2 (b is the base). A power of
// 2 allows us to shift instead of multiply and it is easy to determine the
// shift amount from the leading zeros. We are basically normalizing the u
// and v so that its high bits are shifted to the top of v's range without
// overflow. Note that this can require an extra word in u so that u must
// be of length m+n+1.
unsigned shift = details::countl_zero(v[n - 1]);
uint32_t v_carry = 0;
uint32_t u_carry = 0;
if (shift) {
for (unsigned i = 0; i < m + n; ++i) {
uint32_t u_tmp = u[i] >> (32 - shift);
u[i] = (u[i] << shift) | u_carry;
u_carry = u_tmp;
}
for (unsigned i = 0; i < n; ++i) {
uint32_t v_tmp = v[i] >> (32 - shift);
v[i] = (v[i] << shift) | v_carry;
v_carry = v_tmp;
}
}
u[m + n] = u_carry;
// D2. [Initialize j.] Set j to m. This is the loop counter over the places.
int j = m;
do {
// D3. [Calculate q'.].
// Set qp = (u[j+n]*b + u[j+n-1]) / v[n-1]. (qp=qprime=q')
// Set rp = (u[j+n]*b + u[j+n-1]) % v[n-1]. (rp=rprime=r')
// Now test if qp == b or qp*v[n-2] > b*rp + u[j+n-2]; if so, decrease
// qp by 1, increase rp by v[n-1], and repeat this test if rp < b. The test
// on v[n-2] determines at high speed most of the cases in which the trial
// value qp is one too large, and it eliminates all cases where qp is two
// too large.
uint64_t dividend = details::make_64(u[j + n], u[j + n - 1]);
uint64_t qp = dividend / v[n - 1];
uint64_t rp = dividend % v[n - 1];
if (qp == b || qp * v[n - 2] > b * rp + u[j + n - 2]) {
qp--;
rp += v[n - 1];
if (rp < b && (qp == b || qp * v[n - 2] > b * rp + u[j + n - 2]))
qp--;
}
// D4. [Multiply and subtract.] Replace (u[j+n]u[j+n-1]...u[j]) with
// (u[j+n]u[j+n-1]..u[j]) - qp * (v[n-1]...v[1]v[0]). This computation
// consists of a simple multiplication by a one-place number, combined with
// a subtraction.
// The digits (u[j+n]...u[j]) should be kept positive; if the result of
// this step is actually negative, (u[j+n]...u[j]) should be left as the
// true value plus b**(n+1), namely as the b's complement of
// the true value, and a "borrow" to the left should be remembered.
int64_t borrow = 0;
for (unsigned i = 0; i < n; ++i) {
uint64_t p = (qp) * static_cast<uint64_t>(v[i]);
int64_t subres =
static_cast<int64_t>(u[j + i]) - borrow - details::lo_32(p);
u[j + i] = details::lo_32(subres);
borrow = details::hi_32(p) - details::hi_32(subres);
}
bool is_neg = u[j + n] < borrow;
u[j + n] -= details::lo_32(borrow);
// D5. [Test remainder.] Set q[j] = qp. If the result of step D4 was
// negative, go to step D6; otherwise go on to step D7.
q[j] = details::lo_32(qp);
if (is_neg) {
// D6. [Add back]. The probability that this step is necessary is very
// small, on the order of only 2/b. Make sure that test data accounts for
// this possibility. Decrease q[j] by 1
q[j]--;
// and add (0v[n-1]...v[1]v[0]) to (u[j+n]u[j+n-1]...u[j+1]u[j]).
// A carry will occur to the left of u[j+n], and it should be ignored
// since it cancels with the borrow that occurred in D4.
bool carry = false;
for (unsigned i = 0; i < n; i++) {
uint32_t limit = std::min(u[j + i], v[i]);
u[j + i] += v[i] + static_cast<unsigned int>(carry);
carry = u[j + i] < limit || (carry && u[j + i] == limit);
}
u[j + n] += static_cast<unsigned int>(carry);
}
// D7. [Loop on j.] Decrease j by one. Now if j >= 0, go back to D3.
} while (--j >= 0);
// D8. [Unnormalize]. Now q[...] is the desired quotient, and the desired
// remainder may be obtained by dividing u[...] by d. If r is non-null we
// compute the remainder (urem uses this).
if (r) {
// The value d is expressed by the "shift" value above since we avoided
// multiplication by d by using a shift left. So, all we have to do is
// shift right here.
if (shift) {
uint32_t carry = 0;
for (int i = n - 1; i >= 0; i--) {
r[i] = (u[i] >> shift) | carry;
carry = u[i] << (32 - shift);
}
} else {
for (int i = n - 1; i >= 0; i--) {
r[i] = u[i];
}
}
}
}
Int operator-(Int v) {
v.negate();
return v;
}
Int operator+(Int a, const Int& b) {
a += b;
return a;
}
Int operator+(const Int& a, Int&& b) {
b += a;
return std::move(b);
}
Int operator+(Int a, uint64_t other) {
a += other;
return a;
}
Int operator+(uint64_t other, Int b) {
b += other;
return b;
}
Int operator-(Int a, const Int& b) {
a -= b;
return a;
}
Int operator-(const Int& a, Int&& b) {
b.negate();
b += a;
return std::move(b);
}
Int operator-(Int a, uint64_t other) {
a -= other;
return a;
}
Int operator-(uint64_t other, Int b) {
b.negate();
b += other;
return b;
}
Int operator*(Int a, uint64_t other) {
a *= other;
return a;
}
Int operator*(uint64_t other, Int b) {
b *= other;
return b;
}
bool operator==(uint64_t V1, const Int& V2) {
return V2 == V1;
}
bool operator!=(uint64_t V1, const Int& V2) {
return V2 != V1;
}
Int operator~(Int v) {
v.flip();
return v;
}
Int operator&(Int a, const Int& b) {
a &= b;
return a;
}
Int operator&(const Int& a, Int&& b) {
b &= a;
return std::move(b);
}
Int operator&(Int a, uint64_t other) {
a &= other;
return a;
}
Int operator&(uint64_t other, Int b) {
b &= other;
return b;
}
Int operator|(Int a, const Int& b) {
a |= b;
return a;
}
Int operator|(const Int& a, Int&& b) {
b |= a;
return std::move(b);
}
Int operator|(Int a, uint64_t other) {
a |= other;
return a;
}
Int operator|(uint64_t other, Int b) {
b |= other;
return b;
}
Int operator^(Int a, const Int& b) {
a ^= b;
return a;
}
Int operator^(const Int& a, Int&& b) {
b ^= a;
return std::move(b);
}
Int operator^(Int a, uint64_t other) {
a ^= other;
return a;
}
Int operator^(uint64_t other, Int b) {
b ^= other;
return b;
}
namespace details {
void IEEEFloat::init_from_half_int(const Int& api) {
return init_from_word<10, 0x1f, 0x3ff, 15, 15, 0x400>(api, kSemIEEEhalf);
}
void IEEEFloat::init_from_bfloat_int(const Int& api) {
return init_from_word<7, 0xff, 0x7f, 15, 127, 0x80>(api, kSemBFloat);
}
void IEEEFloat::init_from_float_int(const Int& api) {
return init_from_word<23, 0xff, 0x7fffff, 31, 127, 0x800000>(api,
kSemIEEEsingle);
}
void IEEEFloat::init_from_double_int(const Int& api) {
return init_from_word<52, 0x7ff, 0xfffffffffffffLL, 63, 1023,
0x10000000000000LL>(api, kSemIEEEdouble);
}
void IEEEFloat::init_from_quad_int(const Int& api) {
uint64_t i1 = api.raw_data()[0];
uint64_t i2 = api.raw_data()[1];
uint64_t exp = (i2 >> 48) & 0x7fff;
uint64_t significand = i1;
uint64_t significand2 = i2 & 0xffffffffffffLL;
initialize(&kSemIEEEquad);
lps_assert(kTag, n_parts() == 2);
sign_ = static_cast<unsigned int>(i2 >> 63);
if (exp == 0 && (significand == 0 && significand2 == 0)) {
make_zero(sign_ != 0U);
} else if (exp == 0x7fff && (significand == 0 && significand2 == 0)) {
make_inf(sign_ != 0U);
} else if (exp == 0x7fff && (significand != 0 || significand2 != 0)) {
category_ = FloatCategory::kNaN;
exp_ = exp_NaN();
significand_parts()[0] = significand;
significand_parts()[1] = significand2;
} else {
category_ = FloatCategory::kNormal;
exp_ = exp - 16383;
significand_parts()[0] = significand;
significand_parts()[1] = significand2;
if (exp == 0)
exp_ = -16382;
else
significand_parts()[1] |= 0x1000000000000LL;
}
}
void IEEEFloat::init_from_int(const FloatSemantics* Sem, const Int& api) {
lps_assert(kTag, api.bits() == Sem->bits_);
if (Sem == &kSemIEEEhalf)
return init_from_half_int(api);
if (Sem == &kSemBFloat)
return init_from_bfloat_int(api);
if (Sem == &kSemIEEEsingle)
return init_from_float_int(api);
if (Sem == &kSemIEEEdouble)
return init_from_double_int(api);
if (Sem == &kSemIEEEquad)
return init_from_quad_int(api);
unreachable(kTag);
}
IEEEFloat::IEEEFloat(double d) {
init_from_int(&kSemIEEEdouble, Int::double2bits(d));
}
IEEEFloat::IEEEFloat(float f) {
init_from_int(&kSemIEEEsingle, Int::float2bits(f));
}
IEEEFloat::IEEEFloat(IEEEFloat&& other) : sem_(&kSemBogus) {
*this = std::move(other);
}
bool IEEEFloat::exact_inverse(Float* inv) const {
if (!is_finite_nonzero())
return false;
if (LSB_significand() != sem_->precision_ - 1)
return false;
IEEEFloat reciprocal(*sem_, 1ULL);
if (reciprocal.div(*this, RoundingMode::kNearestTiesToEven) != OpStatus::kOK)
return false;
if (reciprocal.is_denormal())
return false;
assert(reciprocal.is_finite_nonzero() &&
reciprocal.LSB_significand() == reciprocal.sem_->precision_ - 1);
if (inv)
*inv = Float(reciprocal, *sem_);
return true;
}
Int IEEEFloat::bit_cast_to_int() const {
if (sem_ == &kSemIEEEhalf)
return convert_half_to_int();
if (sem_ == &kSemBFloat)
return convert_bfloat_to_int();
if (sem_ == &kSemIEEEsingle)
return convert_float_to_int();
if (sem_ == &kSemIEEEdouble)
return convert_double_to_int();
if (sem_ == &kSemIEEEquad)
return convert_quad_to_int();
unreachable(kTag);
return Int();
}
} // namespace details
float Float::to_float() const {
if (&sem() == &details::kSemIEEEsingle)
return IEEE().to_float();
LPS_CHECK_ERROR(kTag, sem().is_representable_by(details::kSemIEEEsingle),
"float semantics is not representable by IEEEsingle");
Float tmp = *this;
bool loses_info;
auto st = tmp.convert(details::kSemIEEEsingle,
details::FloatBase::RoundingMode::kNearestTiesToEven,
&loses_info);
LPS_CHECK_ERROR(
kTag,
!(static_cast<uint8_t>(st) &
static_cast<uint8_t>(details::FloatBase::OpStatus::kInexact)) &&
!loses_info,
"unexpected imprecision");
return tmp.IEEE().to_float();
}
double Float::to_double() const {
if (&sem() == &details::kSemIEEEdouble)
return IEEE().to_double();
LPS_CHECK_ERROR(kTag, sem().is_representable_by(details::kSemIEEEdouble),
"Float semantics is not representable by IEEEdouble");
Float tmp = *this;
bool loses_info;
auto st = tmp.convert(details::kSemIEEEdouble,
details::FloatBase::RoundingMode::kNearestTiesToEven,
&loses_info);
LPS_CHECK_ERROR(
kTag,
!(static_cast<uint8_t>(st) &
static_cast<uint8_t>(details::FloatBase::OpStatus::kInexact)) &&
!loses_info,
"unexpected imprecision");
return tmp.IEEE().to_double();
}
} // namespace apn
namespace vec {
static uint64_t auto_radix(StringRef& Str) {
if (Str.empty())
return 10;
if (Str.starts_with("0x") || Str.starts_with("0X")) {
Str = Str.substr(2);
return 16;
}
if (Str.starts_with("0b") || Str.starts_with("0B")) {
Str = Str.substr(2);
return 2;
}
if (Str.starts_with("0o")) {
Str = Str.substr(2);
return 8;
}
if (Str[0] == '0' && Str.size() > 1 && str::ascii::is::Digit(Str[1])) {
Str = Str.substr(1);
return 8;
}
return 10;
}
} // namespace vec
bool StringRef::as_int(uint64_t radix, apn::Int& result) const {
StringRef str = *this;
if (radix == 0)
radix = vec::auto_radix(str);
lps_assert(tag_, radix > 1 && radix <= 36);
if (str.empty())
return true;
while (!str.empty() && str.front() == '0')
str = str.substr(1);
if (str.empty()) {
result = apn::Int(64, 0);
return false;
}
unsigned log2_radix = 0;
while ((1U << log2_radix) < radix)
log2_radix++;
bool is_power2_radix = ((1U << log2_radix) == radix);
auto bits = log2_radix * str.size();
if (bits < result.bits())
bits = result.bits();
else if (bits > result.bits())
result = result.zext(bits);
apn::Int radix_int;
apn::Int char_int;
if (!is_power2_radix) {
radix_int = apn::Int(bits, radix);
char_int = apn::Int(bits, 0);
}
result = 0;
while (!str.empty()) {
unsigned char_val;
if (str[0] >= '0' && str[0] <= '9')
char_val = str[0] - '0';
else if (str[0] >= 'a' && str[0] <= 'z')
char_val = str[0] - 'a' + 10;
else if (str[0] >= 'A' && str[0] <= 'Z')
char_val = str[0] - 'A' + 10;
else
return true;
if (char_val >= radix)
return true;
if (is_power2_radix) {
result <<= log2_radix;
result |= char_val;
} else {
result *= radix_int;
char_int = char_val;
result += char_int;
}
str = str.substr(1);
}
return false;
}
} // namespace lps::basic