From 3534b428629be185e096be99e3bd5fdfe32d5544 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Thu, 18 Sep 2014 18:55:44 +0000 Subject: initial commit with rc for skalibs-2.0.0.0 --- src/libbiguint/bu_div_internal.c | 46 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/libbiguint/bu_div_internal.c (limited to 'src/libbiguint/bu_div_internal.c') diff --git a/src/libbiguint/bu_div_internal.c b/src/libbiguint/bu_div_internal.c new file mode 100644 index 0000000..d1be789 --- /dev/null +++ b/src/libbiguint/bu_div_internal.c @@ -0,0 +1,46 @@ +/* ISC license. */ + +#include +#include +#include + +/* + q = a/b, a = a mod b. Assumes b != 0 and qn >= alen - blen + 1. +*/ + +void bu_div_internal (uint32 *a, unsigned int an, uint32 const *b, unsigned int bn, uint32 *q, unsigned int qn) +{ + unsigned int alen = bu_len(a, an) ; + unsigned int blen = bu_len(b, bn) ; + bu_zero(q, qn) ; + if (alen < blen) return ; + { + uint32 bb[alen + 1] ; + unsigned int i = 1 + ((alen - blen) << 5) ; + bu_zero(bb, alen - blen) ; + bu_copy_internal(bb + alen - blen, b, blen) ; + bb[alen] = 0 ; + + while (bu_cmp(a, alen, bb, alen+1) >= 0) + { + bu_slb(bb + alen - blen, blen + 1) ; + i++ ; + } + while (i && (bu_cmp(a, alen, bb, alen+1) < 0)) + { + bu_srb(bb, alen + 1) ; + i-- ; + } + + while (i--) + { + bu_slb(q, alen - blen + 1) ; + if (bu_cmp(a, alen, bb, alen) >= 0) + { + bu_sub(a, alen, a, alen, bb, alen) ; + q[0] |= 1 ; + } + bu_srb(bb, alen) ; + } + } +} -- cgit v1.2.3