From: Bakul Shah <bakul+plan9@bitblocks.com>
To: Fans of the OS Plan 9 from Bell Labs <9fans@9fans.net>
Subject: Re: [9fans] Integer arithmetic: some lessons
Date: Wed, 21 Apr 2010 16:28:52 -0700 [thread overview]
Message-ID: <20100421232852.5AB865B4A@mail.bitblocks.com> (raw)
In-Reply-To: Your message of "Wed, 21 Apr 2010 15:40:19 +0200." <20100421134019.GA1382@polynum.com>
On Wed, 21 Apr 2010 15:40:19 +0200 tlaronde@polynum.com wrote:
> Hello,
>
> Still about integer arithmetic.
...
> Conclusion (apparently): gcc always translate div involving power of two
> to binary manipulations, while (apparently) ken-cc does not.
gcc may choose to implement / with appropriate shifts and
what not but the result must be equivalent. It certainly
can't replace %2 with >>1 for negative numbers.
Try this:
int f(int x) { return x / 2; }
int g(int x) { return x >> 1; }
main(int argc, char** argv)
{
int x = argc > 1? atoi(argv[1]) : -7;
printf("%d %d\n", -7/2, -7>>1);
printf("%d %d\n", f(-7), g(-7));
printf("%d %d\n", f(x), g(x));
}
This will give you
-3 -4
-3 -4
-3 -4
I don't have a way to test what kencc does but I would be
surprised if the result is any different....
>> is an arithmetic shift for signed numbers (the sign bit is
replicated). The clearest way to see this is to compare
-1/2 and -1>>1
The first gives you 0. The second gives you -1 (on twos
complement machines).
IIRC, Pascal's div operator behaves the same as C's / for
integers. May be the problem is somehow related to mod?
> Conclusion: I will have to replace in METAFONT all div involving power
> of two to binary operations, since if I replace in some places and not
> in others, I wreak havoc the algorithms since computations are not done
> the same way for combined chunks.
Replacing div 2^N of a signed number by >>N seems like a mistake.
next prev parent reply other threads:[~2010-04-21 23:28 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-04-21 13:40 tlaronde
2010-04-21 15:03 ` geoff
2010-04-21 20:19 ` tlaronde
2010-04-21 23:28 ` Bakul Shah [this message]
2010-04-22 5:51 ` tlaronde
2010-04-22 9:42 ` Steve Simon
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20100421232852.5AB865B4A@mail.bitblocks.com \
--to=bakul+plan9@bitblocks.com \
--cc=9fans@9fans.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).