cvs commit: src/sys/kern sched_ule.c
M. Warner Losh
imp at bsdimp.com
Sun Mar 18 02:56:02 UTC 2007
In message: <20070317121427.H560 at 10.0.0.1>
Jeff Roberson <jroberson at chesapeake.net> writes:
: On Sat, 17 Mar 2007, Max Laier wrote:
: > On Saturday 17 March 2007 20:09, Jeff Roberson wrote:
: >> Any language lawyers care to comment on this?
: > I find this strange. According to the spec "(Decrementing is equivalent
: > to subtracting 1.)", but "pri = --pri % RQ_NQS;" will behave like you
: > expect, while "pri = (pri - 1) % RQ_NQS;" clearly didn't.
: I noticed this as well.
: When you do --pri, pri is promoted to int for the math and then demoted
: back to char wich truncates the value. Subsequently this value is used
: in the % operation, which gives the expected results.
: When you do pri - 1 the intermediate result is promoted to a signed int
: which doesn't yield the result you'd like when you mod with 64.
(pri - 1u) % 64
is likely what you want. That way the (pri - 1u) turns out to be
unsigned because it is (unsigned - unsigned) % signed. which winds up
being unsigned % signed. The - 1 version is (unsigned - signed) %
signed, which breaks down to signed % signed, which yields the strange
result that you saw.
(unsigned char)(pri - 1) % 64 is another way to fry this fish.
More information about the cvs-src