From 24055b8c49e2c491f0048d553d24707efe9f166c Mon Sep 17 00:00:00 2001 From: Linas Vepstas Date: Sun, 27 Jun 2004 20:06:36 +0000 Subject: [PATCH] add a shift-right (divide by two) function git-svn-id: svn+ssh://svn.gnucash.org/repo/gnucash/trunk@10115 57a11ea4-9604-0410-9ed3-97b8803252fd --- src/engine/qofmath128.c | 22 ++++++++++++++++++++++ src/engine/qofmath128.h | 3 +++ 2 files changed, 25 insertions(+) diff --git a/src/engine/qofmath128.c b/src/engine/qofmath128.c index 5b409cee41..99185ed38c 100644 --- a/src/engine/qofmath128.c +++ b/src/engine/qofmath128.c @@ -278,6 +278,28 @@ add128 (qofint128 a, qofint128 b) return sum; } +/** Shift right by one bit (i.e. divide by two) */ +inline qofint128 +shift128 (qofint128 x) +{ + guint64 sbit = x.hi & 0x1; + x.hi >>= 1; + x.lo >>= 1; + if (sbit) + { + sbit = 1<<30; /* in two step to avoid 1ULL<<63 */ + sbit <<= 33; + x.lo |= sbit; + x.isbig = 1; + return x; + } + if (x.hi) + { + x.isbig = 1; + } + return x; +} + #ifdef TEST_128_BIT_MULT static void pr (gint64 a, gint64 b) { diff --git a/src/engine/qofmath128.h b/src/engine/qofmath128.h index a0b09c308c..e5fe11f60b 100644 --- a/src/engine/qofmath128.h +++ b/src/engine/qofmath128.h @@ -71,6 +71,9 @@ inline qofint128 lcm128 (guint64 a, guint64 b); /** Add a pair of 128-bit numbers, returning a 128-bit number */ inline qofint128 add128 (qofint128 a, qofint128 b); +/** Shift right by one bit (i.e. divide by two) */ +inline qofint128 shift128 (qofint128 x); + #endif /** @} */