Skip to content

Conversation

@kazutakahirata
Copy link
Contributor

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.

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.
@llvmbot
Copy link
Member

llvmbot commented Oct 30, 2025

@llvm/pr-subscribers-llvm-support

Author: Kazu Hirata (kazutakahirata)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/165651.diff

1 Files Affected:

  • (modified) llvm/include/llvm/Support/LEB128.h (+2-4)
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.

@kazutakahirata kazutakahirata merged commit 9f64d75 into llvm:main Oct 30, 2025
12 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_20251029_Supprot_LEB128_simplify branch October 30, 2025 19:05
luciechoi pushed a commit to luciechoi/llvm-project that referenced this pull request Nov 1, 2025
…lvm#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.
DEBADRIBASAK pushed a commit to DEBADRIBASAK/llvm-project that referenced this pull request Nov 3, 2025
…lvm#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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants