Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #126641
| From | jacobnavia <jacob@jacob.remcomp.fr> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: The bug |
| Date | 2018-02-13 10:25 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <p5uaug$orp$1@dont-email.me> (permalink) |
| References | <p5ignt$45g$1@dont-email.me> |
Le 08/02/2018 à 22:50, jacobnavia a écrit :
> Consider this program:
> 1 #include <string.h>
> 2 #include <stdio.h>
> 3 #define MANTISSA_LENGTH 7
> 4 typedef struct tagQfloatAccum {
> 5 int sign;
> 6 unsigned int exponent;
> 7 unsigned long long mantissa[MANTISSA_LENGTH+8];
> 8 } QfloatAccum;
> 9 typedef QfloatAccum * QfloatAccump;
> 10 typedef unsigned long long QELT;
> 11 static void square(QfloatAccump a,QfloatAccump b,int prec )
> 12 {
> 13 QELT *p, *q, *r;
> 14 unsigned __int128 u, lp;
> 15 int k;
> 16
> 17 memset(b->mantissa,0,sizeof(b->mantissa));
> 18 r = &b->mantissa[prec+2];
> 19 for( k=prec+2; k>=2; k-- ) {
> 20 q = &a->mantissa[1];
> 21 p = &a->mantissa[k-1];
> 22 while( p >= q ) {
> 23 lp = ((__int128)(*p)) * (*q);
> 24 if( p != q ) {
> 25 if( ((lp >> 127) & 1 ) )
> 26 *(r-2) += 1;
> 27 lp <<= 1;
> 28 }
> 29 u = lp&0xffffffffffffffffULL;;
> 30 u += *r;
> 31 (*r) = (u&0xffffffffffffffffULL);
> 32 u = u >> 64;
> 33 u += (lp >> 64);
> 34 u += *(r-1);
> 35 *(r-1) = u;
> 36 *(r-2) += (u >> 64);
> 37 p--;
> 38 q++;
> 39 }
> 40 --r;
> 41 }
> 42 }
> 43
> 44 QfloatAccum a =
> 45 {0x0, 0x0, {0x0, 0x5c551d94ae0bf85d, 0xdf43ff68348e9f44,
> 0x75abbd546eb4ad2c , 0x45928b3668d09923, 0xef0e21fbaa8bb66b,
> 46 0xd834f79a27f3d70b, 0xe29f8b325d2f09bc, 0x22342f2796cceefe,
> 0x15136c1f 974cb8e4, 0x0, 0x0, 0x0, 0x0, 0x0}};
> 47 QfloatAccum b = {0};
> 48 QfloatAccum result =
> 49 {0x0, 0x0, {0x0, 0x214d498f853f343e, 0xd13fda08f3fdf79d,
> 0x6066a345e0141842 ,
> 50 0x8db2e50b963ec962, 0x703ffb7a840af142, 0xfebe1ffca06b368,
> 0xe705569e6ab 50956, 0x8c5227bc21e99054, 0x641cbb8833747c1a,
> 51 0xd32c84d233a9e32a, 0x12d004571d8dad0a, 0x0, 0x0, 0x0}};
> 52 int main(void)
> 53 {
> 54 square(&a,&b,9);
> 55 if (memcmp(&result,&b,sizeof(QfloatAccum))) {
> 56 printf("bug!\n");
> 57 }
> 58 }
> ---------------------------------------------------------------------------
> Compiled without any optimizations, this program outputs nothing. Gcc
> with -O2 will print "bug".
>
> Why is this happening?
>
I found the root cause: gcc/clang will assume that there is no "UB" i.e.
no overflow. Then, a multiplication of two unsigned values into a signed
result can't be negative.
Then, the compilers wrongly assume, the bit 127 (the sign bit) can never
be set. Then, the test at line 25 can never yioeld anything other than
zero and the code in line 27 is dead code that is eliminated.
This chain of false reasoning starts with the assumption: Overflows
never exist.
Great.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
The bug jacobnavia <jacob@jacob.remcomp.fr> - 2018-02-08 22:50 +0100
Re: The bug Keith Thompson <kst-u@mib.org> - 2018-02-08 14:15 -0800
Re: The bug jacobnavia <jacob@jacob.remcomp.fr> - 2018-02-13 10:25 +0100
Re: The bug David Brown <david.brown@hesbynett.no> - 2018-02-13 10:51 +0100
Re: The bug jacobnavia <jacob@jacob.remcomp.fr> - 2018-02-13 11:19 +0100
Re: The bug David Brown <david.brown@hesbynett.no> - 2018-02-13 12:23 +0100
Re: The bug Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2018-02-13 08:19 -0800
Re: The bug David Brown <david.brown@hesbynett.no> - 2018-02-15 10:52 +0100
Re: The bug Noob <root@127.0.0.1> - 2018-02-13 16:19 +0100
Re: The bug Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-19 12:11 -0800
Re: The bug supercat@casperkitty.com - 2018-02-19 16:31 -0800
Re: The bug Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-21 23:03 -0800
Re: The bug supercat@casperkitty.com - 2018-02-23 16:40 -0800
Re: The bug Richard Damon <Richard@Damon-Family.org> - 2018-02-24 05:57 -0500
Re: The bug Tim Rentsch <txr@alumni.caltech.edu> - 2018-02-24 13:11 -0800
Re: The bug Anton Shepelev <anton.txt@gmail.com> - 2018-02-25 00:15 +0300
Re: The bug Keith Thompson <kst-u@mib.org> - 2018-02-13 11:44 -0800
Re: The bug asetofsymbols@gmail.com - 2018-02-13 12:13 -0800
Re: The bug Noob <root@127.0.0.1> - 2018-02-15 09:45 +0100
csiph-web