[PATCH] Use saturated arithmetic for overflow detection.
Ondřej Bílka
neleai@seznam.cz
Fri Nov 1 13:59:00 GMT 2013
On Wed, Oct 30, 2013 at 12:26:04PM -0700, Paul Eggert wrote:
> On 10/30/2013 12:14 PM, OndÅej BÃlka wrote:
> > These jumps can be predicted almost perfectly as overflows do not happen.
> > Here factors like that original comparison needs to load a large
> > constant in register start play role in evaluation.
>
> All true. Perhaps a benchmark would help clarify this?
> (We can easily measure code bloat, anyway. :-)
I wrote and attached quick benchmark for v2 of patch.
When code is hot a assembly looks fastest but it migth be that
gcc missoptimized loop.
$ time ./assembly
real 0m5.129s
user 0m5.130s
sys 0m0.000s
$ time ./generic
real 0m7.625s
user 0m7.628s
sys 0m0.000s
$ time ./current
real 0m5.961s
user 0m5.964s
sys 0m0.000s
when I use two multiplications in assembly it becomes
real 0m6.916s
user 0m6.919s
sys 0m0.000s
-------------- next part --------------
#include <stdint.h>
#include <stdlib.h>
#include "sysdeps/unix/sysv/linux/x86_64/saturated.h"
int main()
{
volatile size_t z2 = 34;
size_t x,y = 0;
size_t z = z2;
for (x=0;x<3400000000;x++) {
// y += ADD_S(MUL_S(MUL_S(z, 42), 3),8);
y += ADD_S(MUL_S(z, 42* 3),8);
z = (34 * z + 135) % (1 << 30);
}
return y;
}
-------------- next part --------------
#include <stdint.h>
#include <stdlib.h>
#include "sysdeps/generic/saturated.h"
int main()
{
volatile size_t z2 = 34;
size_t x,y = 0;
size_t z = z2;
for (x=0;x<3400000000;x++) {
// y += ADD_S(MUL_S(MUL_S(z, 42), 3),8);
y += ADD_S(MUL_S(z, 42* 3),8);
z = (34 * z + 135) % (1 << 30);
}
return y;
}
-------------- next part --------------
#include <stdint.h>
#include <stdlib.h>
#include "sysdeps/unix/sysv/linux/x86_64/saturated.h"
int main()
{
volatile size_t z2 = 34;
size_t x,y = 0;
size_t z = z2;
for (x=0;x<3400000000;x++) {
y += (z <= ((SIZE_MAX - 8) / 42 / 3)) ? (z*42*3+8) : SIZE_MAX;
z = (34 * z + 135) % (1 << 30);
}
return y;
}
More information about the Libc-alpha
mailing list