From a702c419a64fb208317cc009ca0f75aecfe09bf9 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Thu, 21 May 2026 11:23:52 +0200 Subject: [PATCH 1/2] Add bitwise assembly helpers --- std/std.solc | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/std/std.solc b/std/std.solc index b97115e88..1e6f0752a 100644 --- a/std/std.solc +++ b/std/std.solc @@ -49,6 +49,12 @@ export { bytes, bytes4(*), bytes32(*), + bandWord, + borWord, + bxorWord, + bnotWord, + bshlWord, + bshrWord, calldata(*), concatLit, eqWord, @@ -411,6 +417,60 @@ function subWord(l: word, r: word) -> word { return rw; } +// Bitwise AND +function bandWord(x: word, y: word) -> word { + let res: word; + assembly { + res := and(x, y) + } + return res; +} + +// Bitwise OR +function borWord(x: word) -> word { + let res: word; + assembly { + res := or(x) + } + return res; +} + +// Bitwise XOR +function bxorWord(x: word) -> word { + let res: word; + assembly { + res := xor(x) + } + return res; +} + +// Bitwise NOT +function bnotWord(x: word) -> word { + let res: word; + assembly { + res := not(x) + } + return res; +} + +// Bitwise SHL +function bshlWord(x: word, y: word) -> word { + let res: word; + assembly { + res := shl(x, y) + } + return res; +} + +// Bitwise SHR +function bshrWord(x: word, y: word) -> word { + let res: word; + assembly { + res := shr(x, y) + } + return res; +} + instance word:Eq { function eq(x:word, y:word) -> bool { return eqWord(x, y); From 7112358fe0fd43060660d44e55b61ef34d47157c Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 22 May 2026 16:06:22 +0200 Subject: [PATCH 2/2] Use new helpers --- std/dispatch.solc | 7 ++----- std/std.solc | 4 +--- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/std/dispatch.solc b/std/dispatch.solc index bd83901c5..473f4fedc 100644 --- a/std/dispatch.solc +++ b/std/dispatch.solc @@ -82,11 +82,8 @@ forall name payability args rets fn => instance Method(name,payability,args,rets,fn):Selector { function compute(prx : Proxy(Method(name,payability,args,rets,fn))) -> bytes4 { // let hash : word = keccakLit(sigStr(prx)); - let hash = keccakLit(sigStr(Proxy:Proxy(name)) + "(" + sigStr(Proxy:Proxy(args)) + ")"); - let res : word; - assembly { res := shr(224, hash) } - - return bytes4(res); + let hash = keccakLit(sigStr(Proxy:Proxy(name)) + "(" + sigStr(Proxy:Proxy(args)) + ")"); + return bytes4(bshrWord(224, hash)); } } diff --git a/std/std.solc b/std/std.solc index 1e6f0752a..453bafb88 100644 --- a/std/std.solc +++ b/std/std.solc @@ -1183,9 +1183,7 @@ instance bool:ABIEncode { } function round_up_to_mul_of_32(value:word) -> word { - let result : word; - assembly { result := and(add(value, 31), not(31)) } - return result; + return bandWord(value + 31, bnotWord(31)); } function encodeIntoFromBytesLike(srcPtr:word, basePtr:word, offset:word, tail:word) -> word {