From: Ian Jackson Subject: Re: xl: Need help with overflow and error handling for vif rate support Date: Thu, 29 Mar 2012 15:36:48 +0100 Message-ID: <20340.29568.999666.53434@mariner.uk.xensource.com> References: <4F71F32D.9080601@iweb.com> <1332928628.14468.9.camel@zakaz.uk.xensource.com> Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Return-path: In-Reply-To: <1332928628.14468.9.camel@zakaz.uk.xensource.com> List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Sender: xen-devel-bounces@lists.xen.org Errors-To: xen-devel-bounces@lists.xen.org To: Ian Campbell Cc: "xen-devel@lists.xen.org" List-Id: xen-devel@lists.xenproject.org Ian Campbell writes ("Re: [Xen-devel] xl: Need help with overflow and error= handling for vif rate support"): > On Tue, 2012-03-27 at 18:04 +0100, Mathieu Gagn=E9 wrote: > > - How should I check for overflows when multiplying 2 uint64_t together? > > - I'm currently using math.h and log. Is it the correct approach? > = > You can check if A*B > UINT64_MAX by first checking > if B !=3D 0 && A > UINT64_MAX/B > WOULD OVERFLOW Another approach is to limit both A and B to less than sqrt(UINT64_MAX), or to limit each of them to some smaller number. Or use floating point, and do a check at the end when you convert it back. Ian.