[llvm] [ADT, Support] Use std::min and std::max (NFC) (PR #164145)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 19 09:27:00 PDT 2025
https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/164145
>From f0b235a58ebdc138fad440238cbc19c15b4aea2f Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sat, 18 Oct 2025 12:47:49 -0700
Subject: [PATCH 1/2] [ADT, Support] Use std::min and std::max (NFC)
---
llvm/include/llvm/ADT/PagedVector.h | 3 +--
llvm/include/llvm/Support/Format.h | 3 +--
llvm/include/llvm/Support/GraphWriter.h | 2 +-
llvm/lib/Support/APFloat.cpp | 9 +++------
llvm/lib/Support/Unix/Signals.inc | 3 +--
5 files changed, 7 insertions(+), 13 deletions(-)
diff --git a/llvm/include/llvm/ADT/PagedVector.h b/llvm/include/llvm/ADT/PagedVector.h
index 52ecd0bb0ba11..0a691f8889aa0 100644
--- a/llvm/include/llvm/ADT/PagedVector.h
+++ b/llvm/include/llvm/ADT/PagedVector.h
@@ -189,8 +189,7 @@ template <typename T, size_t PageSize = 1024 / sizeof(T)> class PagedVector {
while (ElementIdx < PV->Size &&
!PV->PageToDataPtrs[ElementIdx / PageSize])
ElementIdx += PageSize;
- if (ElementIdx > PV->Size)
- ElementIdx = PV->Size;
+ ElementIdx = std::min(ElementIdx, PV->Size);
}
return *this;
diff --git a/llvm/include/llvm/Support/Format.h b/llvm/include/llvm/Support/Format.h
index b549341a40e7f..67db634eb9d46 100644
--- a/llvm/include/llvm/Support/Format.h
+++ b/llvm/include/llvm/Support/Format.h
@@ -236,8 +236,7 @@ class FormattedBytes {
: Bytes(B), FirstByteOffset(O), IndentLevel(IL), NumPerLine(NPL),
ByteGroupSize(BGS), Upper(U), ASCII(A) {
- if (ByteGroupSize > NumPerLine)
- ByteGroupSize = NumPerLine;
+ ByteGroupSize = std::min<uint32_t>(ByteGroupSize, NumPerLine);
}
};
diff --git a/llvm/include/llvm/Support/GraphWriter.h b/llvm/include/llvm/Support/GraphWriter.h
index a8784edfcf896..3bef75cc7e508 100644
--- a/llvm/include/llvm/Support/GraphWriter.h
+++ b/llvm/include/llvm/Support/GraphWriter.h
@@ -343,7 +343,7 @@ template <typename GraphType, typename Derived> class GraphWriterBase {
const void *DestNodeID, int DestNodePort,
const std::string &Attrs) {
if (SrcNodePort > 64) return; // Eminating from truncated part?
- if (DestNodePort > 64) DestNodePort = 64; // Targeting the truncated part?
+ DestNodePort = std::min(DestNodePort, 64); // Targeting the truncated part?
O << "\tNode" << SrcNodeID;
if (SrcNodePort >= 0)
diff --git a/llvm/lib/Support/APFloat.cpp b/llvm/lib/Support/APFloat.cpp
index b4de79ab14baf..47876042206a1 100644
--- a/llvm/lib/Support/APFloat.cpp
+++ b/llvm/lib/Support/APFloat.cpp
@@ -2600,8 +2600,7 @@ APFloat::opStatus IEEEFloat::convert(const fltSemantics &toSemantics,
int exponentChange = omsb - fromSemantics.precision;
if (exponent + exponentChange < toSemantics.minExponent)
exponentChange = toSemantics.minExponent - exponent;
- if (exponentChange < shift)
- exponentChange = shift;
+ exponentChange = std::max(exponentChange, shift);
if (exponentChange < 0) {
shift -= exponentChange;
exponent += exponentChange;
@@ -3043,8 +3042,7 @@ IEEEFloat::roundSignificandWithExponent(const integerPart *decSigParts,
if (decSig.exponent < semantics->minExponent) {
excessPrecision += (semantics->minExponent - decSig.exponent);
truncatedBits = excessPrecision;
- if (excessPrecision > calcSemantics.precision)
- excessPrecision = calcSemantics.precision;
+ excessPrecision = std::min(excessPrecision, calcSemantics.precision);
}
/* Extra half-ulp lost in reciprocal of exponent. */
powHUerr = (powStatus == opOK && calcLostFraction == lfExactlyZero) ? 0:2;
@@ -3441,8 +3439,7 @@ char *IEEEFloat::convertNormalToHexString(char *dst, unsigned int hexDigits,
/* Convert as much of "part" to hexdigits as we can. */
unsigned int curDigits = integerPartWidth / 4;
- if (curDigits > outputDigits)
- curDigits = outputDigits;
+ curDigits = std::min(curDigits, outputDigits);
dst += partAsHex (dst, part, curDigits, hexDigitChars);
outputDigits -= curDigits;
}
diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc
index 573ad82f2dea4..78d6540db98a7 100644
--- a/llvm/lib/Support/Unix/Signals.inc
+++ b/llvm/lib/Support/Unix/Signals.inc
@@ -868,8 +868,7 @@ void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
nwidth = strlen(name) - 1;
}
- if (nwidth > width)
- width = nwidth;
+ width = std::max(nwidth, width);
}
for (int i = 0; i < depth; ++i) {
>From e70d43aa52e8b6d77f4afbfb27f62f7657412f94 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Sun, 19 Oct 2025 09:26:31 -0700
Subject: [PATCH 2/2] Address a comment.
---
llvm/include/llvm/Support/Format.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/Support/Format.h b/llvm/include/llvm/Support/Format.h
index 67db634eb9d46..b549341a40e7f 100644
--- a/llvm/include/llvm/Support/Format.h
+++ b/llvm/include/llvm/Support/Format.h
@@ -236,7 +236,8 @@ class FormattedBytes {
: Bytes(B), FirstByteOffset(O), IndentLevel(IL), NumPerLine(NPL),
ByteGroupSize(BGS), Upper(U), ASCII(A) {
- ByteGroupSize = std::min<uint32_t>(ByteGroupSize, NumPerLine);
+ if (ByteGroupSize > NumPerLine)
+ ByteGroupSize = NumPerLine;
}
};
More information about the llvm-commits
mailing list