|
|
Subscribe / Log in / New account

Zig 2024 roadmap

Zig 2024 roadmap

Posted Feb 5, 2024 14:53 UTC (Mon) by pbonzini (subscriber, #60935)
In reply to: Zig 2024 roadmap by khim
Parent article: Zig 2024 roadmap

I never quite understood why shifts of negative values are still an issue now that C23 mandates twos complement representation. They should be changed to be equal to x*2^n for left shift, and x/2^n rounded towards negative infinity for right shift.


to post comments

Zig 2024 roadmap

Posted Feb 5, 2024 15:01 UTC (Mon) by farnz (subscriber, #17727) [Link] (1 responses)

That then makes x >> y and x << y a multiple instruction sequence, not a single instruction sequence. On AArch64, for example, LSL x1, x2, x3 is defined as "take the bottom 6 bits of x3, shift x2 left by that amount"; this ignores the sign bit completely, and to implement the behaviour you're suggesting, I'd have to check the sign bit of x3, then choose whether to do an LSL, LSR, or ASR instruction based on signedness of x2's current contents and sign bit of x3.

Zig 2024 roadmap

Posted Feb 5, 2024 15:17 UTC (Mon) by khim (subscriber, #9252) [Link]

You are talking about right argument, pbonzini talks about left one.

Zig 2024 roadmap

Posted Feb 5, 2024 15:18 UTC (Mon) by khim (subscriber, #9252) [Link] (4 responses)

P1236R1 changed definition to precisely what you say in C++20.

Are you sure C23 haven't picked up that change?

Zig 2024 roadmap

Posted Feb 5, 2024 15:24 UTC (Mon) by pbonzini (subscriber, #60935) [Link]

Oh, I would be happy to be wrong!

Zig 2024 roadmap

Posted Feb 6, 2024 9:02 UTC (Tue) by pbonzini (subscriber, #60935) [Link] (2 responses)

Nope:

> 6.5.7 Bitwise shift operators
>
> [...] 4 The result of E1 << E2 is E1 left-shifted E2 bit positions [...] if E1 has a signed type and nonnegative value, and E1 * 2^E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined
>
> 5 The result of E1 >> E2 is E1 right-shifted E2 bit positions [...] If E1 has a signed type and a negative value, the resulting value is implementation-defined

Zig 2024 roadmap

Posted Feb 6, 2024 10:13 UTC (Tue) by khim (subscriber, #9252) [Link] (1 responses)

> Nope:

Ugh. I think someone should propose to fix it, then. C++ have finally removed “undefined behavior” from there (crazy values still trigger undefined behavior when E2, the right operand is “strange”, but anything is accepted as E1, the left operand — starting from C++20), thus in practice compilers already have code to handle everything properly

Zig 2024 roadmap

Posted Feb 6, 2024 15:25 UTC (Tue) by pbonzini (subscriber, #60935) [Link]

GCC and clang have promised for over 10 years to not use that latitude (not sure if left shift overflow is undefined, but anyway negative numbers can be shifted left and right with no other worries). So yeah it should be a no brainer.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds