You could save a register and a TestEqual if you did two jumps (one for x<a, one for x>=b) but otherwise there's not much you can do for a range check. Keep in mind though that SwitchOnSmi does a range check itself (and falls through on failure), so unless you want to exclude a range that's inside your Switch range, you don't need a range check.
Another thing to keep in mind is that SwitchOnSmi expects a Smi value in the accumulator, so you'll need to add a Smi check to the bytecode handler. Thankfully 'switch' comparison semantics are that of strict equality, so afaict you don't need to add a ToPrimitive or anything like that, but I think you might have to add an explicit -0 check before the Smi check. Hope that helps, happy to review when you get a patch together - this has been on my backlog for literal years, so I'm glad to see someone else doing it! - Leszek On Sat, 15 May 2021, 07:03 Mihir Shah, <mihirshah.11...@gmail.com> wrote: > Hi, > > I was working on a jump table implementation for switch statements with > all constant Smi case labels (in the parse step, instead of generating the > traditional if-else-if kind of logic), and needed to do range checking at > the top. > > I was wondering, then, was there a better way to do range checking, i.e. > does value in accumulator register x lie in range (known at compile time) > [a,b]? I think the standard trick of reducing a<=x<=b to 0<=x-a<=b-a and > then doing unsigned comparison here would not work because Smi is signed. > > Because right now my idea of the bytecode is something like this (which > feels very inefficient): > > Load x into accumulator and register r1 ... > TestLessThan [b] > Star [r2] > Ldar [r1] > TestGreaterThan [a] > TestEqual [r2] > JumpIfFalse <after the switch> > Ldar [r1] > > ... proceed with SwitchOnSmi... > > Thank you! > > > -- > -- > v8-dev mailing list > v8-dev@googlegroups.com > http://groups.google.com/group/v8-dev > --- > You received this message because you are subscribed to the Google Groups > "v8-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to v8-dev+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/v8-dev/6df36377-1d02-4de9-aec4-5890003af416n%40googlegroups.com > <https://groups.google.com/d/msgid/v8-dev/6df36377-1d02-4de9-aec4-5890003af416n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- -- v8-dev mailing list v8-dev@googlegroups.com http://groups.google.com/group/v8-dev --- You received this message because you are subscribed to the Google Groups "v8-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/CAGRskv9aQB6Yhacnuk%3DsexpxQ52oDwzEdFAOnoJZxkZRxm%3DqjA%40mail.gmail.com.