<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div style="word-wrap:break-word">
<div class="x_BodyFragment"><font size="2"><span style="font-size:10pt">
<div class="x_PlainText"><br>
Your bigint library has a bug when compiled on a mac using the -O2 flag with Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn). In particular, Line 115 of BigInteger::convertToSignedPrimitive()
<br>
<br>
X x = -X(b);<br>
<br>
doesn’t behave as expected when compiled at the -O2 level.<br>
<br>
Compile the attached C++ sample on a mac using clang/llvm to see exactly what goes wrong.<br>
<br>
<br>
I fixed this bug and added 64-bit tests. You will find the fixes/improvements here:
<a href="https://github.com/kurmasz/bigint">https://github.com/kurmasz/bigint</a><br>
<br>
Zack Kurmas<br>
</div>
</span></font></div>
<div class="x_BodyFragment"><font size="2"><span style="font-size:10pt">
<div class="x_PlainText"><br>
-- <br>
Zachary Kurmas<br>
Assistant Professor of Computer Science<br>
Grand Valley State University<br>
<a href="http://www.cis.gvsu.edu/~kurmasz">http://www.cis.gvsu.edu/~kurmasz</a><br>
<br>
<br>
——<br>
<br>
#include <iostream><br>
<br>
template<class Out> Out negate(unsigned long in) {<br>
// This line casts an unsigned long to a signed long.<br>
// 2^63 as an unsigned long is 0x8000000000000000<br>
// Casting 2^63 from unsigned long to signed long should<br>
// produce -2^63 (which is also 0x8000000000000000).<br>
// The twos complement of -2^63 just happens to be -2^63, <br>
// so, (long)(2^63) should always be -2^63, correct?<br>
<br>
// This code works as expected when compiled with gcc 4.4.7; but,<br>
// when compiled on a mac with -O2 using LLVM 5.1 / clang-503.0.40,<br>
// it produces the wrong answer. (The source of the problem is<br>
// clearly visible in the generated assembly code.)<br>
<br>
Out out = -Out(in); <br>
if (out > 0) {<br>
std::cout << "Oops!" << std::endl;<br>
} else {<br>
std::cout << "Correct." << std::endl;<br>
}<br>
<br>
return out;<br>
}<br>
<br>
<br>
int main() {<br>
unsigned long in = 9223372036854775808UL;<br>
<br>
std::cout << "Attempt 3: " << std::endl;<br>
negate<long>(in);<br>
}<br>
<br>
</div>
</span></font></div>
</div>
<div style="word-wrap:break-word"></div>
</body>
</html>