Merge pull request #10404 from ethereum/ewasm-eq-simplify

[ewasm] Change eq() in polyfill to branch-less version
This commit is contained in:
chriseth 2020-11-30 11:54:45 +01:00 committed by GitHub
commit 75b1624180
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 71 additions and 55 deletions

View File

@ -90,9 +90,9 @@ function mul_64x64_128(x, y) -> hi, lo {
// value split into four 64 bit values.
function mul_128x128_256(x1, x2, y1, y2) -> r1, r2, r3, r4 {
let ah, al := mul_64x64_128(x1, y1)
let bh, bl := mul_64x64_128(x1, y2)
let ch, cl := mul_64x64_128(x2, y1)
let dh, dl := mul_64x64_128(x2, y2)
let bh, bl := mul_64x64_128(x1, y2)
let ch, cl := mul_64x64_128(x2, y1)
let dh, dl := mul_64x64_128(x2, y2)
r4 := dl
let carry1, carry2
let t1, t2

View File

@ -35,15 +35,18 @@ function iszero512(x1, x2, x3, x4, x5, x6, x7, x8) -> r:i32 {
}
function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4 {
if i64.eq(x1, y1) {
if i64.eq(x2, y2) {
if i64.eq(x3, y3) {
if i64.eq(x4, y4) {
r4 := 1
}
}
}
}
r4 := i64.extend_i32_u(
i32.and(
i64.eq(x1, y1),
i32.and(
i64.eq(x2, y2),
i32.and(
i64.eq(x3, y3),
i64.eq(x4, y4)
)
)
)
)
}
// returns 0 if a == b, -1 if a < b and 1 if a > b

View File

@ -31,10 +31,9 @@ object "object" {
let x_6 := x_2
let x_7 := x_3
let _2 := 1
let _3 := i64.or(_1, _1)
let _4:i32 := i32.eqz(i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, _2)))))
let _3:i32 := i32.eqz(i32.eqz(i64.eqz(i64.or(i64.or(_1, _1), i64.or(_1, _2)))))
for { }
i32.eqz(_4)
i32.eqz(_3)
{
let x_8, x_9, x_10, x_11 := add(x_4, x_5, x_6, x_7, _1, _1, _1, _2)
x_4 := x_8
@ -43,10 +42,12 @@ object "object" {
x_7 := x_11
}
{
let _5, _6, _7, _8 := iszero_197_919_1528(_1, _1, _1, lt_199(x_4, x_5, x_6, x_7, _1, _1, _1, 10))
if i32.eqz(i64.eqz(i64.or(i64.or(_5, _6), i64.or(_7, _8)))) { break }
if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 2))))) { break }
if i32.eqz(i64.eqz(i64.or(_3, i64.or(_1, eq(x_4, x_5, x_6, x_7, _1, _1, _1, 4))))) { continue }
let _4, _5, _6, _7 := iszero_200_868_1493(_1, _1, _1, lt_202(x_4, x_5, x_6, x_7, _1, _1, _1, 10))
if i32.eqz(i64.eqz(i64.or(i64.or(_4, _5), i64.or(_6, _7)))) { break }
let _8, _9, _10, _11 := eq_201_869_1494(x_4, x_5, x_6, x_7, _1, _1, _1, 2)
if i32.eqz(i64.eqz(i64.or(i64.or(_8, _9), i64.or(_10, _11)))) { break }
let _12, _13, _14, _15 := eq_201_869_1494(x_4, x_5, x_6, x_7, _1, _1, _1, 4)
if i32.eqz(i64.eqz(i64.or(i64.or(_12, _13), i64.or(_14, _15)))) { continue }
}
sstore(_1, _1, _1, _1, x_4, x_5, x_6, x_7)
}
@ -67,19 +68,13 @@ object "object" {
let r1_1, carry_2 := add_carry(x1, y1, carry_1)
r1 := r1_1
}
function iszero_197_919_1528(x1, x2, x3, x4) -> r1, r2, r3, r4
function iszero_200_868_1493(x1, x2, x3, x4) -> r1, r2, r3, r4
{
r4 := i64.extend_i32_u(i64.eqz(i64.or(i64.or(x1, x2), i64.or(x3, x4))))
}
function eq(x1, x2, x3, x4, y1, y2, y3, y4) -> r4
function eq_201_869_1494(x1, x2, x3, x4, y1, y2, y3, y4) -> r1, r2, r3, r4
{
if i64.eq(x1, y1)
{
if i64.eq(x2, y2)
{
if i64.eq(x3, y3) { if i64.eq(x4, y4) { r4 := 1 } }
}
}
r4 := i64.extend_i32_u(i32.and(i64.eq(x1, y1), i32.and(i64.eq(x2, y2), i32.and(i64.eq(x3, y3), i64.eq(x4, y4)))))
}
function cmp(a, b) -> r:i32
{
@ -87,7 +82,7 @@ object "object" {
case 1:i32 { r := 0xffffffff:i32 }
default { r := i64.ne(a, b) }
}
function lt_199(x1, x2, x3, x4, y1, y2, y3, y4) -> z4
function lt_202(x1, x2, x3, x4, y1, y2, y3, y4) -> z4
{
let z:i32 := false
switch cmp(x1, y1)
@ -178,7 +173,7 @@ object "object" {
Binary representation:
0061736d0100000001540c6000006000017f60017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60047e7e7e7e017f60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f00025e0408657468657265756d0c73746f7261676553746f7265000a08657468657265756d06726576657274000a08657468657265756d0f67657443616c6c4461746153697a65000108657468657265756d0c63616c6c44617461436f7079000b030f0e000408050803080602020205090705030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00040aa1090ea302030b7e017f087e02404200210002402000200020002000100f21012300210223012103230221040b20012105200221062003210720042108420121092000200084210a200a200020098484504545210b02400340200b45450d01024002402000200020002005200620072008200020002000420a100a1007210c2300210d2301210e2302210f0b200c200d84200e200f8484504504400c030b200a20002005200620072008200020002000420210088484504504400c030b200a20002005200620072008200020002000420410088484504504400c010b0b024020052006200720082000200020002009100621102300211123012112230221130b201021052011210620122107201321080c000b0b2000200020002000200520062007200810110b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1005210d2300210e0b200d210a024020012005200e1005210f230021100b200f2109024020002004201010052111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2d01017e024020002004510440200120055104402002200651044020032007510440420121080b0b0b0b0b20080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041009210a200a41004604400240200120051009210b200b41004604400240200220061009210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100c421086210220022000421088100c8421010b20010b1e01027e02402000100d422086210220022000422088100d8421010b20010bfc0105047e017f017e087f047e024010022108420021092009200920092009100b210a2000200120022003100b210b2009200920094220100b210c200b417f200c6b4b04404100410010010b2008200b6b210d200b20084b04404100210d0b4100210e200d200e4b0440200a200b200d10030b200c200d4b0440200c200d6b210f200a200d6a2110200e2111024003402011200f49450d010240201020116a200e3a00000b201141016a21110c000b0b0b200e290000100e2112200e41086a290000100e2113200e41106a290000100e2114200e41186a290000100e2115201221042013210520142106201521070b20052400200624012007240220040b3200024020002001100e370000200041086a2002100e370000200041106a2003100e370000200041186a2004100e3700000b0b230002404100200020012002200310104120200420052006200710104100412010000b0b
0061736d0100000001540c6000006000017f60017e017e60027e7e017f60037e7e7e017e60047e7e7e7e017e60047e7e7e7e017f60087e7e7e7e7e7e7e7e0060087e7e7e7e7e7e7e7e017e60057f7e7e7e7e0060027f7f0060037f7f7f00025e0408657468657265756d0c73746f7261676553746f7265000a08657468657265756d06726576657274000a08657468657265756d0f67657443616c6c4461746153697a65000108657468657265756d0c63616c6c44617461436f7079000b030f0e000408050803080602020205090705030100010610037e0142000b7e0142000b7e0142000b071102066d656d6f72790200046d61696e00040acb090ecb02030a7e017f107e02404200210002402000200020002000100f21012300210223012103230221040b20012105200221062003210720042108420121092000200084200020098484504545210a02400340200a45450d01024002402000200020002005200620072008200020002000420a100a1007210b2300210c2301210d2302210e0b200b200c84200d200e8484504504400c030b0240200520062007200820002000200042021008210f2300211023012111230221120b200f201084201120128484504504400c030b024020052006200720082000200020004204100821132300211423012115230221160b2013201484201520168484504504400c010b0b0240200520062007200820002000200020091006211723002118230121192302211a0b201721052018210620192107201a21080c000b0b2000200020002000200520062007200810110b0b2901037e0240200020017c2105200520027c21032005200054200320055472ad21040b2004240020030b6c010b7e0240200320077c210c200c42007c210b024020022006200c200354200b200c5472ad1005210d2300210e0b200d210a024020012005200e1005210f230021100b200f2109024020002004201010052111230021120b201121080b20092400200a2401200b240220080b2401047e0240200020018420022003848450ad21070b20052400200624012007240220040b2f01047e02402000200451200120055120022006512003200751717171ad210b0b20092400200a2401200b240220080b2701027f024002402000200154210320034101460440417f210205200020015221020b0b0b20020b8a0102017e047f0240410021090240200020041009210a200a41004604400240200120051009210b200b41004604400240200220061009210c200c41004604402003200754210905200c41014604404100210905410121090b0b0b05200b41014604404100210905410121090b0b0b05200a41014604404100210905410121090b0b0b2009ad21080b20080b2901017f024042002000200184200284520440000b42002003422088520440000b2003a721040b20040b1f01017e024020004208864280fe0383200042088842ff01838421010b20010b1e01027e02402000100c421086210220022000421088100c8421010b20010b1e01027e02402000100d422086210220022000422088100d8421010b20010bfc0105047e017f017e087f047e024010022108420021092009200920092009100b210a2000200120022003100b210b2009200920094220100b210c200b417f200c6b4b04404100410010010b2008200b6b210d200b20084b04404100210d0b4100210e200d200e4b0440200a200b200d10030b200c200d4b0440200c200d6b210f200a200d6a2110200e2111024003402011200f49450d010240201020116a200e3a00000b201141016a21110c000b0b0b200e290000100e2112200e41086a290000100e2113200e41106a290000100e2114200e41186a290000100e2115201221042013210520142106201521070b20052400200624012007240220040b3200024020002001100e370000200041086a2002100e370000200041106a2003100e370000200041186a2004100e3700000b0b230002404100200020012002200310104120200420052006200710104100412010000b0b
Text representation:
(module
@ -203,12 +198,19 @@ Text representation:
(local $x_6 i64)
(local $x_7 i64)
(local $_2 i64)
(local $_3 i64)
(local $_4 i32)
(local $_3 i32)
(local $_4 i64)
(local $_5 i64)
(local $_6 i64)
(local $_7 i64)
(local $_8 i64)
(local $_9 i64)
(local $_10 i64)
(local $_11 i64)
(local $_12 i64)
(local $_13 i64)
(local $_14 i64)
(local $_15 i64)
(local $x_8 i64)
(local $x_9 i64)
(local $x_10 i64)
@ -227,26 +229,39 @@ Text representation:
(local.set $x_6 (local.get $x_2))
(local.set $x_7 (local.get $x_3))
(local.set $_2 (i64.const 1))
(local.set $_3 (i64.or (local.get $_1) (local.get $_1)))
(local.set $_4 (i32.eqz (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (local.get $_2)))))))
(local.set $_3 (i32.eqz (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_1) (local.get $_1)) (i64.or (local.get $_1) (local.get $_2)))))))
(block $label__3
(loop $label__5
(br_if $label__3 (i32.eqz (i32.eqz (local.get $_4))))
(br_if $label__3 (i32.eqz (i32.eqz (local.get $_3))))
(block $label__4
(block
(local.set $_5 (call $iszero_197_919_1528 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_199 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10))))
(local.set $_6 (global.get $global_))
(local.set $_7 (global.get $global__1))
(local.set $_8 (global.get $global__2))
(local.set $_4 (call $iszero_200_868_1493 (local.get $_1) (local.get $_1) (local.get $_1) (call $lt_202 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 10))))
(local.set $_5 (global.get $global_))
(local.set $_6 (global.get $global__1))
(local.set $_7 (global.get $global__2))
)
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_5) (local.get $_6)) (i64.or (local.get $_7) (local.get $_8))))) (then
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_4) (local.get $_5)) (i64.or (local.get $_6) (local.get $_7))))) (then
(br $label__3)
))
(if (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 2)))))) (then
(block
(local.set $_8 (call $eq_201_869_1494 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 2)))
(local.set $_9 (global.get $global_))
(local.set $_10 (global.get $global__1))
(local.set $_11 (global.get $global__2))
)
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_8) (local.get $_9)) (i64.or (local.get $_10) (local.get $_11))))) (then
(br $label__3)
))
(if (i32.eqz (i64.eqz (i64.or (local.get $_3) (i64.or (local.get $_1) (call $eq (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 4)))))) (then
(block
(local.set $_12 (call $eq_201_869_1494 (local.get $x_4) (local.get $x_5) (local.get $x_6) (local.get $x_7) (local.get $_1) (local.get $_1) (local.get $_1) (i64.const 4)))
(local.set $_13 (global.get $global_))
(local.set $_14 (global.get $global__1))
(local.set $_15 (global.get $global__2))
)
(if (i32.eqz (i64.eqz (i64.or (i64.or (local.get $_12) (local.get $_13)) (i64.or (local.get $_14) (local.get $_15))))) (then
(br $label__4)
))
@ -338,7 +353,7 @@ Text representation:
(local.get $r1)
)
(func $iszero_197_919_1528
(func $iszero_200_868_1493
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)
@ -358,7 +373,7 @@ Text representation:
(local.get $r1)
)
(func $eq
(func $eq_201_869_1494
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)
@ -368,20 +383,18 @@ Text representation:
(param $y3 i64)
(param $y4 i64)
(result i64)
(local $r1 i64)
(local $r2 i64)
(local $r3 i64)
(local $r4 i64)
(block $label__9
(if (i64.eq (local.get $x1) (local.get $y1)) (then
(if (i64.eq (local.get $x2) (local.get $y2)) (then
(if (i64.eq (local.get $x3) (local.get $y3)) (then
(if (i64.eq (local.get $x4) (local.get $y4)) (then
(local.set $r4 (i64.const 1))
))
))
))
))
(local.set $r4 (i64.extend_i32_u (i32.and (i64.eq (local.get $x1) (local.get $y1)) (i32.and (i64.eq (local.get $x2) (local.get $y2)) (i32.and (i64.eq (local.get $x3) (local.get $y3)) (i64.eq (local.get $x4) (local.get $y4)))))))
)
(local.get $r4)
(global.set $global_ (local.get $r2))
(global.set $global__1 (local.get $r3))
(global.set $global__2 (local.get $r4))
(local.get $r1)
)
(func $cmp
@ -405,7 +418,7 @@ Text representation:
(local.get $r)
)
(func $lt_199
(func $lt_202
(param $x1 i64)
(param $x2 i64)
(param $x3 i64)