lkml.org 
[lkml]   [2019]   [Apr]   [9]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
SubjectRe: [PATCH v1 bitops] bitops: Fix UBSAN undefined behavior warning for rotation right
From
Date
On 09/04/2019 10.08, Rasmus Villemoes wrote:

> one could do
>
> u32 ror32(u32 x, unsigned s)
> {
> return (x >> (s&31)) | (x << ((32-s)&31));
> }
>
> to make the shifts always well-defined and also work as expected for s
> >= 32... if only gcc recognized that the masking is redundant, so that
> its "that's a ror" pattern detection could kick in. Unfortunately, it
> seems that the above generates
>
> 0: 89 f1 mov %esi,%ecx
> 2: 89 f8 mov %edi,%eax
> 4: f7 d9 neg %ecx
> 6: d3 e0 shl %cl,%eax
> 8: 89 f1 mov %esi,%ecx
> a: d3 ef shr %cl,%edi
> c: 09 f8 or %edi,%eax
> e: c3 retq
>
> while without the masking one gets
>
> 10: 89 f8 mov %edi,%eax
> 12: 89 f1 mov %esi,%ecx
> 14: d3 c8 ror %cl,%eax
> 16: c3 retq

Ah, but that's with an ancient gcc 7. With gcc 8, the above pattern is
recognized and generates good code, while eliminating UB. I was about to
file a gcc bug, but found
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82498 .

Rasmus

\
 
 \ /
  Last update: 2019-04-09 10:59    [W:0.085 / U:1.012 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site