Skip to content

Instantly share code, notes, and snippets.

@Keith-S-Thompson
Last active October 14, 2015 01:48
Show Gist options
  • Save Keith-S-Thompson/4288661 to your computer and use it in GitHub Desktop.
Save Keith-S-Thompson/4288661 to your computer and use it in GitHub Desktop.

http://stackoverflow.com/questions/13855582/gcc-outputs-very-large-executable-for-mersenne-program

This is a copy of a small C program that prints the largest(?) known Mersenne prime from http://bellard.org/mersenne.html. The file size is slightly larger than the 438 bytes mentioned on the web page, probably because of line breaks.

The original version generates a very large object file because of the large initialized static array.

By removing the initialization and replacing it with an assignment to the 0th element in main, the entire array is no longer allocated in the object file, but the program still produces the same output.

Note that nothing in the C standard says anything about the size of object files. This compile-time behavior is specific to gcc -- and probably to a number of other compilers that behave similarly.

int m=167772161,N=1,t[1<<25],a,*p,i,e=34893349,s,c,U=1;g(d,h){for(i=s;i<1<<
24;i*=2)d=d*1LL*d%m;for(p=t;p<t+N;p+=s)for(i=s,c=1;i;i--)a=p[s]*(h?c:1LL)%m,p[s]
=(m+*p-a)*(h?1LL:c)%m,a+=*p,*p++=a%m,c=c*1LL*d%m;}main(){t[0]=2;while(e/=2){N*=2;U=U*
1LL*(m+1)/2%m;for(s=N;s/=2;)g(17,0);for(p=t;p<t+N;p++)*p=*p*1LL**p%m*U%m;for(s=1
;s<N;s*=2)g(29606852,1);for(a=0,p=t;p<t+N;)a+=*p<<(e&1),*p++=a%10,a/=10;}while(!
*--p);for(t[0]--;p>=t;)putchar(48+*p--);}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment