This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: [PATCH v2.0] Use saturated arithmetic for overflow detection.
- From: OndÅej BÃlka <neleai at seznam dot cz>
- To: Paul Eggert <eggert at cs dot ucla dot edu>
- Cc: libc-alpha at sourceware dot org
- Date: Sun, 3 Nov 2013 10:15:40 +0100
- Subject: Re: [PATCH v2.0] Use saturated arithmetic for overflow detection.
- Authentication-results: sourceware.org; auth=none
- References: <Pine dot LNX dot 4 dot 64 dot 1310301749400 dot 22878 at digraph dot polyomino dot org dot uk> <20131030183318 dot GA18706 at domone dot podge> <20131101133126 dot GA2546 at domone dot podge> <5273E29D dot 90000 at cs dot ucla dot edu> <20131101175802 dot GA5471 at domone dot podge> <527412A1 dot 8080707 at cs dot ucla dot edu> <20131101215358 dot GA7000 at domone dot podge> <527442F2 dot 3040705 at cs dot ucla dot edu> <20131102092509 dot GA8809 at domone dot podge> <5276073E dot 90108 at cs dot ucla dot edu>
On Sun, Nov 03, 2013 at 01:20:14AM -0700, Paul Eggert wrote:
> OndÅej BÃlka wrote:
> > checking takes 10 bytes for branch version, 12 bytes in branchfree
> > version.
>
> When I try it, the branch version takes one byte more than
> the branchfree version. This is when I do branching (or branchfree)
> for both adding and multiplying. Here's the testcase. Compiled
> with -O3, foo_branch is 32 bytes and foo_branchfree 31.
>
I was referring to version with a sbb trick to decrease size which I
mentioned but did not send new version.
This also improves code size a bit because for addition we do not
require specific registers.
For multiplication we must use rdx:rax pair due to instruction
semantics.
#include <stdint.h>
#include <stdlib.h>
static inline size_t
add_sat_branch (size_t x, size_t y)
{
size_t ret;
asm ("add %2, %0; jnc 1f; sbb %0, %0; 1:" : "=r" (ret) : "0" (x) , "r" (y));
return ret;
}
static inline size_t
add_sat_branchfree (size_t x, size_t y)
{
size_t ret, scratch;
asm ("add %1, %0; sbb %1, %1; or %1, %0" : "=r" (ret), "=r" (scratch) : "0" (x) , "1" (y));
return ret;
}
static inline size_t
mul_sat_branch (size_t x, size_t y)
{
size_t ret, scratch;
asm ("mul %%rdx; jno 1f; sbb %%rax, %%rax; 1:" : "=a" (ret), "=d" (scratch) : "a" (x) , "d" (y));
return ret;
}
static inline size_t
mul_sat_branchfree (size_t x, size_t y)
{
size_t ret;
size_t scratch;
asm ("mul %%rdx; sbb %%rdx, %%rdx; or %%rdx, %%rax" : "=a" (ret), "=d" (scratch) : "a" (x) , "d" (y));
return ret;
}
size_t
foo_branch (size_t a)
{
return add_sat_branch (mul_sat_branch (a, 42 * 3), 8);
}
size_t
foo_branchfree (size_t a)
{
return add_sat_branchfree (mul_sat_branchfree (a, 42 * 3), 8);
}