|
Message-ID: <20140519181257.GA12324@port70.net> Date: Mon, 19 May 2014 20:12:58 +0200 From: Szabolcs Nagy <nsz@...t70.net> To: musl@...ts.openwall.com Subject: Re: thoughts on reallocarray, explicit_bzero? * Rich Felker <dalias@...c.org> [2014-05-19 12:55:23 -0400]: > On Mon, May 19, 2014 at 06:25:57PM +0200, Szabolcs Nagy wrote: > > static size_t sizemul(size_t a, size_t b) > > { > > return b>1 && a>1 && a>-1/b ? -1 : a*b; > > } > > On 32-bit this can easily be optimized to just one conditional instead > of three: > > uint64_t tmp = (uint64_t)a * b; > return tmp>SIZE_MAX ? SIZE_MAX : tmp; > > Of course that requires an ifdef, which is perhaps ugly. or without ifdef static size_t sizemul(size_t a, size_t b) { if (2*sizeof a <= sizeof 1ULL) { unsigned long long m = 1ULL*a*b; return m>>8*sizeof a ? -1 : m; } return (a|b)>>4*sizeof a && b && a>-1/b ? -1 : a*b; }
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.