+++ /dev/null
-/* $NetBSD: dmisc.c,v 1.2.4.1.4.1 2008/04/08 21:10:55 jdc Exp $ */\r
-\r
-/****************************************************************\r
-\r
-The author of this software is David M. Gay.\r
-\r
-Copyright (C) 1998 by Lucent Technologies\r
-All Rights Reserved\r
-\r
-Permission to use, copy, modify, and distribute this software and\r
-its documentation for any purpose and without fee is hereby\r
-granted, provided that the above copyright notice appear in all\r
-copies and that both that the copyright notice and this\r
-permission notice and warranty disclaimer appear in supporting\r
-documentation, and that the name of Lucent or any of its entities\r
-not be used in advertising or publicity pertaining to\r
-distribution of the software without specific, written prior\r
-permission.\r
-\r
-LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,\r
-INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.\r
-IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY\r
-SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES\r
-WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER\r
-IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,\r
-ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF\r
-THIS SOFTWARE.\r
-\r
-****************************************************************/\r
-\r
-/* Please send bug reports to David M. Gay (dmg at acm dot org,\r
- * with " at " changed at "@" and " dot " changed to "."). */\r
-#include <LibConfig.h>\r
-\r
-#include "gdtoaimp.h"\r
-\r
-#ifndef MULTIPLE_THREADS\r
- char *dtoa_result;\r
-#endif\r
-\r
- char *\r
-#ifdef KR_headers\r
-rv_alloc(i) size_t i;\r
-#else\r
-rv_alloc(size_t i)\r
-#endif\r
-{\r
- size_t j;\r
- int k, *r;\r
-\r
- j = sizeof(ULong);\r
- for(k = 0;\r
- sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= i;\r
- j <<= 1)\r
- k++;\r
- r = (int*)(void*)Balloc(k);\r
- if (r == NULL)\r
- return NULL;\r
- *r = k;\r
- return\r
-#ifndef MULTIPLE_THREADS\r
- dtoa_result =\r
-#endif\r
- (char *)(void *)(r+1);\r
- }\r
-\r
- char *\r
-#ifdef KR_headers\r
-nrv_alloc(s, rve, n) CONST char *s; char **rve; size_t n;\r
-#else\r
-nrv_alloc(CONST char *s, char **rve, size_t n)\r
-#endif\r
-{\r
- char *rv, *t;\r
-\r
- t = rv = rv_alloc(n);\r
- if (t == NULL)\r
- return NULL;\r
- while((*t = *s++) !=0)\r
- t++;\r
- if (rve)\r
- *rve = t;\r
- return rv;\r
- }\r
-\r
-/* freedtoa(s) must be used to free values s returned by dtoa\r
- * when MULTIPLE_THREADS is #defined. It should be used in all cases,\r
- * but for consistency with earlier versions of dtoa, it is optional\r
- * when MULTIPLE_THREADS is not defined.\r
- */\r
-\r
- void\r
-#ifdef KR_headers\r
-freedtoa(s) char *s;\r
-#else\r
-freedtoa(char *s)\r
-#endif\r
-{\r
- Bigint *b = (Bigint *)(void *)((int *)(void *)s - 1);\r
- b->maxwds = 1 << (b->k = *(int*)(void*)b);\r
- Bfree(b);\r
-#ifndef MULTIPLE_THREADS\r
- if (s == dtoa_result)\r
- dtoa_result = 0;\r
-#endif\r
- }\r
-\r
- int\r
-quorem\r
-#ifdef KR_headers\r
- (b, S) Bigint *b, *S;\r
-#else\r
- (Bigint *b, Bigint *S)\r
-#endif\r
-{\r
- int n;\r
- ULong *bx, *bxe, q, *sx, *sxe;\r
-#ifdef ULLong\r
- ULLong borrow, carry, y, ys;\r
-#else\r
- ULong borrow, carry, y, ys;\r
-#ifdef Pack_32\r
- ULong si, z, zs;\r
-#endif\r
-#endif\r
-\r
- n = S->wds;\r
-#ifdef DEBUG\r
- /*debug*/ if (b->wds > n)\r
- /*debug*/ Bug("oversize b in quorem");\r
-#endif\r
- if (b->wds < n)\r
- return 0;\r
- sx = S->x;\r
- sxe = sx + --n;\r
- bx = b->x;\r
- bxe = bx + n;\r
- q = *bxe / (*sxe + 1); /* ensure q <= true quotient */\r
-#ifdef DEBUG\r
- /*debug*/ if (q > 9)\r
- /*debug*/ Bug("oversized quotient in quorem");\r
-#endif\r
- if (q) {\r
- borrow = 0;\r
- carry = 0;\r
- do {\r
-#ifdef ULLong\r
- ys = *sx++ * (ULLong)q + carry;\r
- carry = ys >> 32;\r
- /* LINTED conversion */\r
- y = *bx - (ys & 0xffffffffUL) - borrow;\r
- borrow = y >> 32 & 1UL;\r
- /* LINTED conversion */\r
- *bx++ = (UINT32)(y & 0xffffffffUL);\r
-#else\r
-#ifdef Pack_32\r
- si = *sx++;\r
- ys = (si & 0xffff) * q + carry;\r
- zs = (si >> 16) * q + (ys >> 16);\r
- carry = zs >> 16;\r
- y = (*bx & 0xffff) - (ys & 0xffff) - borrow;\r
- borrow = (y & 0x10000) >> 16;\r
- z = (*bx >> 16) - (zs & 0xffff) - borrow;\r
- borrow = (z & 0x10000) >> 16;\r
- Storeinc(bx, z, y);\r
-#else\r
- ys = *sx++ * q + carry;\r
- carry = ys >> 16;\r
- y = *bx - (ys & 0xffff) - borrow;\r
- borrow = (y & 0x10000) >> 16;\r
- *bx++ = y & 0xffff;\r
-#endif\r
-#endif\r
- }\r
- while(sx <= sxe);\r
- if (!*bxe) {\r
- bx = b->x;\r
- while(--bxe > bx && !*bxe)\r
- --n;\r
- b->wds = n;\r
- }\r
- }\r
- if (cmp(b, S) >= 0) {\r
- q++;\r
- borrow = 0;\r
- carry = 0;\r
- bx = b->x;\r
- sx = S->x;\r
- do {\r
-#ifdef ULLong\r
- ys = *sx++ + carry;\r
- carry = ys >> 32;\r
- /* LINTED conversion */\r
- y = *bx - (ys & 0xffffffffUL) - borrow;\r
- borrow = y >> 32 & 1UL;\r
- /* LINTED conversion */\r
- *bx++ = (UINT32)(y & 0xffffffffUL);\r
-#else\r
-#ifdef Pack_32\r
- si = *sx++;\r
- ys = (si & 0xffff) + carry;\r
- zs = (si >> 16) + (ys >> 16);\r
- carry = zs >> 16;\r
- y = (*bx & 0xffff) - (ys & 0xffff) - borrow;\r
- borrow = (y & 0x10000) >> 16;\r
- z = (*bx >> 16) - (zs & 0xffff) - borrow;\r
- borrow = (z & 0x10000) >> 16;\r
- Storeinc(bx, z, y);\r
-#else\r
- ys = *sx++ + carry;\r
- carry = ys >> 16;\r
- y = *bx - (ys & 0xffff) - borrow;\r
- borrow = (y & 0x10000) >> 16;\r
- *bx++ = y & 0xffff;\r
-#endif\r
-#endif\r
- }\r
- while(sx <= sxe);\r
- bx = b->x;\r
- bxe = bx + n;\r
- if (!*bxe) {\r
- while(--bxe > bx && !*bxe)\r
- --n;\r
- b->wds = n;\r
- }\r
- }\r
- return (int)q;\r
- }\r