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: Paul Eggert <eggert at cs dot ucla dot edu>
- To: OndÅej BÃlka <neleai at seznam dot cz>
- Cc: libc-alpha at sourceware dot org
- Date: Sun, 03 Nov 2013 01:20:14 -0700
- Subject: Re: [PATCH v2.0] Use saturated arithmetic for overflow detection.
- Authentication-results: sourceware.org; auth=none
- References: <20131030174502 dot GA18107 at domone dot podge> <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>
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.
#include <stdint.h>
#include <stdlib.h>
static inline size_t
add_sat_branch (size_t x, size_t y)
{
size_t ret;
asm ("add %%rdx, %%rax; jno 1f; xor %%rax, %%rax; not %%rax; 1:" : "=a" (ret) : "a" (x) , "d" (y));
return ret;
}
static inline size_t
add_sat_branchfree (size_t x, size_t y)
{
size_t ret, scratch;
asm ("add %%rdx, %%rax; sbb %%rdx, %%rdx; or %%rdx, %%rax" : "=a" (ret), "=d" (scratch) : "a" (x) , "d" (y));
return ret;
}
static inline size_t
mul_sat_branch (size_t x, size_t y)
{
size_t ret;
asm ("mul %%rdx; jno 1f; xor %%rax, %%rax; not %%rax; 1:" : "=a" (ret) : "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);
}