FLANG
integer.h
1//===-- include/flang/Evaluate/integer.h ------------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_EVALUATE_INTEGER_H_
10#define FORTRAN_EVALUATE_INTEGER_H_
11
12// Emulates binary integers of an arbitrary (but fixed) bit size for use
13// when the host C++ environment does not support that size or when the
14// full suite of Fortran's integer intrinsic scalar functions are needed.
15// The data model is typeless, so signed* and unsigned operations
16// are distinguished from each other with distinct member function interfaces.
17// (*"Signed" here means two's-complement, just to be clear. Ones'-complement
18// and signed-magnitude encodings appear to be extinct in 2018.)
19
20#include "flang/Common/bit-population-count.h"
21#include "flang/Common/leading-zero-bit-count.h"
22#include "flang/Evaluate/common.h"
23#include <cinttypes>
24#include <climits>
25#include <cstddef>
26#include <cstdint>
27#include <string>
28#include <type_traits>
29
30// Some environments, viz. glibc 2.17 and *BSD, allow the macro HUGE
31// to leak out of <math.h>.
32#undef HUGE
33
34namespace Fortran::evaluate::value {
35
36// Computes decimal range in the sense of SELECTED_INT_KIND
37static constexpr int DecimalRange(int bits) {
38 // This magic value is LOG10(2.)*1E12.
39 return static_cast<int>((bits * 301029995664) / 1000000000000);
40}
41
42// Implements an integer as an assembly of smaller host integer parts
43// that constitute the digits of a large-radix fixed-point number.
44// For best performance, the type of these parts should be half of the
45// size of the largest efficient integer supported by the host processor.
46// These parts are stored in either little- or big-endian order, which can
47// match that of the host's endianness or not; but if the ordering matches
48// that of the host, raw host data can be overlaid with a properly configured
49// instance of this class and used in situ.
50// To facilitate exhaustive testing of what would otherwise be more rare
51// edge cases, this class template may be configured to use other part
52// types &/or partial fields in the parts. The radix (i.e., the number
53// of possible values in a part), however, must be a power of two; this
54// template class is not generalized to enable, say, decimal arithmetic.
55// Member functions that correspond to Fortran intrinsic functions are
56// named accordingly in ALL CAPS so that they can be referenced easily in
57// the language standard.
58template <int BITS, bool IS_LITTLE_ENDIAN = isHostLittleEndian,
59 int PARTBITS = BITS <= 32 ? BITS
60 : BITS % 32 == 0 ? 32
61 : BITS % 16 == 0 ? 16
62 : 8,
63 typename PART = HostUnsignedInt<PARTBITS>,
64 typename BIGPART = HostUnsignedInt<PARTBITS * 2>, int ALIGNMENT = BITS>
65class Integer {
66public:
67 static constexpr int bits{BITS};
68 static constexpr int partBits{PARTBITS};
69 using Part = PART;
70 using BigPart = BIGPART;
71 static_assert(std::is_integral_v<Part>);
72 static_assert(std::is_unsigned_v<Part>);
73 static_assert(std::is_integral_v<BigPart>);
74 static_assert(std::is_unsigned_v<BigPart>);
75 static_assert(CHAR_BIT * sizeof(BigPart) >= 2 * partBits);
76 static constexpr bool littleEndian{IS_LITTLE_ENDIAN};
77 static constexpr int alignment{ALIGNMENT};
78
79private:
80 static constexpr int maxPartBits{CHAR_BIT * sizeof(Part)};
81 static_assert(partBits > 0 && partBits <= maxPartBits);
82 static constexpr int extraPartBits{maxPartBits - partBits};
83 static constexpr int parts{(bits + partBits - 1) / partBits};
84 static_assert(parts >= 1);
85 static constexpr int extraTopPartBits{
86 extraPartBits + (parts * partBits) - bits};
87 static constexpr int topPartBits{maxPartBits - extraTopPartBits};
88 static_assert(topPartBits > 0 && topPartBits <= partBits);
89 static_assert((parts - 1) * partBits + topPartBits == bits);
90 static constexpr Part partMask{static_cast<Part>(~0) >> extraPartBits};
91 static constexpr Part topPartMask{static_cast<Part>(~0) >> extraTopPartBits};
92 static constexpr int partsWithAlignment{
93 (ALIGNMENT + partBits - 1) / partBits};
94
95public:
96 // Some types used for member function results
98 Integer value;
99 bool overflow;
100 };
101
103 Integer value;
104 bool carry;
105 };
106
107 struct Product {
108 bool SignedMultiplicationOverflowed() const {
109 return lower.IsNegative() ? (upper.POPCNT() != bits) : !upper.IsZero();
110 }
111 Integer upper, lower;
112 };
113
115 Integer quotient, remainder;
116 bool divisionByZero, overflow;
117 };
118
120 Integer power;
121 bool divisionByZero{false}, overflow{false}, zeroToZero{false};
122 };
123
124 // Constructors and value-generating static functions
125 constexpr Integer() { Clear(); } // default constructor: zero
126 constexpr Integer(const Integer &) = default;
127 constexpr Integer(Integer &&) = default;
128
129 // C++'s integral types can all be converted to Integer
130 // with silent truncation.
131 template <typename INT, typename = std::enable_if_t<std::is_integral_v<INT>>>
132 constexpr Integer(INT n) {
133 constexpr int nBits = CHAR_BIT * sizeof n;
134 if constexpr (nBits < partBits) {
135 if constexpr (std::is_unsigned_v<INT>) {
136 // Zero-extend an unsigned smaller value.
137 SetLEPart(0, n);
138 for (int j{1}; j < parts; ++j) {
139 SetLEPart(j, 0);
140 }
141 } else {
142 // n has a signed type smaller than the usable
143 // bits in a Part.
144 // Avoid conversions that change both size and sign.
145 using SignedPart = std::make_signed_t<Part>;
146 Part p = static_cast<SignedPart>(n);
147 SetLEPart(0, p);
148 if constexpr (parts > 1) {
149 Part signExtension = static_cast<SignedPart>(-(n < 0));
150 for (int j{1}; j < parts; ++j) {
151 SetLEPart(j, signExtension);
152 }
153 }
154 }
155 } else {
156 // n has some integral type no smaller than the usable
157 // bits in a Part.
158 // Ensure that all shifts are smaller than a whole word.
159 if constexpr (std::is_unsigned_v<INT>) {
160 for (int j{0}; j < parts; ++j) {
161 SetLEPart(j, static_cast<Part>(n));
162 if constexpr (nBits > partBits) {
163 n >>= partBits;
164 } else {
165 n = 0;
166 }
167 }
168 } else {
169 // Avoid left shifts of negative signed values (that's an undefined
170 // behavior in C++).
171 auto signExtension{std::make_unsigned_t<INT>(n < 0)};
172 signExtension = ~signExtension + 1;
173 static_assert(nBits >= partBits);
174 if constexpr (nBits > partBits) {
175 signExtension <<= nBits - partBits;
176 for (int j{0}; j < parts; ++j) {
177 SetLEPart(j, static_cast<Part>(n));
178 n >>= partBits;
179 n |= signExtension;
180 }
181 } else {
182 SetLEPart(0, static_cast<Part>(n));
183 for (int j{1}; j < parts; ++j) {
184 SetLEPart(j, static_cast<Part>(signExtension));
185 }
186 }
187 }
188 }
189 }
190
191 constexpr Integer &operator=(const Integer &) = default;
192
193 constexpr bool operator<(const Integer &that) const {
194 return CompareSigned(that) == Ordering::Less;
195 }
196 constexpr bool operator<=(const Integer &that) const {
197 return CompareSigned(that) != Ordering::Greater;
198 }
199 constexpr bool operator==(const Integer &that) const {
200 return CompareSigned(that) == Ordering::Equal;
201 }
202 constexpr bool operator!=(const Integer &that) const {
203 return !(*this == that);
204 }
205 constexpr bool operator>=(const Integer &that) const {
206 return CompareSigned(that) != Ordering::Less;
207 }
208 constexpr bool operator>(const Integer &that) const {
209 return CompareSigned(that) == Ordering::Greater;
210 }
211
212 // Left-justified mask (e.g., MASKL(1) has only its sign bit set)
213 static constexpr Integer MASKL(int places) {
214 if (places <= 0) {
215 return {};
216 } else if (places >= bits) {
217 return MASKR(bits);
218 } else {
219 return MASKR(bits - places).NOT();
220 }
221 }
222
223 // Right-justified mask (e.g., MASKR(1) == 1, MASKR(2) == 3, &c.)
224 static constexpr Integer MASKR(int places) {
225 Integer result{nullptr};
226 int j{0};
227 for (; j + 1 < parts && places >= partBits; ++j, places -= partBits) {
228 result.LEPart(j) = partMask;
229 }
230 if (places > 0) {
231 if (j + 1 < parts) {
232 result.LEPart(j++) = partMask >> (partBits - places);
233 } else if (j + 1 == parts) {
234 if (places >= topPartBits) {
235 result.LEPart(j++) = topPartMask;
236 } else {
237 result.LEPart(j++) = topPartMask >> (topPartBits - places);
238 }
239 }
240 }
241 for (; j < parts; ++j) {
242 result.LEPart(j) = 0;
243 }
244 return result;
245 }
246
247 static constexpr ValueWithOverflow Read(
248 const char *&pp, std::uint64_t base = 10, bool isSigned = false) {
249 Integer result;
250 bool overflow{false};
251 const char *p{pp};
252 while (*p == ' ' || *p == '\t') {
253 ++p;
254 }
255 bool negate{*p == '-'};
256 if (negate || *p == '+') {
257 while (*++p == ' ' || *p == '\t') {
258 }
259 }
260 Integer radix{base};
261 // This code makes assumptions about local contiguity in regions of the
262 // character set and only works up to base 36. These assumptions hold
263 // for all current combinations of surviving character sets (ASCII, UTF-8,
264 // EBCDIC) and the bases used in Fortran source and formatted I/O
265 // (viz., 2, 8, 10, & 16). But: management thought that a disclaimer
266 // might be needed here to warn future users of this code about these
267 // assumptions, so here you go, future programmer in some postapocalyptic
268 // hellscape, and best of luck with the inexorable killer robots.
269 for (; std::uint64_t digit = *p; ++p) {
270 if (digit >= '0' && digit <= '9' && digit < '0' + base) {
271 digit -= '0';
272 } else if (base > 10 && digit >= 'A' && digit < 'A' + base - 10) {
273 digit -= 'A' - 10;
274 } else if (base > 10 && digit >= 'a' && digit < 'a' + base - 10) {
275 digit -= 'a' - 10;
276 } else {
277 break;
278 }
279 Product shifted{result.MultiplyUnsigned(radix)};
280 overflow |= !shifted.upper.IsZero();
281 ValueWithCarry next{shifted.lower.AddUnsigned(Integer{digit})};
282 overflow |= next.carry;
283 result = next.value;
284 }
285 pp = p;
286 if (negate) {
287 result = result.Negate().value;
288 overflow |= isSigned && !result.IsNegative() && !result.IsZero();
289 } else {
290 overflow |= isSigned && result.IsNegative();
291 }
292 return {result, overflow};
293 }
294
295 template <typename FROM>
296 static constexpr ValueWithOverflow ConvertUnsigned(const FROM &that) {
297 std::uint64_t field{that.ToUInt64()};
298 ValueWithOverflow result{field, false};
299 if constexpr (bits < 64) {
300 result.overflow = (field >> bits) != 0;
301 }
302 for (int j{64}; j < that.bits && !result.overflow; j += 64) {
303 field = that.SHIFTR(j).ToUInt64();
304 if (bits <= j) {
305 result.overflow = field != 0;
306 } else {
307 result.value = result.value.IOR(Integer{field}.SHIFTL(j));
308 if (bits < j + 64) {
309 result.overflow = (field >> (bits - j)) != 0;
310 }
311 }
312 }
313 return result;
314 }
315
316 template <typename FROM>
317 static constexpr ValueWithOverflow ConvertSigned(const FROM &that) {
318 ValueWithOverflow result{ConvertUnsigned(that)};
319 if constexpr (bits > FROM::bits) {
320 if (that.IsNegative()) {
321 result.value = result.value.IOR(MASKL(bits - FROM::bits));
322 }
323 result.overflow = false;
324 } else if constexpr (bits < FROM::bits) {
325 auto back{FROM::template ConvertSigned<Integer>(result.value)};
326 result.overflow = back.value.CompareUnsigned(that) != Ordering::Equal;
327 }
328 return result;
329 }
330
331 std::string UnsignedDecimal() const {
332 if constexpr (bits < 4) {
333 char digit = '0' + ToUInt64();
334 return {digit};
335 } else if (IsZero()) {
336 return {'0'};
337 } else {
338 QuotientWithRemainder qr{DivideUnsigned(10)};
339 char digit = '0' + qr.remainder.ToUInt64();
340 if (qr.quotient.IsZero()) {
341 return {digit};
342 } else {
343 return qr.quotient.UnsignedDecimal() + digit;
344 }
345 }
346 }
347
348 std::string SignedDecimal() const {
349 if (IsNegative()) {
350 return std::string{'-'} + Negate().value.UnsignedDecimal();
351 } else {
352 return UnsignedDecimal();
353 }
354 }
355
356 // Omits a leading "0x".
357 std::string Hexadecimal() const {
358 std::string result;
359 int digits{(bits + 3) >> 2};
360 for (int j{0}; j < digits; ++j) {
361 int pos{(digits - 1 - j) * 4};
362 char nybble = IBITS(pos, 4).ToUInt64();
363 if (nybble != 0 || !result.empty() || j + 1 == digits) {
364 char digit = '0' + nybble;
365 if (digit > '9') {
366 digit += 'a' - ('9' + 1);
367 }
368 result += digit;
369 }
370 }
371 return result;
372 }
373
374 static constexpr int DIGITS{bits - 1}; // don't count the sign bit
375 static constexpr Integer HUGE() { return MASKR(bits - 1); }
376 static constexpr Integer Least() { return MASKL(1); }
377 static constexpr int RANGE{DecimalRange(bits - 1)};
378 static constexpr int UnsignedRANGE{DecimalRange(bits)};
379
380 constexpr bool IsZero() const {
381 for (int j{0}; j < parts; ++j) {
382 if (part_[j] != 0) {
383 return false;
384 }
385 }
386 return true;
387 }
388
389 constexpr bool IsNegative() const {
390 return (LEPart(parts - 1) >> (topPartBits - 1)) & 1;
391 }
392
393 constexpr Ordering CompareToZeroSigned() const {
394 if (IsNegative()) {
395 return Ordering::Less;
396 } else if (IsZero()) {
397 return Ordering::Equal;
398 } else {
399 return Ordering::Greater;
400 }
401 }
402
403 // Count the number of contiguous most-significant bit positions
404 // that are clear.
405 constexpr int LEADZ() const {
406 if (LEPart(parts - 1) != 0) {
407 int lzbc{common::LeadingZeroBitCount(LEPart(parts - 1))};
408 return lzbc - extraTopPartBits;
409 }
410 int upperZeroes{topPartBits};
411 for (int j{1}; j < parts; ++j) {
412 if (Part p{LEPart(parts - 1 - j)}) {
413 int lzbc{common::LeadingZeroBitCount(p)};
414 return upperZeroes + lzbc - extraPartBits;
415 }
416 upperZeroes += partBits;
417 }
418 return bits;
419 }
420
421 // Count the number of bit positions that are set.
422 constexpr int POPCNT() const {
423 int count{0};
424 for (int j{0}; j < parts; ++j) {
425 count += common::BitPopulationCount(part_[j]);
426 }
427 return count;
428 }
429
430 // True when POPCNT is odd.
431 constexpr bool POPPAR() const { return POPCNT() & 1; }
432
433 constexpr int TRAILZ() const {
434 auto minus1{AddUnsigned(MASKR(bits))}; // { x-1, carry = x > 0 }
435 if (!minus1.carry) {
436 return bits; // was zero
437 } else {
438 // x ^ (x-1) has all bits set at and below original least-order set bit.
439 return IEOR(minus1.value).POPCNT() - 1;
440 }
441 }
442
443 constexpr bool BTEST(int pos) const {
444 if (pos < 0 || pos >= bits) {
445 return false;
446 } else {
447 return (LEPart(pos / partBits) >> (pos % partBits)) & 1;
448 }
449 }
450
451 constexpr Ordering CompareUnsigned(const Integer &y) const {
452 for (int j{parts}; j-- > 0;) {
453 if (LEPart(j) > y.LEPart(j)) {
454 return Ordering::Greater;
455 }
456 if (LEPart(j) < y.LEPart(j)) {
457 return Ordering::Less;
458 }
459 }
460 return Ordering::Equal;
461 }
462
463 constexpr bool BGE(const Integer &y) const {
464 return CompareUnsigned(y) != Ordering::Less;
465 }
466 constexpr bool BGT(const Integer &y) const {
467 return CompareUnsigned(y) == Ordering::Greater;
468 }
469 constexpr bool BLE(const Integer &y) const { return !BGT(y); }
470 constexpr bool BLT(const Integer &y) const { return !BGE(y); }
471
472 constexpr Ordering CompareSigned(const Integer &y) const {
473 bool isNegative{IsNegative()};
474 if (isNegative != y.IsNegative()) {
475 return isNegative ? Ordering::Less : Ordering::Greater;
476 }
477 return CompareUnsigned(y);
478 }
479
480 template <typename UINT = std::uint64_t> constexpr UINT ToUInt() const {
481 UINT n{LEPart(0)};
482 std::size_t filled{partBits};
483 constexpr std::size_t maxBits{CHAR_BIT * sizeof n};
484 for (int j{1}; filled < maxBits && j < parts; ++j, filled += partBits) {
485 n |= UINT{LEPart(j)} << filled;
486 }
487 return n;
488 }
489
490 template <typename SINT = std::int64_t, typename UINT = std::uint64_t>
491 constexpr SINT ToSInt() const {
492 SINT n = ToUInt<UINT>();
493 constexpr std::size_t maxBits{CHAR_BIT * sizeof n};
494 if constexpr (bits < maxBits) {
495 // Avoid left shifts of negative signed values (that's an undefined
496 // behavior in C++).
497 auto u{std::make_unsigned_t<SINT>(ToUInt())};
498 u = (u >> (bits - 1)) << (bits - 1); // Get the sign bit only.
499 u = ~u + 1; // Negate top bits if not 0.
500 n |= static_cast<SINT>(u);
501 }
502 return n;
503 }
504
505 constexpr std::uint64_t ToUInt64() const { return ToUInt<std::uint64_t>(); }
506
507 constexpr std::int64_t ToInt64() const {
508 return ToSInt<std::int64_t, std::uint64_t>();
509 }
510
511 // Ones'-complement (i.e., C's ~)
512 constexpr Integer NOT() const {
513 Integer result{nullptr};
514 for (int j{0}; j < parts; ++j) {
515 result.SetLEPart(j, ~LEPart(j));
516 }
517 return result;
518 }
519
520 // Two's-complement negation (-x = ~x + 1).
521 // An overflow flag accompanies the result, and will be true when the
522 // operand is the most negative signed number (MASKL(1)).
523 constexpr ValueWithOverflow Negate() const {
524 Integer result{nullptr};
525 Part carry{1};
526 for (int j{0}; j + 1 < parts; ++j) {
527 Part newCarry{LEPart(j) == 0 && carry};
528 result.SetLEPart(j, ~LEPart(j) + carry);
529 carry = newCarry;
530 }
531 Part top{LEPart(parts - 1)};
532 result.SetLEPart(parts - 1, ~top + carry);
533 bool overflow{top != 0 && result.LEPart(parts - 1) == top};
534 return {result, overflow};
535 }
536
537 constexpr ValueWithOverflow ABS() const {
538 if (IsNegative()) {
539 return Negate();
540 } else {
541 return {*this, false};
542 }
543 }
544
545 // Shifts the operand left when the count is positive, right when negative.
546 // Vacated bit positions are filled with zeroes.
547 constexpr Integer ISHFT(int count) const {
548 if (count < 0) {
549 return SHIFTR(-count);
550 } else {
551 return SHIFTL(count);
552 }
553 }
554
555 // Left shift with zero fill.
556 constexpr Integer SHIFTL(int count) const {
557 if (count <= 0) {
558 return *this;
559 } else {
560 Integer result{nullptr};
561 int shiftParts{count / partBits};
562 int bitShift{count - partBits * shiftParts};
563 int j{parts - 1};
564 if (bitShift == 0) {
565 for (; j >= shiftParts; --j) {
566 result.SetLEPart(j, LEPart(j - shiftParts));
567 }
568 for (; j >= 0; --j) {
569 result.LEPart(j) = 0;
570 }
571 } else {
572 for (; j > shiftParts; --j) {
573 result.SetLEPart(j,
574 ((LEPart(j - shiftParts) << bitShift) |
575 (LEPart(j - shiftParts - 1) >> (partBits - bitShift))));
576 }
577 if (j == shiftParts) {
578 result.SetLEPart(j, LEPart(0) << bitShift);
579 --j;
580 }
581 for (; j >= 0; --j) {
582 result.LEPart(j) = 0;
583 }
584 }
585 return result;
586 }
587 }
588
589 // Circular shift of a field of least-significant bits. The least-order
590 // "size" bits are shifted circularly in place by "count" positions;
591 // the shift is leftward if count is nonnegative, rightward otherwise.
592 // Higher-order bits are unchanged.
593 constexpr Integer ISHFTC(int count, int size = bits) const {
594 if (count == 0 || size <= 0) {
595 return *this;
596 }
597 if (size > bits) {
598 size = bits;
599 }
600 count %= size;
601 if (count == 0) {
602 return *this;
603 }
604 int middleBits{size - count}, leastBits{count};
605 if (count < 0) {
606 middleBits = -count;
607 leastBits = size + count;
608 }
609 if (size == bits) {
610 return SHIFTL(leastBits).IOR(SHIFTR(middleBits));
611 }
612 Integer unchanged{IAND(MASKL(bits - size))};
613 Integer middle{IAND(MASKR(middleBits)).SHIFTL(leastBits)};
614 Integer least{SHIFTR(middleBits).IAND(MASKR(leastBits))};
615 return unchanged.IOR(middle).IOR(least);
616 }
617
618 // Double shifts, aka shifts with specific fill.
619 constexpr Integer SHIFTLWithFill(const Integer &fill, int count) const {
620 if (count <= 0) {
621 return *this;
622 } else if (count >= 2 * bits) {
623 return {};
624 } else if (count > bits) {
625 return fill.SHIFTL(count - bits);
626 } else if (count == bits) {
627 return fill;
628 } else {
629 return SHIFTL(count).IOR(fill.SHIFTR(bits - count));
630 }
631 }
632
633 constexpr Integer SHIFTRWithFill(const Integer &fill, int count) const {
634 if (count <= 0) {
635 return *this;
636 } else if (count >= 2 * bits) {
637 return {};
638 } else if (count > bits) {
639 return fill.SHIFTR(count - bits);
640 } else if (count == bits) {
641 return fill;
642 } else {
643 return SHIFTR(count).IOR(fill.SHIFTL(bits - count));
644 }
645 }
646
647 constexpr Integer DSHIFTL(const Integer &fill, int count) const {
648 // DSHIFTL(I,J) shifts I:J left; the second argument is the right fill.
649 return SHIFTLWithFill(fill, count);
650 }
651
652 constexpr Integer DSHIFTR(const Integer &value, int count) const {
653 // DSHIFTR(I,J) shifts I:J right; the *first* argument is the left fill.
654 return value.SHIFTRWithFill(*this, count);
655 }
656
657 // Vacated upper bits are filled with zeroes.
658 constexpr Integer SHIFTR(int count) const {
659 if (count <= 0) {
660 return *this;
661 } else {
662 Integer result{nullptr};
663 int shiftParts{count / partBits};
664 int bitShift{count - partBits * shiftParts};
665 int j{0};
666 if (bitShift == 0) {
667 for (; j + shiftParts < parts; ++j) {
668 result.LEPart(j) = LEPart(j + shiftParts);
669 }
670 for (; j < parts; ++j) {
671 result.LEPart(j) = 0;
672 }
673 } else {
674 for (; j + shiftParts + 1 < parts; ++j) {
675 result.SetLEPart(j,
676 (LEPart(j + shiftParts) >> bitShift) |
677 (LEPart(j + shiftParts + 1) << (partBits - bitShift)));
678 }
679 if (j + shiftParts + 1 == parts) {
680 result.LEPart(j++) = LEPart(parts - 1) >> bitShift;
681 }
682 for (; j < parts; ++j) {
683 result.LEPart(j) = 0;
684 }
685 }
686 return result;
687 }
688 }
689
690 // Be advised, an arithmetic (sign-filling) right shift is not
691 // the same as a division by a power of two in all cases.
692 constexpr Integer SHIFTA(int count) const {
693 if (count <= 0) {
694 return *this;
695 } else if (IsNegative()) {
696 return SHIFTR(count).IOR(MASKL(count));
697 } else {
698 return SHIFTR(count);
699 }
700 }
701
702 // Clears a single bit.
703 constexpr Integer IBCLR(int pos) const {
704 if (pos < 0 || pos >= bits) {
705 return *this;
706 } else {
707 Integer result{*this};
708 result.LEPart(pos / partBits) &= ~(Part{1} << (pos % partBits));
709 return result;
710 }
711 }
712
713 // Sets a single bit.
714 constexpr Integer IBSET(int pos) const {
715 if (pos < 0 || pos >= bits) {
716 return *this;
717 } else {
718 Integer result{*this};
719 result.LEPart(pos / partBits) |= Part{1} << (pos % partBits);
720 return result;
721 }
722 }
723
724 // Extracts a field.
725 constexpr Integer IBITS(int pos, int size) const {
726 return SHIFTR(pos).IAND(MASKR(size));
727 }
728
729 constexpr Integer IAND(const Integer &y) const {
730 Integer result{nullptr};
731 for (int j{0}; j < parts; ++j) {
732 result.LEPart(j) = LEPart(j) & y.LEPart(j);
733 }
734 return result;
735 }
736
737 constexpr Integer IOR(const Integer &y) const {
738 Integer result{nullptr};
739 for (int j{0}; j < parts; ++j) {
740 result.LEPart(j) = LEPart(j) | y.LEPart(j);
741 }
742 return result;
743 }
744
745 constexpr Integer IEOR(const Integer &y) const {
746 Integer result{nullptr};
747 for (int j{0}; j < parts; ++j) {
748 result.LEPart(j) = LEPart(j) ^ y.LEPart(j);
749 }
750 return result;
751 }
752
753 constexpr Integer MERGE_BITS(const Integer &y, const Integer &mask) const {
754 return IAND(mask).IOR(y.IAND(mask.NOT()));
755 }
756
757 constexpr Integer MAX(const Integer &y) const {
758 if (CompareSigned(y) == Ordering::Less) {
759 return y;
760 } else {
761 return *this;
762 }
763 }
764
765 constexpr Integer MIN(const Integer &y) const {
766 if (CompareSigned(y) == Ordering::Less) {
767 return *this;
768 } else {
769 return y;
770 }
771 }
772
773 // Unsigned addition with carry.
774 constexpr ValueWithCarry AddUnsigned(
775 const Integer &y, bool carryIn = false) const {
776 Integer sum{nullptr};
777 BigPart carry{carryIn};
778 for (int j{0}; j + 1 < parts; ++j) {
779 carry += LEPart(j);
780 carry += y.LEPart(j);
781 sum.SetLEPart(j, carry);
782 carry >>= partBits;
783 }
784 carry += LEPart(parts - 1);
785 carry += y.LEPart(parts - 1);
786 sum.SetLEPart(parts - 1, carry);
787 return {sum, carry > topPartMask};
788 }
789
790 constexpr ValueWithOverflow AddSigned(const Integer &y) const {
791 bool isNegative{IsNegative()};
792 bool sameSign{isNegative == y.IsNegative()};
793 ValueWithCarry sum{AddUnsigned(y)};
794 bool overflow{sameSign && sum.value.IsNegative() != isNegative};
795 return {sum.value, overflow};
796 }
797
798 constexpr ValueWithOverflow SubtractSigned(const Integer &y) const {
799 bool isNegative{IsNegative()};
800 bool sameSign{isNegative == y.IsNegative()};
801 ValueWithCarry diff{AddUnsigned(y.Negate().value)};
802 bool overflow{!sameSign && diff.value.IsNegative() != isNegative};
803 return {diff.value, overflow};
804 }
805
806 // DIM(X,Y)=MAX(X-Y, 0)
807 constexpr ValueWithOverflow DIM(const Integer &y) const {
808 if (CompareSigned(y) != Ordering::Greater) {
809 return {};
810 } else {
811 return SubtractSigned(y);
812 }
813 }
814
815 constexpr ValueWithOverflow SIGN(bool toNegative) const {
816 if (toNegative == IsNegative()) {
817 return {*this, false};
818 } else if (toNegative) {
819 return Negate();
820 } else {
821 return ABS();
822 }
823 }
824
825 constexpr ValueWithOverflow SIGN(const Integer &sign) const {
826 return SIGN(sign.IsNegative());
827 }
828
829 constexpr Product MultiplyUnsigned(const Integer &y) const {
830 Part product[2 * parts]{}; // little-endian full product
831 for (int j{0}; j < parts; ++j) {
832 if (Part xpart{LEPart(j)}) {
833 for (int k{0}; k < parts; ++k) {
834 if (Part ypart{y.LEPart(k)}) {
835 BigPart xy{xpart};
836 xy *= ypart;
837#if defined __GNUC__ && __GNUC__ < 8 || __GNUC__ >= 12
838 // && to < (2 * parts) was added to avoid GCC build failure on
839 // -Werror=array-bounds. This can be removed if -Werror is disabled.
840 for (int to{j + k}; xy != 0 && to < (2 * parts); ++to) {
841#else
842 for (int to{j + k}; xy != 0; ++to) {
843#endif
844 xy += product[to];
845 product[to] = xy & partMask;
846 xy >>= partBits;
847 }
848 }
849 }
850 }
851 }
852 Integer upper{nullptr}, lower{nullptr};
853 for (int j{0}; j < parts; ++j) {
854 lower.LEPart(j) = product[j];
855 upper.LEPart(j) = product[j + parts];
856 }
857 if constexpr (topPartBits < partBits) {
858 upper = upper.SHIFTL(partBits - topPartBits);
859 upper.LEPart(0) |= lower.LEPart(parts - 1) >> topPartBits;
860 lower.LEPart(parts - 1) &= topPartMask;
861 }
862 return {upper, lower};
863 }
864
865 constexpr Product MultiplySigned(const Integer &y) const {
866 bool yIsNegative{y.IsNegative()};
867 Integer absy{y};
868 if (yIsNegative) {
869 absy = y.Negate().value;
870 }
871 bool isNegative{IsNegative()};
872 Integer absx{*this};
873 if (isNegative) {
874 absx = Negate().value;
875 }
876 Product product{absx.MultiplyUnsigned(absy)};
877 if (isNegative != yIsNegative) {
878 product.lower = product.lower.NOT();
879 product.upper = product.upper.NOT();
880 Integer one{1};
881 auto incremented{product.lower.AddUnsigned(one)};
882 product.lower = incremented.value;
883 if (incremented.carry) {
884 product.upper = product.upper.AddUnsigned(one).value;
885 }
886 }
887 return product;
888 }
889
890 constexpr QuotientWithRemainder DivideUnsigned(const Integer &divisor) const {
891 if (divisor.IsZero()) {
892 return {MASKR(bits), Integer{}, true, false}; // overflow to max value
893 }
894 int bitsDone{LEADZ()};
895 Integer top{SHIFTL(bitsDone)};
896 Integer quotient, remainder;
897 for (; bitsDone < bits; ++bitsDone) {
898 auto doubledTop{top.AddUnsigned(top)};
899 top = doubledTop.value;
900 remainder = remainder.AddUnsigned(remainder, doubledTop.carry).value;
901 bool nextBit{remainder.CompareUnsigned(divisor) != Ordering::Less};
902 quotient = quotient.AddUnsigned(quotient, nextBit).value;
903 if (nextBit) {
904 remainder = remainder.SubtractSigned(divisor).value;
905 }
906 }
907 return {quotient, remainder, false, false};
908 }
909
910 // A nonzero remainder has the sign of the dividend, i.e., it computes
911 // the MOD intrinsic (X-INT(X/Y)*Y), not MODULO (which is below).
912 // 8/5 = 1r3; -8/5 = -1r-3; 8/-5 = -1r3; -8/-5 = 1r-3
913 constexpr QuotientWithRemainder DivideSigned(Integer divisor) const {
914 bool dividendIsNegative{IsNegative()};
915 bool negateQuotient{dividendIsNegative};
916 Ordering divisorOrdering{divisor.CompareToZeroSigned()};
917 if (divisorOrdering == Ordering::Less) {
918 negateQuotient = !negateQuotient;
919 auto negated{divisor.Negate()};
920 if (negated.overflow) {
921 // divisor was (and is) the most negative number
922 if (CompareUnsigned(divisor) == Ordering::Equal) {
923 return {MASKR(1), Integer{}, false, bits <= 1};
924 } else {
925 return {Integer{}, *this, false, false};
926 }
927 }
928 divisor = negated.value;
929 } else if (divisorOrdering == Ordering::Equal) {
930 // division by zero
931 if (dividendIsNegative) {
932 return {MASKL(1), Integer{}, true, false};
933 } else {
934 return {MASKR(bits - 1), Integer{}, true, false};
935 }
936 }
937 Integer dividend{*this};
938 if (dividendIsNegative) {
939 auto negated{Negate()};
940 if (negated.overflow) {
941 // Dividend was (and remains) the most negative number.
942 // See whether the original divisor was -1 (if so, it's 1 now).
943 if (divisorOrdering == Ordering::Less &&
944 divisor.CompareUnsigned(Integer{1}) == Ordering::Equal) {
945 // most negative number / -1 is the sole overflow case
946 return {*this, Integer{}, false, true};
947 }
948 } else {
949 dividend = negated.value;
950 }
951 }
952 // Overflow is not possible, and both the dividend and divisor
953 // are now positive.
954 QuotientWithRemainder result{dividend.DivideUnsigned(divisor)};
955 if (negateQuotient) {
956 result.quotient = result.quotient.Negate().value;
957 }
958 if (dividendIsNegative) {
959 result.remainder = result.remainder.Negate().value;
960 }
961 return result;
962 }
963
964 // Result has the sign of the divisor argument.
965 // 8 mod 5 = 3; -8 mod 5 = 2; 8 mod -5 = -2; -8 mod -5 = -3
966 constexpr ValueWithOverflow MODULO(const Integer &divisor) const {
967 bool negativeDivisor{divisor.IsNegative()};
968 bool distinctSigns{IsNegative() != negativeDivisor};
969 QuotientWithRemainder divided{DivideSigned(divisor)};
970 if (distinctSigns && !divided.remainder.IsZero()) {
971 return {divided.remainder.AddUnsigned(divisor).value, divided.overflow};
972 } else {
973 return {divided.remainder, divided.overflow};
974 }
975 }
976
977 constexpr PowerWithErrors Power(const Integer &exponent) const {
978 PowerWithErrors result{1, false, false, false};
979 if (exponent.IsZero()) {
980 // x**0 -> 1, including the case 0**0, which is not defined specifically
981 // in F'18 afaict; however, other Fortrans tested all produce 1, not 0,
982 // apart from nagfor, which stops with an error at runtime.
983 // Ada, APL, C's pow(), Haskell, Julia, MATLAB, and R all produce 1 too.
984 // F'77 explicitly states that 0**0 is mathematically undefined and
985 // therefore prohibited.
986 result.zeroToZero = IsZero();
987 } else if (exponent.IsNegative()) {
988 if (IsZero()) {
989 result.divisionByZero = true;
990 result.power = MASKR(bits - 1);
991 } else if (CompareSigned(Integer{1}) == Ordering::Equal) {
992 result.power = *this; // 1**x -> 1
993 } else if (CompareSigned(Integer{-1}) == Ordering::Equal) {
994 if (exponent.BTEST(0)) {
995 result.power = *this; // (-1)**x -> -1 if x is odd
996 }
997 } else {
998 result.power.Clear(); // j**k -> 0 if |j| > 1 and k < 0
999 }
1000 } else {
1001 Integer shifted{*this};
1002 Integer pow{exponent};
1003 int nbits{bits - pow.LEADZ()};
1004 for (int j{0}; j < nbits; ++j) {
1005 if (pow.BTEST(j)) {
1006 Product product{result.power.MultiplySigned(shifted)};
1007 result.power = product.lower;
1008 result.overflow |= product.SignedMultiplicationOverflowed();
1009 }
1010 if (j + 1 < nbits) {
1011 Product squared{shifted.MultiplySigned(shifted)};
1012 result.overflow |= squared.SignedMultiplicationOverflowed();
1013 shifted = squared.lower;
1014 }
1015 }
1016 }
1017 return result;
1018 }
1019
1020private:
1021 // A private constructor, selected by the use of nullptr,
1022 // that is used by member functions when it would be a waste
1023 // of time to initialize parts_[].
1024 constexpr Integer(std::nullptr_t) {}
1025
1026 // Accesses parts in little-endian order.
1027 constexpr const Part &LEPart(int part) const {
1028 if constexpr (littleEndian) {
1029 return part_[part];
1030 } else {
1031 return part_[parts - 1 - part];
1032 }
1033 }
1034
1035 constexpr Part &LEPart(int part) {
1036 if constexpr (littleEndian) {
1037 return part_[part];
1038 } else {
1039 return part_[parts - 1 - part];
1040 }
1041 }
1042
1043 constexpr void SetLEPart(int part, Part x) {
1044 LEPart(part) = x & PartMask(part);
1045 }
1046
1047 static constexpr Part PartMask(int part) {
1048 return part == parts - 1 ? topPartMask : partMask;
1049 }
1050
1051 constexpr void Clear() {
1052 for (int j{0}; j < parts; ++j) {
1053 part_[j] = 0;
1054 }
1055 }
1056
1057 Part part_[partsWithAlignment]{};
1058};
1059
1060extern template class Integer<8>;
1061extern template class Integer<16>;
1062extern template class Integer<32>;
1063extern template class Integer<64>;
1064using X87IntegerContainer =
1066extern template class Integer<80, isHostLittleEndian, 16, std::uint16_t,
1067 std::uint32_t, 128>;
1068extern template class Integer<128>;
1069} // namespace Fortran::evaluate::value
1070#endif // FORTRAN_EVALUATE_INTEGER_H_
Definition integer.h:65