<div dir="ltr"><div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Tue, Sep 18, 2018 at 12:13 AM Eric Blake <<a href="mailto:eblake@redhat.com">eblake@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 9/17/18 3:39 PM, Nir Soffer wrote:<br>
<br>
>> +#define IS_ALIGNED(size, align) ({              \<br>
>> +      assert (is_power_of_2 ((align)));         \<br>
>> +      !((size) & ((align) - 1));                \<br>
>> +})<br>
>><br>
> <br>
> But this version will happily accept singed int, and I think this code<br>
> behavior with signed int is undefined.<br>
<br>
Well, sort of. Bit shifts are very likely to hit undefined behavior on <br>
ints. But & and | are not shifts.<br>
<br>
> <br>
> See<br>
> <a href="https://wiki.sei.cmu.edu/confluence/display/c/INT13-C.+Use+bitwise+operators+only+on+unsigned+operands" rel="noreferrer" target="_blank">https://wiki.sei.cmu.edu/confluence/display/c/INT13-C.+Use+bitwise+operators+only+on+unsigned+operands</a><br>
> <br>
> Why use a macro when we can use a function that is likely to be inlined<br>
> anyway?<br>
> This is not used in tight loops, so there is no reason to use a macro.<br>
> <br>
> If you want to use a macro, see the kernel align macros:<br>
<br>
Alas, the kernel has a more restrictive license (GPLv2 in general); <br>
which means we can't copy it into nbdkit.  (True, the definitions will <br>
look similar, but if we claim we copied from somewhere, that source had <br>
better be permissively licensed).<br></blockquote><div><br></div><div>The FreeBSD version:</div><div><pre style="font-size:12px;font-family:Courier,"Courier New",monospace;color:rgb(0,0,0)">#define <a href="http://fxr.watson.org/fxr/ident?i=IS_ALIGNED">IS_ALIGNED</a>(<a href="http://fxr.watson.org/fxr/ident?i=n">n</a>,align)     (!((uint32_t)(<a href="http://fxr.watson.org/fxr/ident?i=n">n</a>) & (align - 1)))</pre></div><div><a href="http://fxr.watson.org/fxr/source/contrib/ncsw/inc/ncsw_ext.h#L182">http://fxr.watson.org/fxr/source/contrib/ncsw/inc/ncsw_ext.h#L182</a></div><div><br></div><div>Nir</div></div></div></div>