@@ -32,64 +32,73 @@ namespace Fortran::common {
32
32
// AddOverflow and MulOverflow are copied from
33
33
// llvm/include/llvm/Support/MathExtras.h and specialised to int64_t.
34
34
35
+ // __has_builtin is not defined in some compilers. Make sure it is defined.
36
+ #ifndef __has_builtin
37
+ #define __has_builtin (x ) 0
38
+ #endif
39
+
35
40
// / Add two signed integers, computing the two's complement truncated result,
36
41
// / returning true if overflow occurred.
37
- static inline bool AddOverflow (int64_t X, int64_t Y, int64_t &Result) {
42
+ static inline bool AddOverflow (
43
+ std::int64_t x, std::int64_t y, std::int64_t &result) {
38
44
#if __has_builtin(__builtin_add_overflow)
39
- return __builtin_add_overflow (X, Y , &Result );
45
+ return __builtin_add_overflow (x, y , &result );
40
46
#else
41
47
// Perform the unsigned addition.
42
- const uint64_t UX = static_cast <uint64_t >(X) ;
43
- const uint64_t UY = static_cast <uint64_t >(Y) ;
44
- const uint64_t UResult = UX + UY ;
48
+ const std:: uint64_t ux{ static_cast <std:: uint64_t >(x)} ;
49
+ const std:: uint64_t uy{ static_cast <std:: uint64_t >(y)} ;
50
+ const std:: uint64_t uresult{ux + uy} ;
45
51
46
52
// Convert to signed.
47
- Result = static_cast <int64_t >(UResult );
53
+ result = static_cast <std:: int64_t >(uresult );
48
54
49
55
// Adding two positive numbers should result in a positive number.
50
- if (X > 0 && Y > 0 ) {
51
- return Result <= 0 ;
56
+ if (x > 0 && y > 0 ) {
57
+ return result <= 0 ;
52
58
}
53
59
// Adding two negatives should result in a negative number.
54
- if (X < 0 && Y < 0 ) {
55
- return Result >= 0 ;
60
+ if (x < 0 && y < 0 ) {
61
+ return result >= 0 ;
56
62
}
57
63
return false ;
58
64
#endif
59
65
}
60
66
61
67
// / Multiply two signed integers, computing the two's complement truncated
62
68
// / result, returning true if an overflow occurred.
63
- static inline bool MulOverflow (int64_t X, int64_t Y, int64_t &Result) {
69
+ static inline bool MulOverflow (
70
+ std::int64_t x, std::int64_t y, std::int64_t &result) {
64
71
#if __has_builtin(__builtin_mul_overflow)
65
- return __builtin_mul_overflow (X, Y , &Result );
72
+ return __builtin_mul_overflow (x, y , &result );
66
73
#else
67
74
// Perform the unsigned multiplication on absolute values.
68
- const uint64_t UX =
69
- X < 0 ? ( 0 - static_cast < uint64_t >(X)) : static_cast <uint64_t >(X) ;
70
- const uint64_t UY =
71
- Y < 0 ? ( 0 - static_cast < uint64_t >(Y)) : static_cast <uint64_t >(Y) ;
72
- const uint64_t UResult = UX * UY ;
75
+ const std:: uint64_t ux{x < 0 ? ( 0 - static_cast <std:: uint64_t >(x))
76
+ : static_cast <std:: uint64_t >(x)} ;
77
+ const std:: uint64_t uy{y < 0 ? ( 0 - static_cast <std:: uint64_t >(y))
78
+ : static_cast <std:: uint64_t >(y)} ;
79
+ const std:: uint64_t uresult{ux * uy} ;
73
80
74
81
// Convert to signed.
75
- const bool IsNegative = (X < 0 ) ^ (Y < 0 );
76
- Result = IsNegative ? (0 - UResult ) : UResult ;
82
+ const bool isNegative = (x < 0 ) ^ (y < 0 );
83
+ result = isNegative ? (0 - uresult ) : uresult ;
77
84
78
85
// If any of the args was 0, result is 0 and no overflow occurs.
79
- if (UX == 0 || UY == 0 ) {
86
+ if (ux == 0 || uy == 0 ) {
80
87
return false ;
81
88
}
82
89
83
- // UX and UY are in [1, 2^n], where n is the number of digits.
90
+ // ux and uy are in [1, 2^n], where n is the number of digits.
84
91
// Check how the max allowed absolute value (2^n for negative, 2^(n-1) for
85
92
// positive) divided by an argument compares to the other.
86
- if (IsNegative) {
87
- return UX > (static_cast <uint64_t >(std::numeric_limits<int64_t >::max ()) +
88
- uint64_t (1 )) /
89
- UY;
93
+ if (isNegative) {
94
+ return ux >
95
+ (static_cast <std::uint64_t >(std::numeric_limits<std::int64_t >::max ()) +
96
+ std::uint64_t {1 }) /
97
+ uy;
90
98
} else {
91
- return UX >
92
- (static_cast <uint64_t >(std::numeric_limits<int64_t >::max ())) / UY;
99
+ return ux >
100
+ (static_cast <std::uint64_t >(std::numeric_limits<std::int64_t >::max ())) /
101
+ uy;
93
102
}
94
103
#endif
95
104
}
@@ -206,10 +215,10 @@ template <typename CHAR = char> class FormatValidator {
206
215
Token token_{}; // current token
207
216
Token knrToken_{}; // k, n, or r UnsignedInteger token
208
217
Token scaleFactorToken_{}; // most recent scale factor token P
209
- int64_t integerValue_{-1 }; // value of UnsignedInteger token
210
- int64_t knrValue_{-1 }; // -1 ==> not present
211
- int64_t scaleFactorValue_{}; // signed k in kP
212
- int64_t wValue_{-1 };
218
+ std:: int64_t integerValue_{-1 }; // value of UnsignedInteger token
219
+ std:: int64_t knrValue_{-1 }; // -1 ==> not present
220
+ std:: int64_t scaleFactorValue_{}; // signed k in kP
221
+ std:: int64_t wValue_{-1 };
213
222
char argString_[3 ]{}; // 1-2 character msg arg; usually edit descriptor name
214
223
bool formatHasErrors_{false };
215
224
bool unterminatedFormatError_{false };
@@ -280,18 +289,18 @@ template <typename CHAR> void FormatValidator<CHAR>::NextToken() {
280
289
case ' 7' :
281
290
case ' 8' :
282
291
case ' 9' : {
283
- const CHAR *lastCursor;
292
+ const CHAR *lastCursor{} ;
284
293
integerValue_ = 0 ;
285
294
bool overflow{false };
286
295
do {
287
296
lastCursor = cursor_;
288
297
if (!overflow) {
289
- overflow =
290
- MulOverflow ( static_cast <int64_t >(10 ), integerValue_, integerValue_);
298
+ overflow = MulOverflow (
299
+ static_cast <std:: int64_t >(10 ), integerValue_, integerValue_);
291
300
}
292
301
if (!overflow) {
293
302
overflow = AddOverflow (
294
- integerValue_, static_cast <int64_t >(c - ' 0' ), integerValue_);
303
+ integerValue_, static_cast <std:: int64_t >(c - ' 0' ), integerValue_);
295
304
}
296
305
c = NextChar ();
297
306
} while (c >= ' 0' && c <= ' 9' );
0 commit comments