Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #123628

Re: Some kind of contextual modulo operator?

Subject Re: Some kind of contextual modulo operator?
Newsgroups comp.lang.c
References <e10414e1-e674-4a1c-96e7-39e598cc4dc9@googlegroups.com> <8baec7c1-5c5e-4391-b011-9317ff220752@googlegroups.com> <XNDTB.102542$au1.26875@fx45.am4> <w5ETB.84939$bN1.53680@fx14.am4> <c73af526-10fe-46c3-a98b-ec3345ec9238@googlegroups.com>
From bartc <bc@freeuk.com>
Message-ID <DiHTB.65770$7d.19343@fx05.am4> (permalink)
Organization virginmedia.com
Date 2017-11-29 23:22 +0000

Show all headers | View raw


On 29/11/2017 20:33, jameskuyper@verizon.net wrote:
> On Wednesday, November 29, 2017 at 2:43:36 PM UTC-5, Bart wrote:
>> On 29/11/2017 19:22, bartc wrote:
>>> On 29/11/2017 19:12, jameskuyper@verizon.net wrote:
>>>> On Wednesday, November 29, 2017 at 1:41:43 PM UTC-5, Rick C. Hodgin
>>>> wrote:
>>>>> Is there some natural way in C to do this (without a macro):
>>>>>
>>>>>       // Round up to the next largest 16-byte border
>>>>>       char *p = malloc((some_value + 16) & ~0xf);
>>>>
>>>> 16*((some_value+15)/16)
>>>
>>> This gives different results (see below). Which is right depends on
>>> whether you want a value that is already a multiple of 16 to be rounded
>>> up. I think usually you don't.
>>>
>>> -------------------------
>>>
>>> for (i=0; i<33; ++i) {
>>>       printf("%d %d %d\n",i,(i+16)&~0x0F, 16*((i+15)/16));
>>> }
>>
>> Your formula behaves strangely for negative values.
> 
> It probably seems strange because you have an incorrect understanding of what it
> should do. In  C99 and later, integer division truncates toward 0, so
> 16*((-10+15)/16 => 16*(5/16) => 16*0 = 0. 16*((-20+15)/16) => 16*((-5)/16) =>
> 16*0 => 0. In C90 or earlier, it was implementation-defined whether (-5)/16 had
> a value of -1 or 0.
> 
> You insist on using compilers in their default mode, in which they don't conform
> to any particular version of the C standard, and then you insist on discussing
> their behavior here,

I'm not quite sure why you bring that up.

I tested the code (as above but using a range for i including negative 
values) using gcc in default mode, but also gcc using -std=c11, gcc 
-std=c89, gcc -std=c99, pelles c, lccwin, dmc, tiny c and (my compiler) mcc.

All gave the same results. I'd only tested with mcc and gcc/default at 
the time (to confirm the results of mine), and I made these extra tests 
now because your disparaging comment suggested it might behave differently.

The behaviour is strange because it IS, in results changing only after 
31 results instead of every 16, in that region just below 0. Nothing to 
do with what Bart understands or doesn't understand.


  rather than a forum devoted to the language that they
> actually implement. For instance, in default mode, gcc implements GnuC, which
> should properly be discussed in a gcc forum, not here.

Rubbish.

> I've no idea how GnuC
> handles integer division involving negative values.

The OP didn't say what flavour of C this was supposed to be. So both 
types of rounding are viable. It would be extraordinary if GnuC has a 
rounding method that rounds neither up, nor down, but does something 
different entirely.

So GnuC is going to round up or down like every other compiler. And 
since the rest of gcc is obliged to round towards zero since C99, and 
since gcc implements C99 and C11, and since the result of C89 rounding 
is implementation defined so it could choose what it does, the money is 
on it rounding towards zero.

> Since the calculated value will be passed to malloc, I assumed that the
> calculated value was not intended to be negative. Since the result would be
> converted to size_t, malloc() itself wouldn't have any troubles with a negative
> value, but I find it hard to a imagine a context where the result of that
> conversion for negative values would be the intended behavior of such code.

I can easily imagine a use for rounding such negative numbers. Try 
allocating offsets for variables within a stack frame, where usually the 
offsets are negative, and ensuring each variable is properly aligned. 
(Although usually you would be rounding downwards there.)

That in the example given, the result was passed to malloc(), could have 
been just an illustration.

-- 
bartc

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-29 10:41 -0800
  Re: Some kind of contextual modulo operator? jameskuyper@verizon.net - 2017-11-29 11:12 -0800
    Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-11-29 19:22 +0000
      Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-11-29 19:43 +0000
        Re: Some kind of contextual modulo operator? jameskuyper@verizon.net - 2017-11-29 12:33 -0800
          Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-11-29 23:22 +0000
            Re: Some kind of contextual modulo operator? jameskuyper@verizon.net - 2017-11-29 20:53 -0800
              Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-11-30 12:17 +0000
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-01 15:08 +0000
                Re: Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-12-01 07:31 -0800
                Re: Some kind of contextual modulo operator? scott@slp53.sl.home (Scott Lurndal) - 2017-12-01 16:32 +0000
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-01 17:07 +0000
                Re: Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-12-01 09:15 -0800
                Re: Some kind of contextual modulo operator? scott@slp53.sl.home (Scott Lurndal) - 2017-12-01 18:14 +0000
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-01 19:03 +0000
                Re: Some kind of contextual modulo operator? scott@slp53.sl.home (Scott Lurndal) - 2017-12-01 20:54 +0000
                Re: Some kind of contextual modulo operator? Keith Thompson <kst-u@mib.org> - 2017-12-01 12:09 -0800
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-01 21:56 +0000
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-01 23:11 +0000
                Re: Some kind of contextual modulo operator? supercat@casperkitty.com - 2017-12-01 15:16 -0800
                Re: Some kind of contextual modulo operator? David Brown <david.brown@hesbynett.no> - 2017-12-02 17:09 +0100
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-02 19:42 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-03 09:16 +1300
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-02 22:06 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-03 11:20 +1300
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-03 00:01 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-03 13:55 +1300
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-03 02:14 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-03 16:13 +1300
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-03 12:28 +0000
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-03 14:26 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-04 10:49 +1300
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-04 10:38 +1300
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-03 23:00 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-04 12:10 +1300
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-03 23:33 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-04 13:18 +1300
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-04 12:19 +0000
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-05 07:58 +1300
                Re: Some kind of contextual modulo operator? David Brown <david.brown@hesbynett.no> - 2017-12-04 00:30 +0100
                Packed structs (was Re: Some kind of contextual modulo operator?) scott@slp53.sl.home (Scott Lurndal) - 2017-12-04 15:02 +0000
                Re: Packed structs (was Re: Some kind of contextual modulo operator?) David Brown <david.brown@hesbynett.no> - 2017-12-04 16:34 +0100
                Re: Packed structs (was Re: Some kind of contextual modulo operator?) Keith Thompson <kst-u@mib.org> - 2017-12-04 09:23 -0800
                Re: Packed structs (was Re: Some kind of contextual modulo operator?) scott@slp53.sl.home (Scott Lurndal) - 2017-12-04 18:31 +0000
                Re: Packed structs (was Re: Some kind of contextual modulo operator?) supercat@casperkitty.com - 2017-12-04 15:37 -0800
                Re: Some kind of contextual modulo operator? supercat@casperkitty.com - 2017-12-04 08:39 -0800
                Re: Some kind of contextual modulo operator? "Chris M. Thomasson" <invalid_chris_thomasson@invalid.invalid> - 2017-12-03 18:13 -0800
                Re: Some kind of contextual modulo operator? Ian Collins <ian-news@hotmail.com> - 2017-12-03 14:50 +1300
                Re: Some kind of contextual modulo operator? scott@slp53.sl.home (Scott Lurndal) - 2017-12-04 15:00 +0000
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-04 15:53 +0000
                Re: Some kind of contextual modulo operator? scott@slp53.sl.home (Scott Lurndal) - 2017-12-04 18:26 +0000
                Re: Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-12-04 10:34 -0800
                Re: Some kind of contextual modulo operator? David Brown <david.brown@hesbynett.no> - 2017-12-03 19:40 +0100
                Re: Some kind of contextual modulo operator? David Kleinecke <dkleinecke@gmail.com> - 2017-12-03 11:35 -0800
                Re: Some kind of contextual modulo operator? scott@slp53.sl.home (Scott Lurndal) - 2017-12-04 14:59 +0000
                Re: Some kind of contextual modulo operator? Keith Thompson <kst-u@mib.org> - 2017-12-01 08:56 -0800
                Re: Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-12-01 08:58 -0800
                Re: Some kind of contextual modulo operator? scott@slp53.sl.home (Scott Lurndal) - 2017-12-01 18:16 +0000
                Re: Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-12-01 10:28 -0800
                Re: Some kind of contextual modulo operator? David Brown <david.brown@hesbynett.no> - 2017-12-01 20:19 +0100
                Re: Some kind of contextual modulo operator? Gareth Owen <gwowen@gmail.com> - 2017-12-01 22:22 +0000
                Re: Some kind of contextual modulo operator? jameskuyper@verizon.net - 2017-12-01 07:49 -0800
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-01 16:54 +0000
                Re: Some kind of contextual modulo operator? jameskuyper@verizon.net - 2017-12-01 09:34 -0800
                Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-12-01 18:48 +0000
                Re: Some kind of contextual modulo operator? jameskuyper@verizon.net - 2017-12-01 11:27 -0800
      Re: Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-29 12:17 -0800
        Re: Some kind of contextual modulo operator? bartc <bc@freeuk.com> - 2017-11-30 12:27 +0000
          Re: Some kind of contextual modulo operator? "Rick C. Hodgin" <rick.c.hodgin@gmail.com> - 2017-11-30 06:11 -0800
    Re: Some kind of contextual modulo operator? "James R. Kuyper" <jameskuyper@verizon.net> - 2017-11-29 15:02 -0500
  Some kind of contextual modulo operator? mcheung63@gmail.com - 2017-12-08 23:20 -0800

csiph-web