From: Robert A Duff on
Gene <gene.ressler(a)gmail.com> writes:

> In some cases, a built-in Ada construct will generate better code than
> a "hand-coded" equivalent in C++. An example I ran into recently was
> incrementing a modular type. In Ada, you say I := I + 1;, and the
> compiler takes care of "wrapping" to zero. In C++, I've frequently
> seen people write i = (i + 1) % n; to simulate the same operation in
> the absence of modular types.

You will see me writing "I := (I + 1) mod N;" (for signed
integer I) in Ada. ;-)

>...In this case, GCC/C++ generates a div/
> mod instruction, which is very expensive.

That's surprising. It wouldn't be hard to optimize the
"I := (I + 1) mod N;" case (which would apply to both
C and Ada).

And it would be desirable, because in most cases the explicit
"mod" (or "%") is more readable.

- Bob
From: Charles H. Sampson on
Robert A Duff <bobduff(a)shell01.TheWorld.com> wrote:

> Gene <gene.ressler(a)gmail.com> writes:
>
> > In some cases, a built-in Ada construct will generate better code than
> > a "hand-coded" equivalent in C++. An example I ran into recently was
> > incrementing a modular type. In Ada, you say I := I + 1;, and the
> > compiler takes care of "wrapping" to zero. In C++, I've frequently
> > seen people write i = (i + 1) % n; to simulate the same operation in
> > the absence of modular types.
>
> You will see me writing "I := (I + 1) mod N;" (for signed
> integer I) in Ada. ;-)
>
I'm surprised, Bob. Are you saying that you signed integers in
preference to a modular type for a variable that cycles? I use
modular-typed variables and, if I've got my engineer's hat on, write

I := I + 1; -- Modular variable. Wraps.

but I have to admit that in the heat of battle the comment might be
omitted. Even in that case, the variable name probably indicates that
wrapping should be expected.

Charlie
--
All the world's a stage, and most
of us are desperately unrehearsed. Sean O'Casey
From: Dmitry A. Kazakov on
On Sun, 8 Aug 2010 10:13:51 -0700, Charles H. Sampson wrote:

> Robert A Duff <bobduff(a)shell01.TheWorld.com> wrote:
>
>> Gene <gene.ressler(a)gmail.com> writes:
>>
>>> In some cases, a built-in Ada construct will generate better code than
>>> a "hand-coded" equivalent in C++. An example I ran into recently was
>>> incrementing a modular type. In Ada, you say I := I + 1;, and the
>>> compiler takes care of "wrapping" to zero. In C++, I've frequently
>>> seen people write i = (i + 1) % n; to simulate the same operation in
>>> the absence of modular types.
>>
>> You will see me writing "I := (I + 1) mod N;" (for signed
>> integer I) in Ada. ;-)
>>
> I'm surprised, Bob. Are you saying that you signed integers in
> preference to a modular type for a variable that cycles?

Sometimes it has to be that way because modular types do not support
non-static bounds. E.g.:

procedure Foo (N : Positive) is
type M is mod 2**N; -- You cannot do that!

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
From: Robert A Duff on
csampson(a)inetworld.net (Charles H. Sampson) writes:

> Robert A Duff <bobduff(a)shell01.TheWorld.com> wrote:
>
> I'm surprised, Bob. Are you saying that you signed integers in
> preference to a modular type for a variable that cycles?

Yes. Unless I'm forced to use modular for some other reason
(e.g. I need one extra bit).

>...I use
> modular-typed variables and, if I've got my engineer's hat on, write
>
> I := I + 1; -- Modular variable. Wraps.

You're not alone. Even Tucker has advocated using modular
types for this sort of thing.

But I think an explicit "mod N" is clearer than a comment.

Variables that cycle are rare, so should be noted explicitly
in the code. And, as Dmitry noted, modular types only work
when the lower bound is 0. It's not unreasonable to have
a circular buffer indexed by a range 1..N.

See my point? Still "surprised"?

> but I have to admit that in the heat of battle the comment might be
> omitted. Even in that case, the variable name probably indicates that
> wrapping should be expected.

- Bob

P.S. Can anybody recall the definition of "not" on modular
types, when the modulus is not a power of 2, without
looking it up? Hint: It makes no sense. The only feature
that's worse than "modular types" is "modular types with a
non-power-of-2 modulus". ;-)
From: (see below) on
On 08/08/2010 21:51, in article wcchbj4j09y.fsf(a)shell01.TheWorld.com,
"Robert A Duff" <bobduff(a)shell01.TheWorld.com> wrote:

> csampson(a)inetworld.net (Charles H. Sampson) writes:
>
>> Robert A Duff <bobduff(a)shell01.TheWorld.com> wrote:
>>
>> I'm surprised, Bob. Are you saying that you signed integers in
>> preference to a modular type for a variable that cycles?
>
> Yes. Unless I'm forced to use modular for some other reason
> (e.g. I need one extra bit).
>
>> ...I use
>> modular-typed variables and, if I've got my engineer's hat on, write
>>
>> I := I + 1; -- Modular variable. Wraps.
>
> You're not alone. Even Tucker has advocated using modular
> types for this sort of thing.
>
> But I think an explicit "mod N" is clearer than a comment.
>
> Variables that cycle are rare, so should be noted explicitly
> in the code. And, as Dmitry noted, modular types only work
> when the lower bound is 0. It's not unreasonable to have
> a circular buffer indexed by a range 1..N.
>
> See my point? Still "surprised"?
>
>> but I have to admit that in the heat of battle the comment might be
>> omitted. Even in that case, the variable name probably indicates that
>> wrapping should be expected.
>
> - Bob
>
> P.S. Can anybody recall the definition of "not" on modular
> types, when the modulus is not a power of 2, without
> looking it up? Hint: It makes no sense. The only feature
> that's worse than "modular types" is "modular types with a
> non-power-of-2 modulus". ;-)

I don't use non-power-of-2 mod types, but I write emulators, and I would be
stuffed if I could not declare, e.g.:

type KDF9_word is mod 2**48;

--
Bill Findlay
<surname><forename> chez blueyonder.co.uk