<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 3, 2015 at 11:13 AM, Oscar Benjamin <span dir="ltr"><<a href="mailto:oscar.j.benjamin@gmail.com" target="_blank">oscar.j.benjamin@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span class="">On 2 July 2015 at 18:29, Jason Swails <<a href="mailto:jason.swails@gmail.com">jason.swails@gmail.com</a>> wrote:<br>
><br>
> As others have suggested, this is almost certainly a 32-bit vs. 64-bit<br>
> issue. Consider the following C program:<br>
><br>
> // maths.h<br>
> #include <math.h><br>
> #include <stdio.h><br>
><br>
> int main() {<br>
> double x;<br>
> int i;<br>
> x = 1-pow(0.5, 53);<br>
><br>
> for (i = 1; i < 1000000; i++) {<br>
> if ((int)(i*x) == i) {<br>
> printf("%d\n", i);<br>
> break;<br>
> }<br>
> }<br>
><br>
> return 0;<br>
> }<br>
><br>
> For the most part, this should be as close to an exact transliteration of<br>
> your Python code as possible.<br>
><br>
> Here's what I get when I try compiling and running it on my 64-bit (Gentoo)<br>
> Linux machine with 32-bit compatible libs:<br>
><br>
> swails@batman ~/test $ gcc maths.c<br>
> swails@batman ~/test $ ./a.out<br>
> swails@batman ~/test $ gcc -m32 maths.c<br>
> swails@batman ~/test $ ./a.out<br>
> 2049<br>
<br>
</span>I was unable to reproduce this on my system. In both cases the loops<br>
run to completion. A look at the assembly generated by gcc shows that<br>
something different goes on there though.<br>
<br>
The loop in the 64 bit one (in the main function) looks like:<br>
<br>
$ objdump -d a.out | less<br>
...<br>
400555: pxor %xmm0,%xmm0<br>
400559: cvtsi2sdl -0xc(%rbp),%xmm0<br>
40055e: mulsd -0x8(%rbp),%xmm0<br>
400563: cvttsd2si %xmm0,%eax<br>
400567: cmp -0xc(%rbp),%eax<br>
40056a: jne 400582 <main+0x4c><br>
40056c: mov -0xc(%rbp),%eax<br>
40056f: mov %eax,%esi<br>
400571: mov $0x400624,%edi<br>
400576: mov $0x0,%eax<br>
40057b: callq 400410 <printf@plt><br>
400580: jmp 40058f <main+0x59><br>
400582: addl $0x1,-0xc(%rbp)<br>
400586: cmpl $0xf423f,-0xc(%rbp)<br>
40058d: jle 400555 <main+0x1f><br>
...<br>
<br>
Where is the 32 bit one looks like:<br>
<br>
$ objdump -d a.out.32 | less<br>
...<br>
804843e: fildl -0x14(%ebp)<br>
8048441: fmull -0x10(%ebp)<br>
8048444: fnstcw -0x1a(%ebp)<br>
8048447: movzwl -0x1a(%ebp),%eax<br>
804844b: mov $0xc,%ah<br>
804844d: mov %ax,-0x1c(%ebp)<br>
8048451: fldcw -0x1c(%ebp)<br>
8048454: fistpl -0x20(%ebp)<br>
8048457: fldcw -0x1a(%ebp)<br>
804845a: mov -0x20(%ebp),%eax<br>
804845d: cmp -0x14(%ebp),%eax<br>
8048460: jne 8048477 <main+0x5c><br>
8048462: sub $0x8,%esp<br>
8048465: pushl -0x14(%ebp)<br>
8048468: push $0x8048520<br>
804846d: call 80482f0 <printf@plt><br>
8048472: add $0x10,%esp<br>
8048475: jmp 8048484 <main+0x69><br>
8048477: addl $0x1,-0x14(%ebp)<br>
804847b: cmpl $0xf423f,-0x14(%ebp)<br>
8048482: jle 804843e <main+0x23><br>
...<br>
<br>
So the 64 bit one is using SSE instructions and the 32-bit one is<br>
using x87. That could explain the difference you see at the C level<br>
but I don't see it on this CPU (/proc/cpuinfo says Intel(R) Core(TM)<br>
i5-3427U CPU @ 1.80GHz).<br></blockquote><div><br></div><div><div class="gmail_default" style="color:rgb(0,0,0);display:inline">Hmm. Well that could explain why you don't get the same results as me. My CPU is a </div>AMD FX(tm)-6100 Six-Core Processor<div class="gmail_default" style="color:rgb(0,0,0);display:inline"> (from /proc/cpuinfo). My objdump looks the same as yours for the 64-bit version, but for 32-bit it looks like:</div></div><div><div class="gmail_default" style="color:rgb(0,0,0);display:inline"><br></div></div><div><div class="gmail_default" style="color:rgb(0,0,0);display:inline">...</div></div>
804843a: db 44 24 14 fildl 0x14(%esp)<div class="gmail_default" style="color:rgb(0,0,0);display:inline"></div><br><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 804843e: dc 4c 24 18 fmull 0x18(%esp)</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048442: dd 5c 24 08 fstpl 0x8(%esp)</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048446: f2 0f 2c 44 24 08 cvttsd2si 0x8(%esp),%eax</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 804844c: 3b 44 24 14 cmp 0x14(%esp),%eax</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048450: 75 16 jne 8048468 <main+0x4b></span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048452: 8b 44 24 14 mov 0x14(%esp),%eax</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048456: 89 44 24 04 mov %eax,0x4(%esp)</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 804845a: c7 04 24 10 85 04 08 movl $0x8048510,(%esp)</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048461: e8 8a fe ff ff call 80482f0 <printf@plt></span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048466: eb 0f jmp 8048477 <main+0x5a></span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048468: 83 44 24 14 01 addl $0x1,0x14(%esp)</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 804846d: 81 7c 24 14 3f 42 0f cmpl $0xf423f,0x14(%esp)</span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048474: 00 </span></div><div class="gmail_default" style="color:rgb(0,0,0)"><span style="color:rgb(34,34,34)"> 8048475: 7e c3 jle 804843a <main+0x1d></span></div><div><div class="gmail_default" style="color:rgb(0,0,0);display:inline">...</div><div class="gmail_default" style="color:rgb(0,0,0);display:inline"></div> <br></div><div><br></div><div><div class="gmail_default" style="color:rgb(0,0,0)">However, I have no experience looking at raw assembler, so I can't discern what it is I'm even looking at (nor do I know what explicit SSE instructions look like in assembler).</div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default" style="color:rgb(0,0,0)">I have a Mac that runs an Intel Core i5, and, like you, both 32- and 64-bit versions run to completion. Which is at least consistent with what others are seeing with Python.</div><div class="gmail_default" style="color:rgb(0,0,0)"><br></div><div class="gmail_default" style="color:rgb(0,0,0)">All the best,</div><div class="gmail_default" style="color:rgb(0,0,0)">Jason</div></div></div>
</div></div>