|
Boost : |
From: Guillaume Melquiond (guillaume.melquiond_at_[hidden])
Date: 2007-09-09 02:33:48
Le samedi 08 septembre 2007 à 23:10 -0400, Daryle Walker a écrit :
> Maybe we should add a header to our detail (and/or pending) folder
> encapsulating the full versions of built-in integer arithmetic
> operations, that most(?) processors have, but neither C nor C++ let us
> access. I'm talking about functions like:
>
> std::pair<unsigned, bool> full_add( unsigned a1, unsigned a2 );
>
> The processor has a "carry" flag whenever an integer addition overflows,
> but we can't access it (without assembly-language hacks).
Is it really worth it? Nowadays, compilers are smart enough. They
generate better code when details are not obfuscated by hand-crafted
assembly code. I tested the following code with GCC:
#include <algorithm>
std::pair<unsigned, bool> full_add(unsigned a, unsigned b)
{ return std::make_pair(a + b, a + b < a); }
bool no_overflow(unsigned a, unsigned b)
{ return !full_add(a, b).second; }
The no_overflow function is a bit dumb, but it is here to check that the
compiler was able to perform optimizations. This is the generated code
on x86-64:
_Z11no_overflowjj:
addl %edi, %esi
setae %al
ret
This is the optimal assembly code. And there is no way to obtain this
code if you use an assembly hack in the full_add function.
Best regards,
Guillaume
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk