Why don't you test it and find out yourself?

On Tue, Mar 11, 2025 at 10:20 AM Sỹ Trần Dũng <sytranc...@gmail.com> wrote:

> I have a question regarding V8's compiler optimization, specifically
> concerning the modulo 2 operation. In compilers like GCC and Clang, it's
> common to see the operation n % 2 optimized to a bitwise AND (n & 1) or a
> bit check instruction, as these are generally more efficient.
>
> I've been examining the bytecode generated by V8, and I've observed that a
> modulo instruction is used for n % 2.
>
> [generated bytecode for function: my_mod (0x3de244c5b401
> <SharedFunctionInfo my_mod>)]
> Bytecode length: 17
> Parameter count 2
> Register count 1
> Frame size 8
>    23 S> 0x32c69b60dc80 @    0 : 0b 03             Ldar a0
>    29 E> 0x32c69b60dc82 @    2 : 4b 02 00          ModSmi [2], [0]
>          0x32c69b60dc85 @    5 : c9                Star0
>          0x32c69b60dc86 @    6 : 0d 01             LdaSmi [1]
>    33 E> 0x32c69b60dc88 @    8 : 6f f9 01          TestEqual r0, [1]
>          0x32c69b60dc8b @   11 : 9e 04             JumpIfFalse [4]
> (0x32c69b60dc8f @ 15)
>    45 S> 0x32c69b60dc8d @   13 : 11                LdaTrue
>    57 S> 0x32c69b60dc8e @   14 : ae                Return
>    64 S> 0x32c69b60dc8f @   15 : 12                LdaFalse
>    77 S> 0x32c69b60dc90 @   16 : ae                Return
>
> I'm curious if this behavior changes when the code is "heated" and
> optimized by Turbofan.
>
> Could someone please tell whether Turbofan performs this particular
> optimization?
>
> Thank you for your time and expertise.
>
> --
>

-- 
-- 
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 visit 
https://groups.google.com/d/msgid/v8-dev/CAKSzg3RMAwVP34-A2yRPsg3zDjkM1AMCfouQi%3DsPWmaSeKALpg%40mail.gmail.com.

Reply via email to