[llvm] [Support] Simplify the continuation condition in encodeSLEB128 (NFC) (PR #165651)
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Wed Oct 29 22:33:14 PDT 2025
https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/165651
The boolean expression to determine if more bytes are needed for a
signed LEB128 value is quite complex:
!((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
((Value == -1) && ((Byte & 0x40) != 0))))
This patch simplifies it to an equivalent expression using a ternary
operator, which is much easier to understand.
>From add7310f5b444585fa6db15a7490cebcaf0d63d3 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Mon, 27 Oct 2025 09:56:00 -0700
Subject: [PATCH] [Support] Simplify the continuation condition in
encodeSLEB128 (NFC)
The boolean expression to determine if more bytes are needed for a
signed LEB128 value is quite complex:
!((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
((Value == -1) && ((Byte & 0x40) != 0))))
This patch simplifies it to an equivalent expression using a ternary
operator, which is much easier to understand.
---
llvm/include/llvm/Support/LEB128.h | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/llvm/include/llvm/Support/LEB128.h b/llvm/include/llvm/Support/LEB128.h
index 898b4ea1f19ab..4e2262fb15c56 100644
--- a/llvm/include/llvm/Support/LEB128.h
+++ b/llvm/include/llvm/Support/LEB128.h
@@ -29,8 +29,7 @@ inline unsigned encodeSLEB128(int64_t Value, raw_ostream &OS,
uint8_t Byte = Value & 0x7f;
// NOTE: this assumes that this signed shift is an arithmetic right shift.
Value >>= 7;
- More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
- ((Value == -1) && ((Byte & 0x40) != 0))));
+ More = Value != ((Byte & 0x40) ? -1 : 0);
Count++;
if (More || Count < PadTo)
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
@@ -58,8 +57,7 @@ inline unsigned encodeSLEB128(int64_t Value, uint8_t *p, unsigned PadTo = 0) {
uint8_t Byte = Value & 0x7f;
// NOTE: this assumes that this signed shift is an arithmetic right shift.
Value >>= 7;
- More = !((((Value == 0 ) && ((Byte & 0x40) == 0)) ||
- ((Value == -1) && ((Byte & 0x40) != 0))));
+ More = Value != ((Byte & 0x40) ? -1 : 0);
Count++;
if (More || Count < PadTo)
Byte |= 0x80; // Mark this byte to show that more bytes will follow.
More information about the llvm-commits
mailing list