tinycc-devel
[Top][All Lists]
Advanced

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

Re: [Tinycc-devel] [PATCH] tccgen.c: Optimise 0<<x, 0>>x, -1>>x, x&0, x*


From: Vincent Lefevre
Subject: Re: [Tinycc-devel] [PATCH] tccgen.c: Optimise 0<<x, 0>>x, -1>>x, x&0, x*0, x|-1, x%1.
Date: Sat, 7 Mar 2015 00:57:13 +0100
User-agent: Mutt/1.5.23-6425-vl-r76280 (2015-03-04)

On 2015-03-07 00:54:13 +0300, Sergey Korshunoff wrote:
> 2015-03-06 12:48 GMT+03:00, Edmund Grimley Evans
> <address@hidden>:
> > To explain a bit better what I mean: On a particular architecture I
> > would like each of the following programs to do the same thing when
> > invoked with no arguments, which isn't required by any standard that I
> > know of, but it's nice to have and easy to achieve.
> >
> > #include <stdio.h>
> > int main(int argc, char *argv[])
> > {
> >   int a = argc >> 1 & 127;
> >   int b = argc >> 8 & 127
> >   printf("%d\n", a / b);
> >   return 0;
> > }
> >
> > #include <stdio.h>
> > int main(int argc, char *argv[])
> > {
> >   int a = argc >> 1 & 127;
> >   int b = argc >> 8 & 127
> >   printf("%d\n", a / 0);
> >   return 0;
> > }
> >
> > #include <stdio.h>
> > int main(int argc, char *argv[])
> > {
> >   int a = argc >> 1 & 127;
> >   int b = argc >> 8 & 127;
> >   printf("%d\n", 0 / 0);
> >   return 0;
> > }
> >
> > What those programs do does, of course, depend on the architecture.
> 
> gcc gives a warning at compilation time about dividing by zero :-)

Only when the divisor zero is known at compile time.

> PS: your solution with memset() is more correct.

Yes, in case there are padding bits or the integer representation is
not two's completement (so that zero can have 2 representations).

> A C standard do not say anything about realtion betwean sizeof(int)
> and any other integer size.

It requires that long long can represent all values of the int type.
So, the only problem with size would be when you have padding bits.

However even in such a case, the memset() solution is correct because:
"For any integer type, the object representation where all the bits
are zero shall be a representation of the value zero in that type."
(6.2.6.2/5).

-- 
Vincent Lefèvre <address@hidden> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)



reply via email to

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