/*-*- mode:c;indent-tabs-mode:t;c-basic-offset:8;tab-width:8;coding:utf-8   -*-â
âvi: set et ft=c ts=8 tw=8 fenc=utf-8                                       :viâ
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â                                                                              â
â  The author of this software is David M. Gay.                                â
â  Please send bug reports to David M. Gay <dmg@acm.org>                       â
â                          or Justine Tunney <jtunney@gmail.com>               â
â                                                                              â
â  Copyright (C) 1998, 1999 by Lucent Technologies                             â
â  All Rights Reserved                                                         â
â                                                                              â
â  Permission to use, copy, modify, and distribute this software and           â
â  its documentation for any purpose and without fee is hereby                 â
â  granted, provided that the above copyright notice appear in all             â
â  copies and that both that the copyright notice and this                     â
â  permission notice and warranty disclaimer appear in supporting              â
â  documentation, and that the name of Lucent or any of its entities           â
â  not be used in advertising or publicity pertaining to                       â
â  distribution of the software without specific, written prior                â
â  permission.                                                                 â
â                                                                              â
â  LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,               â
â  INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.            â
â  IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY           â
â  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES                   â
â  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER             â
â  IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,              â
â  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF              â
â  THIS SOFTWARE.                                                              â
â                                                                              â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ*/
#include "third_party/gdtoa/gdtoa.internal.h"
/* clang-format off */

void
freedtoa(char *s)
{
	ThInfo *TI = 0;
	Bigint *b = (Bigint *)((int *)s - 1);
	b->maxwds = 1 << (b->k = *(int *)b);
	__gdtoa_Bfree(b, &TI);
}

char *
__gdtoa_rv_alloc(int i, ThInfo **PTI)
{
	int j, k, *r;
	j = sizeof(ULong);
	for (k = 0; (int)(sizeof(Bigint) - sizeof(ULong) - sizeof(int)) + j <= i;
	     j <<= 1)
		k++;
	r = (int *)__gdtoa_Balloc(k, PTI);
	*r = k;
	return (char *)(r + 1);
}

char *
__gdtoa_nrv_alloc(char *s, char **rve, int n, ThInfo **PTI)
{
	char *rv, *t;
	t = rv = __gdtoa_rv_alloc(n, PTI);
	while ((*t = *s++) != 0)
		t++;
	if (rve)
		*rve = t;
	return rv;
}

int
__gdtoa_quorem(Bigint *b, Bigint *S)
{
	int n;
	ULong *bx, *bxe, q, *sx, *sxe;
	ULLong borrow, carry, y, ys;
	n = S->wds;
#ifdef DEBUG
	if (b->wds > n)
		Bug("oversize b in __gdtoa_quorem");
#endif
	if (b->wds < n)
		return 0;
	sx = S->x;
	sxe = sx + --n;
	bx = b->x;
	bxe = bx + n;
	q = *bxe / (*sxe + 1);	/* ensure q <= true quotient */
#ifdef DEBUG
	if (q > 9)
		Bug("oversized quotient in __gdtoa_quorem");
#endif
	if (q) {
		borrow = 0;
		carry = 0;
		do {
			ys = *sx++ * (ULLong)q + carry;
			carry = ys >> 32;
			y = *bx - (ys & 0xffffffff) - borrow;
			borrow = y >> 32 & 1UL;
			*bx++ = y & 0xffffffff;
		}
		while(sx <= sxe);
		if (!*bxe) {
			bx = b->x;
			while(--bxe > bx && !*bxe)
				--n;
			b->wds = n;
		}
	}
	if (__gdtoa_cmp(b, S) >= 0) {
		q++;
		borrow = 0;
		carry = 0;
		bx = b->x;
		sx = S->x;
		do {
			ys = *sx++ + carry;
			carry = ys >> 32;
			y = *bx - (ys & 0xffffffff) - borrow;
			borrow = y >> 32 & 1UL;
			*bx++ = y & 0xffffffff;
		}
		while(sx <= sxe);
		bx = b->x;
		bxe = bx + n;
		if (!*bxe) {
			while(--bxe > bx && !*bxe)
				--n;
			b->wds = n;
		}
	}
	return q;
}
