[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [gpsd-dev] Warnings on 32 bit Fedora 24

From: Greg Troxel
Subject: Re: [gpsd-dev] Warnings on 32 bit Fedora 24
Date: Fri, 22 Jul 2016 19:24:17 -0400
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/24.5 (berkeley-unix)

"Gary E. Miller" <address@hidden> writes:

>> -DHAVE_SYS_TIMEPPS_H=1 bits.c bits.c: In function 'ubits':
>> bits.c:40:19: warning: left shift of negative value
>> [-Wshift-negative-value] fld &= ~(-1LL << width);

I think the issue is that if you were to go read and understand the C99
standard, it would say that the left shift operator is only defined for
unsigned or positive values.

So the fix is probably to first figure out what it's supposed to be
doing and write it without invoking undefined behavior.

As I read that, if width is 8, you'd get

  0xffffffff (from the -1LL)
  0xffffff00 (from the <<)
  0x000000ff (from the ~

at least on platforms where long long is still 32 bits.  (I would expect
code to be written for fixed-width types, not native types anyway, but I
haven't looked at this.)  Basically "-1LL" is trying to be a constant
with all the bits set, rather than a negative number, and is assuming
that the machine uses 2s complement arithmetic, which while true on all
computers I can remember using, is probably not actually required by
the C99 standard.

So, I would rewrite this as

  fld &=  (1 << width) - 1

except that this won't work for width 32.   So instead this could be

  fld &=  ~(~0ULL << width)

I would definitely add an assert that the new/old ways are equal and run
tests with it present before changing.  This is tricky business!

Attachment: signature.asc
Description: PGP signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]