Dataset Viewer
repo_name
string | dataset
string | owner
string | lang
string | func_name
string | code
string | docstring
string | url
string | sha
string |
---|---|---|---|---|---|---|---|---|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
NOT_IMPLEMENTED
|
void NOT_IMPLEMENTED(const char* psz)
{
if (strstr(QJSNotImplLog, psz))
return;
if (strlen(QJSNotImplLog) + strlen(psz) > sizeof(QJSNotImplLog) - 16)
return;
if (strlen(QJSNotImplLog))
strcat(QJSNotImplLog, ",");
strcat(QJSNotImplLog, psz);
return;
}
|
// --------- NOT_IMPLEMENTED function definition: we need to log any not implemented function invoked by QJS. ---------
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJSCInterface.c#L232-L246
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
_bc_WaitForSingleObject
|
DWORD __stdcall _bc_WaitForSingleObject(__in HANDLE hHandle, __in DWORD dwMilliseconds)
{
NOT_IMPLEMENTED("WaitForSingleObject");
return 0;
}
|
// --------- Windows APIs: ---------
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJSCInterface.c#L750-L754
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
gettimeofday
|
int gettimeofday(struct timeval * tp, struct timezone * tzp)
{
static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
SYSTEMTIME system_time;
FILETIME file_time;
uint64_t time;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
time = ((uint64_t)file_time.dwLowDateTime);
time += ((uint64_t)file_time.dwHighDateTime) << 32;
tp->tv_sec = (long)((time - EPOCH) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
return 0;
}
|
// From: https://stackoverflow.com/a/26085827
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/cutils.c#L35-L52
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
dbuf_realloc
|
int dbuf_realloc(DynBuf *s, size_t new_size)
{
size_t size;
uint8_t *new_buf;
if (new_size > s->allocated_size) {
if (s->error)
return -1;
size = s->allocated_size * 3 / 2;
if (size > new_size)
new_size = size;
new_buf = s->realloc_func(s->opaque, s->buf, new_size);
if (!new_buf) {
s->error = TRUE;
return -1;
}
s->buf = new_buf;
s->allocated_size = new_size;
}
return 0;
}
|
/* return < 0 if error */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/cutils.c#L128-L147
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
unicode_to_utf8
|
int unicode_to_utf8(uint8_t *buf, unsigned int c)
{
uint8_t *q = buf;
if (c < 0x80) {
*q++ = c;
} else {
if (c < 0x800) {
*q++ = (c >> 6) | 0xc0;
} else {
if (c < 0x10000) {
*q++ = (c >> 12) | 0xe0;
} else {
if (c < 0x00200000) {
*q++ = (c >> 18) | 0xf0;
} else {
if (c < 0x04000000) {
*q++ = (c >> 24) | 0xf8;
} else if (c < 0x80000000) {
*q++ = (c >> 30) | 0xfc;
*q++ = ((c >> 24) & 0x3f) | 0x80;
} else {
return 0;
}
*q++ = ((c >> 18) & 0x3f) | 0x80;
}
*q++ = ((c >> 12) & 0x3f) | 0x80;
}
*q++ = ((c >> 6) & 0x3f) | 0x80;
}
*q++ = (c & 0x3f) | 0x80;
}
return q - buf;
}
|
/* Note: at most 31 bits are encoded. At most UTF8_CHAR_LEN_MAX bytes
are output. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/cutils.c#L229-L262
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
unicode_from_utf8
|
int unicode_from_utf8(const uint8_t *p, int max_len, const uint8_t **pp)
{
int l, c, b, i;
c = *p++;
if (c < 0x80) {
*pp = p;
return c;
}
switch(c) {
case 0xc0: case 0xc1: case 0xc2: case 0xc3:
case 0xc4: case 0xc5: case 0xc6: case 0xc7:
case 0xc8: case 0xc9: case 0xca: case 0xcb:
case 0xcc: case 0xcd: case 0xce: case 0xcf:
case 0xd0: case 0xd1: case 0xd2: case 0xd3:
case 0xd4: case 0xd5: case 0xd6: case 0xd7:
case 0xd8: case 0xd9: case 0xda: case 0xdb:
case 0xdc: case 0xdd: case 0xde: case 0xdf:
l = 1;
break;
case 0xe0: case 0xe1: case 0xe2: case 0xe3:
case 0xe4: case 0xe5: case 0xe6: case 0xe7:
case 0xe8: case 0xe9: case 0xea: case 0xeb:
case 0xec: case 0xed: case 0xee: case 0xef:
l = 2;
break;
case 0xf0: case 0xf1: case 0xf2: case 0xf3:
case 0xf4: case 0xf5: case 0xf6: case 0xf7:
l = 3;
break;
case 0xf8: case 0xf9: case 0xfa: case 0xfb:
l = 4;
break;
case 0xfc: case 0xfd:
l = 5;
break;
default:
return -1;
}
/* check that we have enough characters */
if (l > (max_len - 1))
return -1;
c &= utf8_first_code_mask[l - 1];
for(i = 0; i < l; i++) {
b = *p++;
if (b < 0x80 || b >= 0xc0)
return -1;
c = (c << 6) | (b & 0x3f);
}
if (c < utf8_min_code[l - 1])
return -1;
*pp = p;
return c;
}
|
/* return -1 if error. *pp is not updated in this case. max_len must
be >= 1. The maximum length for a UTF8 byte sequence is 6 bytes. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/cutils.c#L274-L327
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
rqsort
|
void rqsort(void *base, size_t nmemb, size_t size,
int (*cmp)(const void *, const void *, void *),
void *arg)
{
rqsort_arg = arg;
rqsort_cmp = cmp;
qsort(base, nmemb, size, rqsort_cmp2);
}
|
/* not reentrant, but not needed with emscripten */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/cutils.c#L342-L349
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
rqsort
|
void rqsort(void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque)
{
struct { uint8_t *base; size_t count; int depth; } stack[50], *sp = stack;
uint8_t *ptr, *pi, *pj, *plt, *pgt, *top, *m;
size_t m4, i, lt, gt, span, span2;
int c, depth;
exchange_f swap = exchange_func(base, size);
exchange_f swap_block = exchange_func(base, size | 128);
if (nmemb < 2 || size <= 0)
return;
sp->base = (uint8_t *)base;
sp->count = nmemb;
sp->depth = 0;
sp++;
while (sp > stack) {
sp--;
ptr = sp->base;
nmemb = sp->count;
depth = sp->depth;
while (nmemb > 6) {
if (++depth > 50) {
/* depth check to ensure worst case logarithmic time */
heapsortx(ptr, nmemb, size, cmp, opaque);
nmemb = 0;
break;
}
/* select median of 3 from 1/4, 1/2, 3/4 positions */
/* should use median of 5 or 9? */
m4 = (nmemb >> 2) * size;
m = med3(ptr + m4, ptr + 2 * m4, ptr + 3 * m4, cmp, opaque);
swap(ptr, m, size); /* move the pivot to the start or the array */
i = lt = 1;
pi = plt = ptr + size;
gt = nmemb;
pj = pgt = top = ptr + nmemb * size;
for (;;) {
while (pi < pj && (c = cmp(ptr, pi, opaque)) >= 0) {
if (c == 0) {
swap(plt, pi, size);
lt++;
plt += size;
}
i++;
pi += size;
}
while (pi < (pj -= size) && (c = cmp(ptr, pj, opaque)) <= 0) {
if (c == 0) {
gt--;
pgt -= size;
swap(pgt, pj, size);
}
}
if (pi >= pj)
break;
swap(pi, pj, size);
i++;
pi += size;
}
/* array has 4 parts:
* from 0 to lt excluded: elements identical to pivot
* from lt to pi excluded: elements smaller than pivot
* from pi to gt excluded: elements greater than pivot
* from gt to n excluded: elements identical to pivot
*/
/* move elements identical to pivot in the middle of the array: */
/* swap values in ranges [0..lt[ and [i-lt..i[
swapping the smallest span between lt and i-lt is sufficient
*/
span = plt - ptr;
span2 = pi - plt;
lt = i - lt;
if (span > span2)
span = span2;
swap_block(ptr, pi - span, span);
/* swap values in ranges [gt..top[ and [i..top-(top-gt)[
swapping the smallest span between top-gt and gt-i is sufficient
*/
span = top - pgt;
span2 = pgt - pi;
pgt = top - span2;
gt = nmemb - (gt - i);
if (span > span2)
span = span2;
swap_block(pi, top - span, span);
/* now array has 3 parts:
* from 0 to lt excluded: elements smaller than pivot
* from lt to gt excluded: elements identical to pivot
* from gt to n excluded: elements greater than pivot
*/
/* stack the larger segment and keep processing the smaller one
to minimize stack use for pathological distributions */
if (lt > nmemb - gt) {
sp->base = ptr;
sp->count = lt;
sp->depth = depth;
sp++;
ptr = pgt;
nmemb -= gt;
} else {
sp->base = pgt;
sp->count = nmemb - gt;
sp->depth = depth;
sp++;
nmemb = lt;
}
}
/* Use insertion sort for small fragments */
for (pi = ptr + size, top = ptr + nmemb * size; pi < top; pi += size) {
for (pj = pi; pj > ptr && cmp(pj - size, pj, opaque) > 0; pj -= size)
swap(pj, pj - size, size);
}
}
}
|
/* pointer based version with local stack and insertion sort threshhold */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/cutils.c#L535-L652
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
clz
|
static inline int clz(limb_t a)
{
if (a == 0) {
return LIMB_BITS;
} else {
#if LIMB_BITS == 64
return clz64(a);
#else
return clz32(a);
#endif
}
}
|
/* could leading zeros */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L94-L105
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
ceil_div
|
static inline slimb_t ceil_div(slimb_t a, slimb_t b)
{
if (a >= 0)
return (a + b - 1) / b;
else
return a / b;
}
|
/* b must be >= 1 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L129-L135
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
floor_div
|
static inline slimb_t floor_div(slimb_t a, slimb_t b)
{
if (a >= 0) {
return a / b;
} else {
return (a - b + 1) / b;
}
}
|
/* b must be >= 1 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L138-L145
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
smod
|
static inline limb_t smod(slimb_t a, slimb_t b)
{
a = a % (slimb_t)b;
if (a < 0)
a += b;
return a;
}
|
/* return r = a modulo b (0 <= r <= b - 1. b must be >= 1 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L148-L154
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
sat_add
|
static inline slimb_t sat_add(slimb_t a, slimb_t b)
{
slimb_t r;
r = a + b;
/* overflow ? */
if (((a ^ r) & (b ^ r)) < 0)
r = (a >> (LIMB_BITS - 1)) ^ (((limb_t)1 << (LIMB_BITS - 1)) - 1);
return r;
}
|
/* signed addition with saturation */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L157-L165
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_resize
|
int bf_resize(bf_t *r, limb_t len)
{
limb_t *tab;
if (len != r->len) {
tab = bf_realloc(r->ctx, r->tab, len * sizeof(limb_t));
if (!tab && len != 0)
return -1;
r->tab = tab;
r->len = len;
}
return 0;
}
|
/* return 0 if OK, -1 if alloc error */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L194-L206
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_set_ui
|
int bf_set_ui(bf_t *r, uint64_t a)
{
r->sign = 0;
if (a == 0) {
r->expn = BF_EXP_ZERO;
bf_resize(r, 0); /* cannot fail */
}
#if LIMB_BITS == 32
else if (a <= 0xffffffff)
#else
else
#endif
{
int shift;
if (bf_resize(r, 1))
goto fail;
shift = clz(a);
r->tab[0] = a << shift;
r->expn = LIMB_BITS - shift;
}
#if LIMB_BITS == 32
else {
uint32_t a1, a0;
int shift;
if (bf_resize(r, 2))
goto fail;
a0 = a;
a1 = a >> 32;
shift = clz(a1);
r->tab[0] = a0 << shift;
r->tab[1] = (a1 << shift) | (a0 >> (LIMB_BITS - shift));
r->expn = 2 * LIMB_BITS - shift;
}
#endif
return 0;
fail:
bf_set_nan(r);
return BF_ST_MEM_ERROR;
}
|
/* return 0 or BF_ST_MEM_ERROR */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L209-L247
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_set_si
|
int bf_set_si(bf_t *r, int64_t a)
{
int ret;
if (a < 0) {
ret = bf_set_ui(r, -a);
r->sign = 1;
} else {
ret = bf_set_ui(r, a);
}
return ret;
}
|
/* return 0 or BF_ST_MEM_ERROR */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L250-L261
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_set
|
int bf_set(bf_t *r, const bf_t *a)
{
if (r == a)
return 0;
if (bf_resize(r, a->len)) {
bf_set_nan(r);
return BF_ST_MEM_ERROR;
}
r->sign = a->sign;
r->expn = a->expn;
memcpy(r->tab, a->tab, a->len * sizeof(limb_t));
return 0;
}
|
/* return 0 or BF_ST_MEM_ERROR */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L285-L297
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_move
|
void bf_move(bf_t *r, bf_t *a)
{
bf_context_t *s = r->ctx;
if (r == a)
return;
bf_free(s, r->tab);
*r = *a;
}
|
/* equivalent to bf_set(r, a); bf_delete(a) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L300-L307
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
get_bits
|
static inline limb_t get_bits(const limb_t *tab, limb_t len, slimb_t pos)
{
limb_t i, a0, a1;
int p;
i = pos >> LIMB_LOG2_BITS;
p = pos & (LIMB_BITS - 1);
if (i < len)
a0 = tab[i];
else
a0 = 0;
if (p == 0) {
return a0;
} else {
i++;
if (i < len)
a1 = tab[i];
else
a1 = 0;
return (a0 >> p) | (a1 << (LIMB_BITS - p));
}
}
|
/* get LIMB_BITS at bit position 'pos' in tab */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L318-L339
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
scan_bit_nz
|
static inline limb_t scan_bit_nz(const bf_t *r, slimb_t bit_pos)
{
slimb_t pos;
limb_t v;
pos = bit_pos >> LIMB_LOG2_BITS;
if (pos < 0)
return 0;
v = r->tab[pos] & limb_mask(0, bit_pos & (LIMB_BITS - 1));
if (v != 0)
return 1;
pos--;
while (pos >= 0) {
if (r->tab[pos] != 0)
return 1;
pos--;
}
return 0;
}
|
/* return != 0 if one bit between 0 and bit_pos inclusive is not zero. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L373-L391
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_get_rnd_add
|
static int bf_get_rnd_add(int *pret, const bf_t *r, limb_t l,
slimb_t prec, int rnd_mode)
{
int add_one, inexact;
limb_t bit1, bit0;
if (rnd_mode == BF_RNDF) {
bit0 = 1; /* faithful rounding does not honor the INEXACT flag */
} else {
/* starting limb for bit 'prec + 1' */
bit0 = scan_bit_nz(r, l * LIMB_BITS - 1 - bf_max(0, prec + 1));
}
/* get the bit at 'prec' */
bit1 = get_bit(r->tab, l, l * LIMB_BITS - 1 - prec);
inexact = (bit1 | bit0) != 0;
add_one = 0;
switch(rnd_mode) {
case BF_RNDZ:
break;
case BF_RNDN:
if (bit1) {
if (bit0) {
add_one = 1;
} else {
/* round to even */
add_one =
get_bit(r->tab, l, l * LIMB_BITS - 1 - (prec - 1));
}
}
break;
case BF_RNDD:
case BF_RNDU:
if (r->sign == (rnd_mode == BF_RNDD))
add_one = inexact;
break;
case BF_RNDA:
add_one = inexact;
break;
case BF_RNDNA:
case BF_RNDF:
add_one = bit1;
break;
default:
abort();
}
if (inexact)
*pret |= BF_ST_INEXACT;
return add_one;
}
|
/* return the addend for rounding. Note that prec can be <= 0 (for
BF_FLAG_RADPNT_PREC) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L395-L446
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
__bf_round
|
static int __bf_round(bf_t *r, limb_t prec1, bf_flags_t flags, limb_t l,
int ret)
{
limb_t v, a;
int shift, add_one, rnd_mode;
slimb_t i, bit_pos, pos, e_min, e_max, e_range, prec;
/* e_min and e_max are computed to match the IEEE 754 conventions */
e_range = (limb_t)1 << (bf_get_exp_bits(flags) - 1);
e_min = -e_range + 3;
e_max = e_range;
if (flags & BF_FLAG_RADPNT_PREC) {
/* 'prec' is the precision after the radix point */
if (prec1 != BF_PREC_INF)
prec = r->expn + prec1;
else
prec = prec1;
} else if (unlikely(r->expn < e_min) && (flags & BF_FLAG_SUBNORMAL)) {
/* restrict the precision in case of potentially subnormal
result */
assert(prec1 != BF_PREC_INF);
prec = prec1 - (e_min - r->expn);
} else {
prec = prec1;
}
/* round to prec bits */
rnd_mode = flags & BF_RND_MASK;
add_one = bf_get_rnd_add(&ret, r, l, prec, rnd_mode);
if (prec <= 0) {
if (add_one) {
bf_resize(r, 1); /* cannot fail */
r->tab[0] = (limb_t)1 << (LIMB_BITS - 1);
r->expn += 1 - prec;
ret |= BF_ST_UNDERFLOW | BF_ST_INEXACT;
return ret;
} else {
goto underflow;
}
} else if (add_one) {
limb_t carry;
/* add one starting at digit 'prec - 1' */
bit_pos = l * LIMB_BITS - 1 - (prec - 1);
pos = bit_pos >> LIMB_LOG2_BITS;
carry = (limb_t)1 << (bit_pos & (LIMB_BITS - 1));
for(i = pos; i < l; i++) {
v = r->tab[i] + carry;
carry = (v < carry);
r->tab[i] = v;
if (carry == 0)
break;
}
if (carry) {
/* shift right by one digit */
v = 1;
for(i = l - 1; i >= pos; i--) {
a = r->tab[i];
r->tab[i] = (a >> 1) | (v << (LIMB_BITS - 1));
v = a;
}
r->expn++;
}
}
/* check underflow */
if (unlikely(r->expn < e_min)) {
if (flags & BF_FLAG_SUBNORMAL) {
/* if inexact, also set the underflow flag */
if (ret & BF_ST_INEXACT)
ret |= BF_ST_UNDERFLOW;
} else {
underflow:
ret |= BF_ST_UNDERFLOW | BF_ST_INEXACT;
bf_set_zero(r, r->sign);
return ret;
}
}
/* check overflow */
if (unlikely(r->expn > e_max))
return bf_set_overflow(r, r->sign, prec1, flags);
/* keep the bits starting at 'prec - 1' */
bit_pos = l * LIMB_BITS - 1 - (prec - 1);
i = bit_pos >> LIMB_LOG2_BITS;
if (i >= 0) {
shift = bit_pos & (LIMB_BITS - 1);
if (shift != 0)
r->tab[i] &= limb_mask(shift, LIMB_BITS - 1);
} else {
i = 0;
}
/* remove trailing zeros */
while (r->tab[i] == 0)
i++;
if (i > 0) {
l -= i;
memmove(r->tab, r->tab + i, l * sizeof(limb_t));
}
bf_resize(r, l); /* cannot fail */
return ret;
}
|
/* round to prec1 bits assuming 'r' is non zero and finite. 'r' is
assumed to have length 'l' (1 <= l <= r->len). Note: 'prec1' can be
infinite (BF_PREC_INF). 'ret' is 0 or BF_ST_INEXACT if the result
is known to be inexact. Can fail with BF_ST_MEM_ERROR in case of
overflow not returning infinity. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L484-L589
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_normalize_and_round
|
int bf_normalize_and_round(bf_t *r, limb_t prec1, bf_flags_t flags)
{
limb_t l, v, a;
int shift, ret;
slimb_t i;
// bf_print_str("bf_renorm", r);
l = r->len;
while (l > 0 && r->tab[l - 1] == 0)
l--;
if (l == 0) {
/* zero */
r->expn = BF_EXP_ZERO;
bf_resize(r, 0); /* cannot fail */
ret = 0;
} else {
r->expn -= (r->len - l) * LIMB_BITS;
/* shift to have the MSB set to '1' */
v = r->tab[l - 1];
shift = clz(v);
if (shift != 0) {
v = 0;
for(i = 0; i < l; i++) {
a = r->tab[i];
r->tab[i] = (a << shift) | (v >> (LIMB_BITS - shift));
v = a;
}
r->expn -= shift;
}
ret = __bf_round(r, prec1, flags, l, 0);
}
// bf_print_str("r_final", r);
return ret;
}
|
/* 'r' must be a finite number. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L592-L625
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_can_round
|
int bf_can_round(const bf_t *a, slimb_t prec, bf_rnd_t rnd_mode, slimb_t k)
{
BOOL is_rndn;
slimb_t bit_pos, n;
limb_t bit;
if (a->expn == BF_EXP_INF || a->expn == BF_EXP_NAN)
return FALSE;
if (rnd_mode == BF_RNDF) {
return (k >= (prec + 1));
}
if (a->expn == BF_EXP_ZERO)
return FALSE;
is_rndn = (rnd_mode == BF_RNDN || rnd_mode == BF_RNDNA);
if (k < (prec + 2))
return FALSE;
bit_pos = a->len * LIMB_BITS - 1 - prec;
n = k - prec;
/* bit pattern for RNDN or RNDNA: 0111.. or 1000...
for other rounding modes: 000... or 111...
*/
bit = get_bit(a->tab, a->len, bit_pos);
bit_pos--;
n--;
bit ^= is_rndn;
/* XXX: slow, but a few iterations on average */
while (n != 0) {
if (get_bit(a->tab, a->len, bit_pos) != bit)
return TRUE;
bit_pos--;
n--;
}
return FALSE;
}
|
/* return true if rounding can be done at precision 'prec' assuming
the exact result r is such that |r-a| <= 2^(EXP(a)-k). */
/* XXX: check the case where the exponent would be incremented by the
rounding */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L631-L664
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_round
|
int bf_round(bf_t *r, limb_t prec, bf_flags_t flags)
{
if (r->len == 0)
return 0;
return __bf_round(r, prec, flags, r->len, 0);
}
|
/* Cannot fail with BF_ST_MEM_ERROR. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L667-L672
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
dump_limbs
|
static __maybe_unused void dump_limbs(const char *str, const limb_t *tab, limb_t n)
{
limb_t i;
printf("%s: len=%" PRId_LIMB "\n", str, n);
for(i = 0; i < n; i++) {
printf("%" PRId_LIMB ": " FMT_LIMB "\n",
i, tab[i]);
}
}
|
/* for debugging */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L675-L683
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_print_str
|
void bf_print_str(const char *str, const bf_t *a)
{
slimb_t i;
printf("%s=", str);
if (a->expn == BF_EXP_NAN) {
printf("NaN");
} else {
if (a->sign)
putchar('-');
if (a->expn == BF_EXP_ZERO) {
putchar('0');
} else if (a->expn == BF_EXP_INF) {
printf("Inf");
} else {
printf("0x0.");
for(i = a->len - 1; i >= 0; i--)
printf(FMT_LIMB, a->tab[i]);
printf("p%" PRId_LIMB, a->expn);
}
}
printf("\n");
}
|
/* for debugging */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L712-L734
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_cmpu
|
int bf_cmpu(const bf_t *a, const bf_t *b)
{
slimb_t i;
limb_t len, v1, v2;
if (a->expn != b->expn) {
if (a->expn < b->expn)
return -1;
else
return 1;
}
len = bf_max(a->len, b->len);
for(i = len - 1; i >= 0; i--) {
v1 = get_limbz(a, a->len - len + i);
v2 = get_limbz(b, b->len - len + i);
if (v1 != v2) {
if (v1 < v2)
return -1;
else
return 1;
}
}
return 0;
}
|
/* compare the absolute value of 'a' and 'b'. Return < 0 if a < b, 0
if a = b and > 0 otherwise. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L738-L761
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_cmp_full
|
int bf_cmp_full(const bf_t *a, const bf_t *b)
{
int res;
if (a->expn == BF_EXP_NAN || b->expn == BF_EXP_NAN) {
if (a->expn == b->expn)
res = 0;
else if (a->expn == BF_EXP_NAN)
res = 1;
else
res = -1;
} else if (a->sign != b->sign) {
res = 1 - 2 * a->sign;
} else {
res = bf_cmpu(a, b);
if (a->sign)
res = -res;
}
return res;
}
|
/* Full order: -0 < 0, NaN == NaN and NaN is larger than all other numbers */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L764-L783
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_cmp
|
int bf_cmp(const bf_t *a, const bf_t *b)
{
int res;
if (a->expn == BF_EXP_NAN || b->expn == BF_EXP_NAN) {
res = 2;
} else if (a->sign != b->sign) {
if (a->expn == BF_EXP_ZERO && b->expn == BF_EXP_ZERO)
res = 0;
else
res = 1 - 2 * a->sign;
} else {
res = bf_cmpu(a, b);
if (a->sign)
res = -res;
}
return res;
}
|
/* Standard floating point comparison: return 2 if one of the operands
is NaN (unordered) or -1, 0, 1 depending on the ordering assuming
-0 == +0 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L788-L805
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
count_cancelled_bits
|
static limb_t count_cancelled_bits(const bf_t *a, const bf_t *b)
{
slimb_t bit_offset, b_offset, n;
int p, p1;
limb_t v1, v2, mask;
bit_offset = a->len * LIMB_BITS - 1;
b_offset = (b->len - a->len) * LIMB_BITS - (LIMB_BITS - 1) +
a->expn - b->expn;
n = 0;
/* first search the equals bits */
for(;;) {
v1 = get_limbz(a, bit_offset >> LIMB_LOG2_BITS);
v2 = get_bits(b->tab, b->len, bit_offset + b_offset);
// printf("v1=" FMT_LIMB " v2=" FMT_LIMB "\n", v1, v2);
if (v1 != v2)
break;
n += LIMB_BITS;
bit_offset -= LIMB_BITS;
}
/* find the position of the first different bit */
p = clz(v1 ^ v2) + 1;
n += p;
/* then search for '0' in a and '1' in b */
p = LIMB_BITS - p;
if (p > 0) {
/* search in the trailing p bits of v1 and v2 */
mask = limb_mask(0, p - 1);
p1 = bf_min(clz(v1 & mask), clz((~v2) & mask)) - (LIMB_BITS - p);
n += p1;
if (p1 != p)
goto done;
}
bit_offset -= LIMB_BITS;
for(;;) {
v1 = get_limbz(a, bit_offset >> LIMB_LOG2_BITS);
v2 = get_bits(b->tab, b->len, bit_offset + b_offset);
// printf("v1=" FMT_LIMB " v2=" FMT_LIMB "\n", v1, v2);
if (v1 != 0 || v2 != -1) {
/* different: count the matching bits */
p1 = bf_min(clz(v1), clz(~v2));
n += p1;
break;
}
n += LIMB_BITS;
bit_offset -= LIMB_BITS;
}
done:
return n;
}
|
/* Compute the number of bits 'n' matching the pattern:
a= X1000..0
b= X0111..1
When computing a-b, the result will have at least n leading zero
bits.
Precondition: a > b and a.expn - b.expn = 0 or 1
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L816-L866
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_neg
|
static limb_t mp_neg(limb_t *res, const limb_t *op2, mp_size_t n, limb_t carry)
{
int i;
limb_t k, a, v, k1;
k = carry;
for(i=0;i<n;i++) {
v = 0;
a = v - op2[i];
k1 = a > v;
v = a - k;
k = (v > a) | k1;
res[i] = v;
}
return k;
}
|
/* compute 0 - op2 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1077-L1092
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_shr
|
static limb_t mp_shr(limb_t *tab_r, const limb_t *tab, mp_size_t n,
int shift, limb_t high)
{
mp_size_t i;
limb_t l, a;
assert(shift >= 1 && shift < LIMB_BITS);
l = high;
for(i = n - 1; i >= 0; i--) {
a = tab[i];
tab_r[i] = (a >> shift) | (l << (LIMB_BITS - shift));
l = a;
}
return l & (((limb_t)1 << shift) - 1);
}
|
/* r = (a + high*B^n) >> shift. Return the remainder r (0 <= r < 2^shift).
1 <= shift <= LIMB_BITS - 1 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1113-L1127
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_mul1
|
static limb_t mp_mul1(limb_t *tabr, const limb_t *taba, limb_t n,
limb_t b, limb_t l)
{
limb_t i;
dlimb_t t;
for(i = 0; i < n; i++) {
t = (dlimb_t)taba[i] * (dlimb_t)b + l;
tabr[i] = t;
l = t >> LIMB_BITS;
}
return l;
}
|
/* tabr[] = taba[] * b + l. Return the high carry */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1130-L1142
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_add_mul1
|
static limb_t mp_add_mul1(limb_t *tabr, const limb_t *taba, limb_t n,
limb_t b)
{
limb_t i, l;
dlimb_t t;
l = 0;
for(i = 0; i < n; i++) {
t = (dlimb_t)taba[i] * (dlimb_t)b + l + tabr[i];
tabr[i] = t;
l = t >> LIMB_BITS;
}
return l;
}
|
/* tabr[] += taba[] * b, return the high word. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1145-L1158
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_mul_basecase
|
static void mp_mul_basecase(limb_t *result,
const limb_t *op1, limb_t op1_size,
const limb_t *op2, limb_t op2_size)
{
limb_t i, r;
result[op1_size] = mp_mul1(result, op1, op1_size, op2[0], 0);
for(i=1;i<op2_size;i++) {
r = mp_add_mul1(result + i, op1, op1_size, op2[i]);
result[i + op1_size] = r;
}
}
|
/* size of the result : op1_size + op2_size. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1161-L1172
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_mul
|
int mp_mul(bf_context_t *s, limb_t *result,
const limb_t *op1, limb_t op1_size,
const limb_t *op2, limb_t op2_size)
{
#ifdef USE_FFT_MUL
if (unlikely(bf_min(op1_size, op2_size) >= FFT_MUL_THRESHOLD)) {
bf_t r_s, *r = &r_s;
r->tab = result;
/* XXX: optimize memory usage in API */
if (fft_mul(s, r, (limb_t *)op1, op1_size,
(limb_t *)op2, op2_size, FFT_MUL_R_NORESIZE))
return -1;
} else
#endif
{
mp_mul_basecase(result, op1, op1_size, op2, op2_size);
}
return 0;
}
|
/* return 0 if OK, -1 if memory error */
/* XXX: change API so that result can be allocated */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1176-L1194
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_sub_mul1
|
static limb_t mp_sub_mul1(limb_t *tabr, const limb_t *taba, limb_t n,
limb_t b)
{
limb_t i, l;
dlimb_t t;
l = 0;
for(i = 0; i < n; i++) {
t = tabr[i] - (dlimb_t)taba[i] * (dlimb_t)b - l;
tabr[i] = t;
l = -(t >> LIMB_BITS);
}
return l;
}
|
/* tabr[] -= taba[] * b. Return the value to substract to the high
word. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1198-L1211
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
udiv1norm_init
|
static inline limb_t udiv1norm_init(limb_t d)
{
limb_t a0, a1;
a1 = -d - 1;
a0 = -1;
return (((dlimb_t)a1 << LIMB_BITS) | a0) / d;
}
|
/* WARNING: d must be >= 2^(LIMB_BITS-1) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1214-L1220
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
udiv1norm
|
static inline limb_t udiv1norm(limb_t *pr, limb_t a1, limb_t a0,
limb_t d, limb_t d_inv)
{
limb_t n1m, n_adj, q, r, ah;
dlimb_t a;
n1m = ((slimb_t)a0 >> (LIMB_BITS - 1));
n_adj = a0 + (n1m & d);
a = (dlimb_t)d_inv * (a1 - n1m) + n_adj;
q = (a >> LIMB_BITS) + a1;
/* compute a - q * r and update q so that the remainder is\
between 0 and d - 1 */
a = ((dlimb_t)a1 << LIMB_BITS) | a0;
a = a - (dlimb_t)q * d - d;
ah = a >> LIMB_BITS;
q += 1 + ah;
r = (limb_t)a + (ah & d);
*pr = r;
return q;
}
|
/* return the quotient and the remainder in '*pr'of 'a1*2^LIMB_BITS+a0
/ d' with 0 <= a1 < d. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1224-L1242
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_div1norm
|
static limb_t mp_div1norm(limb_t *tabr, const limb_t *taba, limb_t n,
limb_t b, limb_t r)
{
slimb_t i;
if (n >= UDIV1NORM_THRESHOLD) {
limb_t b_inv;
b_inv = udiv1norm_init(b);
for(i = n - 1; i >= 0; i--) {
tabr[i] = udiv1norm(&r, r, taba[i], b, b_inv);
}
} else {
dlimb_t a1;
for(i = n - 1; i >= 0; i--) {
a1 = ((dlimb_t)r << LIMB_BITS) | taba[i];
tabr[i] = a1 / b;
r = a1 % b;
}
}
return r;
}
|
/* b must be >= 1 << (LIMB_BITS - 1) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1245-L1265
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_divnorm
|
static int mp_divnorm(bf_context_t *s, limb_t *tabq, limb_t *taba, limb_t na,
const limb_t *tabb, limb_t nb)
{
limb_t r, a, c, q, v, b1, b1_inv, n, dummy_r;
slimb_t i, j;
b1 = tabb[nb - 1];
if (nb == 1) {
taba[0] = mp_div1norm(tabq, taba, na, b1, 0);
return 0;
}
n = na - nb;
if (bf_min(n, nb) >= DIVNORM_LARGE_THRESHOLD) {
return mp_divnorm_large(s, tabq, taba, na, tabb, nb);
}
if (n >= UDIV1NORM_THRESHOLD)
b1_inv = udiv1norm_init(b1);
else
b1_inv = 0;
/* first iteration: the quotient is only 0 or 1 */
q = 1;
for(j = nb - 1; j >= 0; j--) {
if (taba[n + j] != tabb[j]) {
if (taba[n + j] < tabb[j])
q = 0;
break;
}
}
tabq[n] = q;
if (q) {
mp_sub(taba + n, taba + n, tabb, nb, 0);
}
for(i = n - 1; i >= 0; i--) {
if (unlikely(taba[i + nb] >= b1)) {
q = -1;
} else if (b1_inv) {
q = udiv1norm(&dummy_r, taba[i + nb], taba[i + nb - 1], b1, b1_inv);
} else {
dlimb_t al;
al = ((dlimb_t)taba[i + nb] << LIMB_BITS) | taba[i + nb - 1];
q = al / b1;
r = al % b1;
}
r = mp_sub_mul1(taba + i, tabb, nb, q);
v = taba[i + nb];
a = v - r;
c = (a > v);
taba[i + nb] = a;
if (c != 0) {
/* negative result */
for(;;) {
q--;
c = mp_add(taba + i, taba + i, tabb, nb, 0);
/* propagate carry and test if positive result */
if (c != 0) {
if (++taba[i + nb] == 0) {
break;
}
}
}
}
tabq[i] = q;
}
return 0;
}
|
/* base case division: divides taba[0..na-1] by tabb[0..nb-1]. tabb[nb
- 1] must be >= 1 << (LIMB_BITS - 1). na - nb must be >= 0. 'taba'
is modified and contains the remainder (nb limbs). tabq[0..na-nb]
contains the quotient with tabq[na - nb] <= 1. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1275-L1344
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_recip
|
int mp_recip(bf_context_t *s, limb_t *tabr, const limb_t *taba, limb_t n)
{
mp_size_t l, h, k, i;
limb_t *tabxh, *tabt, c, *tabu;
if (n <= 2) {
/* return ceil(B^(2*n)/a) - 1 */
/* XXX: could avoid allocation */
tabu = bf_malloc(s, sizeof(limb_t) * (2 * n + 1));
tabt = bf_malloc(s, sizeof(limb_t) * (n + 2));
if (!tabt || !tabu)
goto fail;
for(i = 0; i < 2 * n; i++)
tabu[i] = 0;
tabu[2 * n] = 1;
if (mp_divnorm(s, tabt, tabu, 2 * n + 1, taba, n))
goto fail;
for(i = 0; i < n + 1; i++)
tabr[i] = tabt[i];
if (mp_scan_nz(tabu, n) == 0) {
/* only happens for a=B^n/2 */
mp_sub_ui(tabr, 1, n + 1);
}
} else {
l = (n - 1) / 2;
h = n - l;
/* n=2p -> l=p-1, h = p + 1, k = p + 3
n=2p+1-> l=p, h = p + 1; k = p + 2
*/
tabt = bf_malloc(s, sizeof(limb_t) * (n + h + 1));
tabu = bf_malloc(s, sizeof(limb_t) * (n + 2 * h - l + 2));
if (!tabt || !tabu)
goto fail;
tabxh = tabr + l;
if (mp_recip(s, tabxh, taba + l, h))
goto fail;
if (mp_mul(s, tabt, taba, n, tabxh, h + 1)) /* n + h + 1 limbs */
goto fail;
while (tabt[n + h] != 0) {
mp_sub_ui(tabxh, 1, h + 1);
c = mp_sub(tabt, tabt, taba, n, 0);
mp_sub_ui(tabt + n, c, h + 1);
}
/* T = B^(n+h) - T */
mp_neg(tabt, tabt, n + h + 1, 0);
tabt[n + h]++;
if (mp_mul(s, tabu, tabt + l, n + h + 1 - l, tabxh, h + 1))
goto fail;
/* n + 2*h - l + 2 limbs */
k = 2 * h - l;
for(i = 0; i < l; i++)
tabr[i] = tabu[i + k];
mp_add(tabr + l, tabr + l, tabu + 2 * h, h, 0);
}
bf_free(s, tabt);
bf_free(s, tabu);
return 0;
fail:
bf_free(s, tabt);
bf_free(s, tabu);
return -1;
}
|
/* compute r=B^(2*n)/a such as a*r < B^(2*n) < a*r + 2 with n >= 1. 'a'
has n limbs with a[n-1] >= B/2 and 'r' has n+1 limbs with r[n] = 1.
See Modern Computer Arithmetic by Richard P. Brent and Paul
Zimmermann, algorithm 3.5 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1351-L1412
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_cmp
|
static int mp_cmp(const limb_t *taba, const limb_t *tabb, mp_size_t n)
{
mp_size_t i;
for(i = n - 1; i >= 0; i--) {
if (taba[i] != tabb[i]) {
if (taba[i] < tabb[i])
return -1;
else
return 1;
}
}
return 0;
}
|
/* return -1, 0 or 1 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1415-L1427
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_divnorm_large
|
static int mp_divnorm_large(bf_context_t *s,
limb_t *tabq, limb_t *taba, limb_t na,
const limb_t *tabb, limb_t nb)
{
limb_t *tabb_inv, nq, *tabt, i, n;
nq = na - nb;
#ifdef DEBUG_DIVNORM_LARGE
printf("na=%d nb=%d nq=%d\n", (int)na, (int)nb, (int)nq);
mp_print_str("a", taba, na);
mp_print_str("b", tabb, nb);
#endif
assert(nq >= 1);
n = nq;
if (nq < nb)
n++;
tabb_inv = bf_malloc(s, sizeof(limb_t) * (n + 1));
tabt = bf_malloc(s, sizeof(limb_t) * 2 * (n + 1));
if (!tabb_inv || !tabt)
goto fail;
if (n >= nb) {
for(i = 0; i < n - nb; i++)
tabt[i] = 0;
for(i = 0; i < nb; i++)
tabt[i + n - nb] = tabb[i];
} else {
/* truncate B: need to increment it so that the approximate
inverse is smaller that the exact inverse */
for(i = 0; i < n; i++)
tabt[i] = tabb[i + nb - n];
if (mp_add_ui(tabt, 1, n)) {
/* tabt = B^n : tabb_inv = B^n */
memset(tabb_inv, 0, n * sizeof(limb_t));
tabb_inv[n] = 1;
goto recip_done;
}
}
if (mp_recip(s, tabb_inv, tabt, n))
goto fail;
recip_done:
/* Q=A*B^-1 */
if (mp_mul(s, tabt, tabb_inv, n + 1, taba + na - (n + 1), n + 1))
goto fail;
for(i = 0; i < nq + 1; i++)
tabq[i] = tabt[i + 2 * (n + 1) - (nq + 1)];
#ifdef DEBUG_DIVNORM_LARGE
mp_print_str("q", tabq, nq + 1);
#endif
bf_free(s, tabt);
bf_free(s, tabb_inv);
tabb_inv = NULL;
/* R=A-B*Q */
tabt = bf_malloc(s, sizeof(limb_t) * (na + 1));
if (!tabt)
goto fail;
if (mp_mul(s, tabt, tabq, nq + 1, tabb, nb))
goto fail;
/* we add one more limb for the result */
mp_sub(taba, taba, tabt, nb + 1, 0);
bf_free(s, tabt);
/* the approximated quotient is smaller than than the exact one,
hence we may have to increment it */
#ifdef DEBUG_DIVNORM_LARGE2
int cnt = 0;
static int cnt_max;
#endif
for(;;) {
if (taba[nb] == 0 && mp_cmp(taba, tabb, nb) < 0)
break;
taba[nb] -= mp_sub(taba, taba, tabb, nb, 0);
mp_add_ui(tabq, 1, nq + 1);
#ifdef DEBUG_DIVNORM_LARGE2
cnt++;
#endif
}
#ifdef DEBUG_DIVNORM_LARGE2
if (cnt > cnt_max) {
cnt_max = cnt;
printf("\ncnt=%d nq=%d nb=%d\n", cnt_max, (int)nq, (int)nb);
}
#endif
return 0;
fail:
bf_free(s, tabb_inv);
bf_free(s, tabt);
return -1;
}
|
//#define DEBUG_DIVNORM_LARGE
//#define DEBUG_DIVNORM_LARGE2
/* subquadratic divnorm */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1433-L1522
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_mul_2exp
|
int bf_mul_2exp(bf_t *r, slimb_t e, limb_t prec, bf_flags_t flags)
{
slimb_t e_max;
if (r->len == 0)
return 0;
e_max = ((limb_t)1 << BF_EXT_EXP_BITS_MAX) - 1;
e = bf_max(e, -e_max);
e = bf_min(e, e_max);
r->expn += e;
return __bf_round(r, prec, flags, r->len, 0);
}
|
/* multiply 'r' by 2^e */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1606-L1616
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_get_exp_min
|
slimb_t bf_get_exp_min(const bf_t *a)
{
slimb_t i;
limb_t v;
int k;
for(i = 0; i < a->len; i++) {
v = a->tab[i];
if (v != 0) {
k = ctz(v);
return a->expn - (a->len - i) * LIMB_BITS + k;
}
}
return 0;
}
|
/* Return e such as a=m*2^e with m odd integer. return 0 if a is zero,
Infinite or Nan. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1620-L1634
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_tdivremu
|
static void bf_tdivremu(bf_t *q, bf_t *r,
const bf_t *a, const bf_t *b)
{
if (bf_cmpu(a, b) < 0) {
bf_set_ui(q, 0);
bf_set(r, a);
} else {
bf_div(q, a, b, bf_max(a->expn - b->expn + 1, 2), BF_RNDZ);
bf_rint(q, BF_RNDZ);
bf_mul(r, q, b, BF_PREC_INF, BF_RNDZ);
bf_sub(r, a, r, BF_PREC_INF, BF_RNDZ);
}
}
|
/* a and b must be finite numbers with a >= 0 and b > 0. 'q' is the
integer defined as floor(a/b) and r = a - q * b. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1638-L1650
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_divrem
|
int bf_divrem(bf_t *q, bf_t *r, const bf_t *a, const bf_t *b,
limb_t prec, bf_flags_t flags, int rnd_mode)
{
bf_t a1_s, *a1 = &a1_s;
bf_t b1_s, *b1 = &b1_s;
int q_sign, ret;
BOOL is_ceil, is_rndn;
assert(q != a && q != b);
assert(r != a && r != b);
assert(q != r);
if (a->len == 0 || b->len == 0) {
bf_set_zero(q, 0);
if (a->expn == BF_EXP_NAN || b->expn == BF_EXP_NAN) {
bf_set_nan(r);
return 0;
} else if (a->expn == BF_EXP_INF || b->expn == BF_EXP_ZERO) {
bf_set_nan(r);
return BF_ST_INVALID_OP;
} else {
bf_set(r, a);
return bf_round(r, prec, flags);
}
}
q_sign = a->sign ^ b->sign;
is_rndn = (rnd_mode == BF_RNDN || rnd_mode == BF_RNDNA);
switch(rnd_mode) {
default:
case BF_RNDZ:
case BF_RNDN:
case BF_RNDNA:
is_ceil = FALSE;
break;
case BF_RNDD:
is_ceil = q_sign;
break;
case BF_RNDU:
is_ceil = q_sign ^ 1;
break;
case BF_RNDA:
is_ceil = TRUE;
break;
case BF_DIVREM_EUCLIDIAN:
is_ceil = a->sign;
break;
}
a1->expn = a->expn;
a1->tab = a->tab;
a1->len = a->len;
a1->sign = 0;
b1->expn = b->expn;
b1->tab = b->tab;
b1->len = b->len;
b1->sign = 0;
/* XXX: could improve to avoid having a large 'q' */
bf_tdivremu(q, r, a1, b1);
if (bf_is_nan(q) || bf_is_nan(r))
goto fail;
if (r->len != 0) {
if (is_rndn) {
int res;
b1->expn--;
res = bf_cmpu(r, b1);
b1->expn++;
if (res > 0 ||
(res == 0 &&
(rnd_mode == BF_RNDNA ||
get_bit(q->tab, q->len, q->len * LIMB_BITS - q->expn)))) {
goto do_sub_r;
}
} else if (is_ceil) {
do_sub_r:
ret = bf_add_si(q, q, 1, BF_PREC_INF, BF_RNDZ);
ret |= bf_sub(r, r, b1, BF_PREC_INF, BF_RNDZ);
if (ret & BF_ST_MEM_ERROR)
goto fail;
}
}
r->sign ^= a->sign;
q->sign = q_sign;
return bf_round(r, prec, flags);
fail:
bf_set_nan(q);
bf_set_nan(r);
return BF_ST_MEM_ERROR;
}
|
/* division and remainder.
rnd_mode is the rounding mode for the quotient. The additional
rounding mode BF_RND_EUCLIDIAN is supported.
'q' is an integer. 'r' is rounded with prec and flags (prec can be
BF_PREC_INF).
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1732-L1824
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_sqrtrem1
|
static limb_t mp_sqrtrem1(limb_t *pr, limb_t a)
{
limb_t s1, r1, s, r, q, u, num;
/* use a table for the 16 -> 8 bit sqrt */
s1 = sqrt_table[(a >> (LIMB_BITS - 8)) - 64];
r1 = (a >> (LIMB_BITS - 16)) - s1 * s1;
if (r1 > 2 * s1) {
r1 -= 2 * s1 + 1;
s1++;
}
/* one iteration to get a 32 -> 16 bit sqrt */
num = (r1 << 8) | ((a >> (LIMB_BITS - 32 + 8)) & 0xff);
q = num / (2 * s1); /* q <= 2^8 */
u = num % (2 * s1);
s = (s1 << 8) + q;
r = (u << 8) | ((a >> (LIMB_BITS - 32)) & 0xff);
r -= q * q;
if ((slimb_t)r < 0) {
s--;
r += 2 * s + 1;
}
#if LIMB_BITS == 64
s1 = s;
r1 = r;
/* one more iteration for 64 -> 32 bit sqrt */
num = (r1 << 16) | ((a >> (LIMB_BITS - 64 + 16)) & 0xffff);
q = num / (2 * s1); /* q <= 2^16 */
u = num % (2 * s1);
s = (s1 << 16) + q;
r = (u << 16) | ((a >> (LIMB_BITS - 64)) & 0xffff);
r -= q * q;
if ((slimb_t)r < 0) {
s--;
r += 2 * s + 1;
}
#endif
*pr = r;
return s;
}
|
/* a >= 2^(LIMB_BITS - 2). Return (s, r) with s=floor(sqrt(a)) and
r=a-s^2. 0 <= r <= 2 * s */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1887-L1928
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_isqrt
|
limb_t bf_isqrt(limb_t a)
{
limb_t s, r;
int k;
if (a == 0)
return 0;
k = clz(a) & ~1;
s = mp_sqrtrem1(&r, a << k);
s >>= (k >> 1);
return s;
}
|
/* return floor(sqrt(a)) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1931-L1942
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_sqrtrem_rec
|
static int mp_sqrtrem_rec(bf_context_t *s, limb_t *tabs, limb_t *taba, limb_t n,
limb_t *tmp_buf, limb_t *prh)
{
limb_t l, h, rh, ql, qh, c, i;
if (n == 1) {
*prh = mp_sqrtrem2(tabs, taba);
return 0;
}
#ifdef DEBUG_SQRTREM
mp_print_str("a", taba, 2 * n);
#endif
l = n / 2;
h = n - l;
if (mp_sqrtrem_rec(s, tabs + l, taba + 2 * l, h, tmp_buf, &qh))
return -1;
#ifdef DEBUG_SQRTREM
mp_print_str("s1", tabs + l, h);
mp_print_str_h("r1", taba + 2 * l, h, qh);
mp_print_str_h("r2", taba + l, n, qh);
#endif
/* the remainder is in taba + 2 * l. Its high bit is in qh */
if (qh) {
mp_sub(taba + 2 * l, taba + 2 * l, tabs + l, h, 0);
}
/* instead of dividing by 2*s, divide by s (which is normalized)
and update q and r */
if (mp_divnorm(s, tmp_buf, taba + l, n, tabs + l, h))
return -1;
qh += tmp_buf[l];
for(i = 0; i < l; i++)
tabs[i] = tmp_buf[i];
ql = mp_shr(tabs, tabs, l, 1, qh & 1);
qh = qh >> 1; /* 0 or 1 */
if (ql)
rh = mp_add(taba + l, taba + l, tabs + l, h, 0);
else
rh = 0;
#ifdef DEBUG_SQRTREM
mp_print_str_h("q", tabs, l, qh);
mp_print_str_h("u", taba + l, h, rh);
#endif
mp_add_ui(tabs + l, qh, h);
#ifdef DEBUG_SQRTREM
mp_print_str_h("s2", tabs, n, sh);
#endif
/* q = qh, tabs[l - 1 ... 0], r = taba[n - 1 ... l] */
/* subtract q^2. if qh = 1 then q = B^l, so we can take shortcuts */
if (qh) {
c = qh;
} else {
if (mp_mul(s, taba + n, tabs, l, tabs, l))
return -1;
c = mp_sub(taba, taba, taba + n, 2 * l, 0);
}
rh -= mp_sub_ui(taba + 2 * l, c, n - 2 * l);
if ((slimb_t)rh < 0) {
mp_sub_ui(tabs, 1, n);
rh += mp_add_mul1(taba, tabs, n, 2);
rh += mp_add_ui(taba, 1, n);
}
*prh = rh;
return 0;
}
|
//#define DEBUG_SQRTREM
/* tmp_buf must contain (n / 2 + 1 limbs). *prh contains the highest
limb of the remainder. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L1976-L2042
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_sqrtrem
|
int mp_sqrtrem(bf_context_t *s, limb_t *tabs, limb_t *taba, limb_t n)
{
limb_t tmp_buf1[8];
limb_t *tmp_buf;
mp_size_t n2;
int ret;
n2 = n / 2 + 1;
if (n2 <= countof(tmp_buf1)) {
tmp_buf = tmp_buf1;
} else {
tmp_buf = bf_malloc(s, sizeof(limb_t) * n2);
if (!tmp_buf)
return -1;
}
ret = mp_sqrtrem_rec(s, tabs, taba, n, tmp_buf, taba + n);
if (tmp_buf != tmp_buf1)
bf_free(s, tmp_buf);
return ret;
}
|
/* 'taba' has 2*n limbs with n >= 1 and taba[2*n-1] >= 2 ^ (LIMB_BITS
- 2). Return (s, r) with s=floor(sqrt(a)) and r=a-s^2. 0 <= r <= 2
* s. tabs has n limbs. r is returned in the lower n limbs of
taba. Its r[n] is the returned value of the function. */
/* Algorithm from the article "Karatsuba Square Root" by Paul Zimmermann and
inspirated from its GMP implementation */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2050-L2068
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_sqrtrem
|
int bf_sqrtrem(bf_t *r, bf_t *rem1, const bf_t *a)
{
int ret;
if (a->len == 0) {
if (a->expn == BF_EXP_NAN) {
bf_set_nan(r);
} else if (a->expn == BF_EXP_INF && a->sign) {
goto invalid_op;
} else {
bf_set(r, a);
}
if (rem1)
bf_set_ui(rem1, 0);
ret = 0;
} else if (a->sign) {
invalid_op:
bf_set_nan(r);
if (rem1)
bf_set_ui(rem1, 0);
ret = BF_ST_INVALID_OP;
} else {
bf_t rem_s, *rem;
bf_sqrt(r, a, (a->expn + 1) / 2, BF_RNDZ);
bf_rint(r, BF_RNDZ);
/* see if the result is exact by computing the remainder */
if (rem1) {
rem = rem1;
} else {
rem = &rem_s;
bf_init(r->ctx, rem);
}
/* XXX: could avoid recomputing the remainder */
bf_mul(rem, r, r, BF_PREC_INF, BF_RNDZ);
bf_neg(rem);
bf_add(rem, rem, a, BF_PREC_INF, BF_RNDZ);
if (bf_is_nan(rem)) {
ret = BF_ST_MEM_ERROR;
goto done;
}
if (rem->len != 0) {
ret = BF_ST_INEXACT;
} else {
ret = 0;
}
done:
if (!rem1)
bf_delete(rem);
}
return ret;
}
|
/* Integer square root with remainder. 'a' must be an integer. r =
floor(sqrt(a)) and rem = a - r^2. BF_ST_INEXACT is set if the result
is inexact. 'rem' can be NULL if the remainder is not needed. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2073-L2124
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_rint
|
int bf_rint(bf_t *r, int rnd_mode)
{
return bf_round(r, 0, rnd_mode | BF_FLAG_RADPNT_PREC);
}
|
/* convert to integer (infinite precision) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2299-L2302
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_logic_or
|
int bf_logic_or(bf_t *r, const bf_t *a, const bf_t *b)
{
return bf_logic_op(r, a, b, BF_LOGIC_OR);
}
|
/* 'a' and 'b' must be integers. Return 0 or BF_ST_MEM_ERROR. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2409-L2412
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_logic_xor
|
int bf_logic_xor(bf_t *r, const bf_t *a, const bf_t *b)
{
return bf_logic_op(r, a, b, BF_LOGIC_XOR);
}
|
/* 'a' and 'b' must be integers. Return 0 or BF_ST_MEM_ERROR. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2415-L2418
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_logic_and
|
int bf_logic_and(bf_t *r, const bf_t *a, const bf_t *b)
{
return bf_logic_op(r, a, b, BF_LOGIC_AND);
}
|
/* 'a' and 'b' must be integers. Return 0 or BF_ST_MEM_ERROR. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2421-L2424
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_get_int32
|
int bf_get_int32(int *pres, const bf_t *a, int flags)
{
uint32_t v;
int ret;
if (a->expn >= BF_EXP_INF) {
ret = BF_ST_INVALID_OP;
if (flags & BF_GET_INT_MOD) {
v = 0;
} else if (a->expn == BF_EXP_INF) {
v = (uint32_t)INT32_MAX + a->sign;
} else {
v = INT32_MAX;
}
} else if (a->expn <= 0) {
v = 0;
ret = 0;
} else if (a->expn <= 31) {
v = a->tab[a->len - 1] >> (LIMB_BITS - a->expn);
if (a->sign)
v = -v;
ret = 0;
} else if (!(flags & BF_GET_INT_MOD)) {
ret = BF_ST_INVALID_OP;
if (a->sign) {
v = (uint32_t)INT32_MAX + 1;
if (a->expn == 32 &&
(a->tab[a->len - 1] >> (LIMB_BITS - 32)) == v) {
ret = 0;
}
} else {
v = INT32_MAX;
}
} else {
v = get_bits(a->tab, a->len, a->len * LIMB_BITS - a->expn);
if (a->sign)
v = -v;
ret = 0;
}
*pres = v;
return ret;
}
|
/* The rounding mode is always BF_RNDZ. Return BF_ST_INVALID_OP if there
is an overflow and 0 otherwise. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2533-L2573
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_get_int64
|
int bf_get_int64(int64_t *pres, const bf_t *a, int flags)
{
uint64_t v;
int ret;
if (a->expn >= BF_EXP_INF) {
ret = BF_ST_INVALID_OP;
if (flags & BF_GET_INT_MOD) {
v = 0;
} else if (a->expn == BF_EXP_INF) {
v = (uint64_t)INT64_MAX + a->sign;
} else {
v = INT64_MAX;
}
} else if (a->expn <= 0) {
v = 0;
ret = 0;
} else if (a->expn <= 63) {
#if LIMB_BITS == 32
if (a->expn <= 32)
v = a->tab[a->len - 1] >> (LIMB_BITS - a->expn);
else
v = (((uint64_t)a->tab[a->len - 1] << 32) |
get_limbz(a, a->len - 2)) >> (64 - a->expn);
#else
v = a->tab[a->len - 1] >> (LIMB_BITS - a->expn);
#endif
if (a->sign)
v = -v;
ret = 0;
} else if (!(flags & BF_GET_INT_MOD)) {
ret = BF_ST_INVALID_OP;
if (a->sign) {
uint64_t v1;
v = (uint64_t)INT64_MAX + 1;
if (a->expn == 64) {
v1 = a->tab[a->len - 1];
#if LIMB_BITS == 32
v1 = (v1 << 32) | get_limbz(a, a->len - 2);
#endif
if (v1 == v)
ret = 0;
}
} else {
v = INT64_MAX;
}
} else {
slimb_t bit_pos = a->len * LIMB_BITS - a->expn;
v = get_bits(a->tab, a->len, bit_pos);
#if LIMB_BITS == 32
v |= (uint64_t)get_bits(a->tab, a->len, bit_pos + 32) << 32;
#endif
if (a->sign)
v = -v;
ret = 0;
}
*pres = v;
return ret;
}
|
/* The rounding mode is always BF_RNDZ. Return BF_ST_INVALID_OP if there
is an overflow and 0 otherwise. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2577-L2634
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_get_uint64
|
int bf_get_uint64(uint64_t *pres, const bf_t *a)
{
uint64_t v;
int ret;
if (a->expn == BF_EXP_NAN) {
goto overflow;
} else if (a->expn <= 0) {
v = 0;
ret = 0;
} else if (a->sign) {
v = 0;
ret = BF_ST_INVALID_OP;
} else if (a->expn <= 64) {
#if LIMB_BITS == 32
if (a->expn <= 32)
v = a->tab[a->len - 1] >> (LIMB_BITS - a->expn);
else
v = (((uint64_t)a->tab[a->len - 1] << 32) |
get_limbz(a, a->len - 2)) >> (64 - a->expn);
#else
v = a->tab[a->len - 1] >> (LIMB_BITS - a->expn);
#endif
ret = 0;
} else {
overflow:
v = UINT64_MAX;
ret = BF_ST_INVALID_OP;
}
*pres = v;
return ret;
}
|
/* The rounding mode is always BF_RNDZ. Return BF_ST_INVALID_OP if there
is an overflow and 0 otherwise. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2638-L2668
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_integer_from_radix_rec
|
static int bf_integer_from_radix_rec(bf_t *r, const limb_t *tab,
limb_t n, int level, limb_t n0,
limb_t radix, bf_t *pow_tab)
{
int ret;
if (n == 1) {
ret = bf_set_ui(r, tab[0]);
} else {
bf_t T_s, *T = &T_s, *B;
limb_t n1, n2;
n2 = (((n0 * 2) >> (level + 1)) + 1) / 2;
n1 = n - n2;
// printf("level=%d n0=%ld n1=%ld n2=%ld\n", level, n0, n1, n2);
B = &pow_tab[level];
if (B->len == 0) {
ret = bf_pow_ui_ui(B, radix, n2, BF_PREC_INF, BF_RNDZ);
if (ret)
return ret;
}
ret = bf_integer_from_radix_rec(r, tab + n2, n1, level + 1, n0,
radix, pow_tab);
if (ret)
return ret;
ret = bf_mul(r, r, B, BF_PREC_INF, BF_RNDZ);
if (ret)
return ret;
bf_init(r->ctx, T);
ret = bf_integer_from_radix_rec(T, tab, n2, level + 1, n0,
radix, pow_tab);
if (!ret)
ret = bf_add(r, r, T, BF_PREC_INF, BF_RNDZ);
bf_delete(T);
}
return ret;
// bf_print_str(" r=", r);
}
|
/* return != 0 if error */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2693-L2729
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_integer_from_radix
|
static int bf_integer_from_radix(bf_t *r, const limb_t *tab,
limb_t n, limb_t radix)
{
bf_context_t *s = r->ctx;
int pow_tab_len, i, ret;
limb_t radixl;
bf_t *pow_tab;
radixl = get_limb_radix(radix);
pow_tab_len = ceil_log2(n) + 2; /* XXX: check */
pow_tab = bf_malloc(s, sizeof(pow_tab[0]) * pow_tab_len);
if (!pow_tab)
return -1;
for(i = 0; i < pow_tab_len; i++)
bf_init(r->ctx, &pow_tab[i]);
ret = bf_integer_from_radix_rec(r, tab, n, 0, n, radixl, pow_tab);
for(i = 0; i < pow_tab_len; i++) {
bf_delete(&pow_tab[i]);
}
bf_free(s, pow_tab);
return ret;
}
|
/* return 0 if OK != 0 if memory error */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2732-L2753
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_mul_pow_radix
|
int bf_mul_pow_radix(bf_t *r, const bf_t *T, limb_t radix,
slimb_t expn, limb_t prec, bf_flags_t flags)
{
int ret, expn_sign, overflow;
slimb_t e, extra_bits, prec1, ziv_extra_bits;
bf_t B_s, *B = &B_s;
if (T->len == 0) {
return bf_set(r, T);
} else if (expn == 0) {
ret = bf_set(r, T);
ret |= bf_round(r, prec, flags);
return ret;
}
e = expn;
expn_sign = 0;
if (e < 0) {
e = -e;
expn_sign = 1;
}
bf_init(r->ctx, B);
if (prec == BF_PREC_INF) {
/* infinite precision: only used if the result is known to be exact */
ret = bf_pow_ui_ui(B, radix, e, BF_PREC_INF, BF_RNDN);
if (expn_sign) {
ret |= bf_div(r, T, B, T->len * LIMB_BITS, BF_RNDN);
} else {
ret |= bf_mul(r, T, B, BF_PREC_INF, BF_RNDN);
}
} else {
ziv_extra_bits = 16;
for(;;) {
prec1 = prec + ziv_extra_bits;
/* XXX: correct overflow/underflow handling */
/* XXX: rigorous error analysis needed */
extra_bits = ceil_log2(e) * 2 + 1;
ret = bf_pow_ui_ui(B, radix, e, prec1 + extra_bits, BF_RNDN | BF_FLAG_EXT_EXP);
overflow = !bf_is_finite(B);
/* XXX: if bf_pow_ui_ui returns an exact result, can stop
after the next operation */
if (expn_sign)
ret |= bf_div(r, T, B, prec1 + extra_bits, BF_RNDN | BF_FLAG_EXT_EXP);
else
ret |= bf_mul(r, T, B, prec1 + extra_bits, BF_RNDN | BF_FLAG_EXT_EXP);
if (ret & BF_ST_MEM_ERROR)
break;
if ((ret & BF_ST_INEXACT) &&
!bf_can_round(r, prec, flags & BF_RND_MASK, prec1) &&
!overflow) {
/* and more precision and retry */
ziv_extra_bits = ziv_extra_bits + (ziv_extra_bits / 2);
} else {
/* XXX: need to use __bf_round() to pass the inexact
flag for the subnormal case */
ret = bf_round(r, prec, flags) | (ret & BF_ST_INEXACT);
break;
}
}
}
bf_delete(B);
return ret;
}
|
/* compute and round T * radix^expn. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2756-L2818
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_add_limb
|
static int bf_add_limb(bf_t *a, slimb_t *ppos, limb_t v)
{
slimb_t pos;
pos = *ppos;
if (unlikely(pos < 0)) {
limb_t new_size, d, *new_tab;
new_size = bf_max(a->len + 1, a->len * 3 / 2);
new_tab = bf_realloc(a->ctx, a->tab, sizeof(limb_t) * new_size);
if (!new_tab)
return -1;
a->tab = new_tab;
d = new_size - a->len;
memmove(a->tab + d, a->tab, a->len * sizeof(limb_t));
a->len = new_size;
pos += d;
}
a->tab[pos--] = v;
*ppos = pos;
return 0;
}
|
/* add a limb at 'pos' and decrement pos. new space is created if
needed. Return 0 if OK, -1 if memory error */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L2834-L2853
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_atof2
|
int bf_atof2(bf_t *r, slimb_t *pexponent,
const char *str, const char **pnext, int radix,
limb_t prec, bf_flags_t flags)
{
return bf_atof_internal(r, pexponent, str, pnext, radix, prec, flags,
FALSE);
}
|
/*
Return (status, n, exp). 'status' is the floating point status. 'n'
is the parsed number.
If (flags & BF_ATOF_EXPONENT) and if the radix is not a power of
two, the parsed number is equal to r *
(*pexponent)^radix. Otherwise *pexponent = 0.
*/
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L3137-L3143
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_mul_log2_radix
|
slimb_t bf_mul_log2_radix(slimb_t a1, unsigned int radix, int is_inv,
int is_ceil1)
{
int is_neg;
limb_t a;
BOOL is_ceil;
is_ceil = is_ceil1;
a = a1;
if (a1 < 0) {
a = -a;
is_neg = 1;
} else {
is_neg = 0;
}
is_ceil ^= is_neg;
if ((radix & (radix - 1)) == 0) {
int radix_bits;
/* radix is a power of two */
radix_bits = ceil_log2(radix);
if (is_inv) {
if (is_ceil)
a += radix_bits - 1;
a = a / radix_bits;
} else {
a = a * radix_bits;
}
} else {
const uint32_t *tab;
limb_t b0, b1;
dlimb_t t;
if (is_inv) {
tab = inv_log2_radix[radix - 2];
#if LIMB_BITS == 32
b1 = tab[0];
b0 = tab[1];
#else
b1 = ((limb_t)tab[0] << 32) | tab[1];
b0 = (limb_t)tab[2] << 32;
#endif
t = (dlimb_t)b0 * (dlimb_t)a;
t = (dlimb_t)b1 * (dlimb_t)a + (t >> LIMB_BITS);
a = t >> (LIMB_BITS - 1);
} else {
b0 = log2_radix[radix - 2];
t = (dlimb_t)b0 * (dlimb_t)a;
a = t >> (LIMB_BITS - 3);
}
/* a = floor(result) and 'result' cannot be an integer */
a += is_ceil;
}
if (is_neg)
a = -a;
return a;
}
|
/* compute floor(a*b) or ceil(a*b) with b = log2(radix) or
b=1/log2(radix). For is_inv = 0, strict accuracy is not guaranteed
when radix is not a power of two. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L3315-L3370
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_integer_to_radix_rec
|
static int bf_integer_to_radix_rec(bf_t *pow_tab,
limb_t *out, const bf_t *a, limb_t n,
int level, limb_t n0, limb_t radixl,
unsigned int radixl_bits)
{
limb_t n1, n2, q_prec;
int ret;
assert(n >= 1);
if (n == 1) {
out[0] = get_bits(a->tab, a->len, a->len * LIMB_BITS - a->expn);
} else if (n == 2) {
dlimb_t t;
slimb_t pos;
pos = a->len * LIMB_BITS - a->expn;
t = ((dlimb_t)get_bits(a->tab, a->len, pos + LIMB_BITS) << LIMB_BITS) |
get_bits(a->tab, a->len, pos);
if (likely(radixl == RADIXL_10)) {
/* use division by a constant when possible */
out[0] = t % RADIXL_10;
out[1] = t / RADIXL_10;
} else {
out[0] = t % radixl;
out[1] = t / radixl;
}
} else {
bf_t Q, R, *B, *B_inv;
int q_add;
bf_init(a->ctx, &Q);
bf_init(a->ctx, &R);
n2 = (((n0 * 2) >> (level + 1)) + 1) / 2;
n1 = n - n2;
B = &pow_tab[2 * level];
B_inv = &pow_tab[2 * level + 1];
ret = 0;
if (B->len == 0) {
/* compute BASE^n2 */
ret |= bf_pow_ui_ui(B, radixl, n2, BF_PREC_INF, BF_RNDZ);
/* we use enough bits for the maximum possible 'n1' value,
i.e. n2 + 1 */
ret |= bf_set_ui(&R, 1);
ret |= bf_div(B_inv, &R, B, (n2 + 1) * radixl_bits + 2, BF_RNDN);
}
// printf("%d: n1=% " PRId64 " n2=%" PRId64 "\n", level, n1, n2);
q_prec = n1 * radixl_bits;
ret |= bf_mul(&Q, a, B_inv, q_prec, BF_RNDN);
ret |= bf_rint(&Q, BF_RNDZ);
ret |= bf_mul(&R, &Q, B, BF_PREC_INF, BF_RNDZ);
ret |= bf_sub(&R, a, &R, BF_PREC_INF, BF_RNDZ);
if (ret & BF_ST_MEM_ERROR)
goto fail;
/* adjust if necessary */
q_add = 0;
while (R.sign && R.len != 0) {
if (bf_add(&R, &R, B, BF_PREC_INF, BF_RNDZ))
goto fail;
q_add--;
}
while (bf_cmpu(&R, B) >= 0) {
if (bf_sub(&R, &R, B, BF_PREC_INF, BF_RNDZ))
goto fail;
q_add++;
}
if (q_add != 0) {
if (bf_add_si(&Q, &Q, q_add, BF_PREC_INF, BF_RNDZ))
goto fail;
}
if (bf_integer_to_radix_rec(pow_tab, out + n2, &Q, n1, level + 1, n0,
radixl, radixl_bits))
goto fail;
if (bf_integer_to_radix_rec(pow_tab, out, &R, n2, level + 1, n0,
radixl, radixl_bits)) {
fail:
bf_delete(&Q);
bf_delete(&R);
return -1;
}
bf_delete(&Q);
bf_delete(&R);
}
return 0;
}
|
/* 'n' is the number of output limbs */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L3373-L3456
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_integer_to_radix
|
static int bf_integer_to_radix(bf_t *r, const bf_t *a, limb_t radixl)
{
bf_context_t *s = r->ctx;
limb_t r_len;
bf_t *pow_tab;
int i, pow_tab_len, ret;
r_len = r->len;
pow_tab_len = (ceil_log2(r_len) + 2) * 2; /* XXX: check */
pow_tab = bf_malloc(s, sizeof(pow_tab[0]) * pow_tab_len);
if (!pow_tab)
return -1;
for(i = 0; i < pow_tab_len; i++)
bf_init(r->ctx, &pow_tab[i]);
ret = bf_integer_to_radix_rec(pow_tab, r->tab, a, r_len, 0, r_len, radixl,
ceil_log2(radixl));
for(i = 0; i < pow_tab_len; i++) {
bf_delete(&pow_tab[i]);
}
bf_free(s, pow_tab);
return ret;
}
|
/* return 0 if OK != 0 if memory error */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L3459-L3482
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_convert_to_radix
|
static int bf_convert_to_radix(bf_t *r, slimb_t *pE,
const bf_t *a, int radix,
limb_t P, bf_rnd_t rnd_mode,
BOOL is_fixed_exponent)
{
slimb_t E, e, prec, extra_bits, ziv_extra_bits, prec0;
bf_t B_s, *B = &B_s;
int e_sign, ret, res;
if (a->len == 0) {
/* zero case */
*pE = 0;
return bf_set(r, a);
}
if (is_fixed_exponent) {
E = *pE;
} else {
/* compute the new exponent */
E = 1 + bf_mul_log2_radix(a->expn - 1, radix, TRUE, FALSE);
}
// bf_print_str("a", a);
// printf("E=%ld P=%ld radix=%d\n", E, P, radix);
for(;;) {
e = P - E;
e_sign = 0;
if (e < 0) {
e = -e;
e_sign = 1;
}
/* Note: precision for log2(radix) is not critical here */
prec0 = bf_mul_log2_radix(P, radix, FALSE, TRUE);
ziv_extra_bits = 16;
for(;;) {
prec = prec0 + ziv_extra_bits;
/* XXX: rigorous error analysis needed */
extra_bits = ceil_log2(e) * 2 + 1;
ret = bf_pow_ui_ui(r, radix, e, prec + extra_bits,
BF_RNDN | BF_FLAG_EXT_EXP);
if (!e_sign)
ret |= bf_mul(r, r, a, prec + extra_bits,
BF_RNDN | BF_FLAG_EXT_EXP);
else
ret |= bf_div(r, a, r, prec + extra_bits,
BF_RNDN | BF_FLAG_EXT_EXP);
if (ret & BF_ST_MEM_ERROR)
return BF_ST_MEM_ERROR;
/* if the result is not exact, check that it can be safely
rounded to an integer */
if ((ret & BF_ST_INEXACT) &&
!bf_can_round(r, r->expn, rnd_mode, prec)) {
/* and more precision and retry */
ziv_extra_bits = ziv_extra_bits + (ziv_extra_bits / 2);
continue;
} else {
ret = bf_rint(r, rnd_mode);
if (ret & BF_ST_MEM_ERROR)
return BF_ST_MEM_ERROR;
break;
}
}
if (is_fixed_exponent)
break;
/* check that the result is < B^P */
/* XXX: do a fast approximate test first ? */
bf_init(r->ctx, B);
ret = bf_pow_ui_ui(B, radix, P, BF_PREC_INF, BF_RNDZ);
if (ret) {
bf_delete(B);
return ret;
}
res = bf_cmpu(r, B);
bf_delete(B);
if (res < 0)
break;
/* try a larger exponent */
E++;
}
*pE = E;
return 0;
}
|
/* a must be >= 0. 'P' is the wanted number of digits in radix
'radix'. 'r' is the mantissa represented as an integer. *pE
contains the exponent. Return != 0 if memory error. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L3487-L3568
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
limb_to_a2
|
static void limb_to_a2(char *buf, limb_t n, unsigned int radix_bits, int len)
{
int digit, i;
unsigned int mask;
mask = (1 << radix_bits) - 1;
for(i = len - 1; i >= 0; i--) {
digit = n & mask;
n >>= radix_bits;
if (digit < 10)
digit += '0';
else
digit += 'a' - 10;
buf[i] = digit;
}
}
|
/* for power of 2 radixes */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L3595-L3610
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
output_digits
|
static void output_digits(DynBuf *s, const bf_t *a1, int radix, limb_t n_digits,
limb_t dot_pos, BOOL is_dec)
{
limb_t i, v, l;
slimb_t pos, pos_incr;
int digits_per_limb, buf_pos, radix_bits, first_buf_pos;
char buf[65];
bf_t a_s, *a;
if (is_dec) {
digits_per_limb = LIMB_DIGITS;
a = (bf_t *)a1;
radix_bits = 0;
pos = a->len;
pos_incr = 1;
first_buf_pos = 0;
} else if ((radix & (radix - 1)) == 0) {
a = (bf_t *)a1;
radix_bits = ceil_log2(radix);
digits_per_limb = LIMB_BITS / radix_bits;
pos_incr = digits_per_limb * radix_bits;
/* digits are aligned relative to the radix point */
pos = a->len * LIMB_BITS + smod(-a->expn, radix_bits);
first_buf_pos = 0;
} else {
limb_t n, radixl;
digits_per_limb = digits_per_limb_table[radix - 2];
radixl = get_limb_radix(radix);
a = &a_s;
bf_init(a1->ctx, a);
n = (n_digits + digits_per_limb - 1) / digits_per_limb;
if (bf_resize(a, n)) {
dbuf_set_error(s);
goto done;
}
if (bf_integer_to_radix(a, a1, radixl)) {
dbuf_set_error(s);
goto done;
}
radix_bits = 0;
pos = n;
pos_incr = 1;
first_buf_pos = pos * digits_per_limb - n_digits;
}
buf_pos = digits_per_limb;
i = 0;
while (i < n_digits) {
if (buf_pos == digits_per_limb) {
pos -= pos_incr;
if (radix_bits == 0) {
v = get_limbz(a, pos);
limb_to_a(buf, v, radix, digits_per_limb);
} else {
v = get_bits(a->tab, a->len, pos);
limb_to_a2(buf, v, radix_bits, digits_per_limb);
}
buf_pos = first_buf_pos;
first_buf_pos = 0;
}
if (i < dot_pos) {
l = dot_pos;
} else {
if (i == dot_pos)
dbuf_putc(s, '.');
l = n_digits;
}
l = bf_min(digits_per_limb - buf_pos, l - i);
dbuf_put(s, (uint8_t *)(buf + buf_pos), l);
buf_pos += l;
i += l;
}
done:
if (a != a1)
bf_delete(a);
}
|
/* 'a' must be an integer if the is_dec = FALSE or if the radix is not
a power of two. A dot is added before the 'dot_pos' digit. dot_pos
= n_digits does not display the dot. 0 <= dot_pos <=
n_digits. n_digits >= 1. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L3616-L3691
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_const_log2_rec
|
static void bf_const_log2_rec(bf_t *T, bf_t *P, bf_t *Q, limb_t n1,
limb_t n2, BOOL need_P)
{
bf_context_t *s = T->ctx;
if ((n2 - n1) == 1) {
if (n1 == 0) {
bf_set_ui(P, 3);
} else {
bf_set_ui(P, n1);
P->sign = 1;
}
bf_set_ui(Q, 2 * n1 + 1);
Q->expn += 2;
bf_set(T, P);
} else {
limb_t m;
bf_t T1_s, *T1 = &T1_s;
bf_t P1_s, *P1 = &P1_s;
bf_t Q1_s, *Q1 = &Q1_s;
m = n1 + ((n2 - n1) >> 1);
bf_const_log2_rec(T, P, Q, n1, m, TRUE);
bf_init(s, T1);
bf_init(s, P1);
bf_init(s, Q1);
bf_const_log2_rec(T1, P1, Q1, m, n2, need_P);
bf_mul(T, T, Q1, BF_PREC_INF, BF_RNDZ);
bf_mul(T1, T1, P, BF_PREC_INF, BF_RNDZ);
bf_add(T, T, T1, BF_PREC_INF, BF_RNDZ);
if (need_P)
bf_mul(P, P, P1, BF_PREC_INF, BF_RNDZ);
bf_mul(Q, Q, Q1, BF_PREC_INF, BF_RNDZ);
bf_delete(T1);
bf_delete(P1);
bf_delete(Q1);
}
}
|
/***************************************************************/
/* transcendental functions */
/* Note: the algorithm is from MPFR */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4004-L4040
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_const_log2_internal
|
static void bf_const_log2_internal(bf_t *T, limb_t prec)
{
limb_t w, N;
bf_t P_s, *P = &P_s;
bf_t Q_s, *Q = &Q_s;
w = prec + 15;
N = w / 3 + 1;
bf_init(T->ctx, P);
bf_init(T->ctx, Q);
bf_const_log2_rec(T, P, Q, 0, N, FALSE);
bf_div(T, T, Q, prec, BF_RNDN);
bf_delete(P);
bf_delete(Q);
}
|
/* compute log(2) with faithful rounding at precision 'prec' */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4043-L4057
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_const_pi_internal
|
static void bf_const_pi_internal(bf_t *Q, limb_t prec)
{
bf_context_t *s = Q->ctx;
int64_t n, prec1;
bf_t P, G;
/* number of serie terms */
n = prec / CHUD_BITS_PER_TERM + 1;
/* XXX: precision analysis */
prec1 = prec + 32;
bf_init(s, &P);
bf_init(s, &G);
chud_bs(&P, Q, &G, 0, n, 0, BF_PREC_INF);
bf_mul_ui(&G, Q, CHUD_A, prec1, BF_RNDN);
bf_add(&P, &G, &P, prec1, BF_RNDN);
bf_div(Q, Q, &P, prec1, BF_RNDF);
bf_set_ui(&P, CHUD_C);
bf_sqrt(&G, &P, prec1, BF_RNDF);
bf_mul_ui(&G, &G, (uint64_t)CHUD_C / 12, prec1, BF_RNDF);
bf_mul(Q, Q, &G, prec, BF_RNDN);
bf_delete(&P);
bf_delete(&G);
}
|
/* compute Pi with faithful rounding at precision 'prec' using the
Chudnovsky formula */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4124-L4150
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_const_pi_signed
|
static int bf_const_pi_signed(bf_t *T, int sign, limb_t prec, bf_flags_t flags)
{
bf_context_t *s = T->ctx;
return bf_const_get(T, prec, flags, &s->pi_cache, bf_const_pi_internal,
sign);
}
|
/* return rounded pi * (1 - 2 * sign) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4194-L4199
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_add_epsilon
|
static int bf_add_epsilon(bf_t *r, const bf_t *a, slimb_t e, int e_sign,
limb_t prec, int flags)
{
bf_t T_s, *T = &T_s;
int ret;
/* small argument case: result = 1 + epsilon * sign(x) */
bf_init(a->ctx, T);
bf_set_ui(T, 1);
T->sign = e_sign;
T->expn += e;
ret = bf_add(r, r, T, prec, flags);
bf_delete(T);
return ret;
}
|
/* add (1 - 2*e_sign) * 2^e */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4263-L4276
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_exp_internal
|
static int bf_exp_internal(bf_t *r, const bf_t *a, limb_t prec, void *opaque)
{
bf_context_t *s = r->ctx;
bf_t T_s, *T = &T_s;
slimb_t n, K, l, i, prec1;
assert(r != a);
/* argument reduction:
T = a - n*log(2) with 0 <= T < log(2) and n integer.
*/
bf_init(s, T);
if (a->expn <= -1) {
/* 0 <= abs(a) <= 0.5 */
if (a->sign)
n = -1;
else
n = 0;
} else {
bf_const_log2(T, LIMB_BITS, BF_RNDZ);
bf_div(T, a, T, LIMB_BITS, BF_RNDD);
bf_get_limb(&n, T, 0);
}
K = bf_isqrt((prec + 1) / 2);
l = (prec - 1) / K + 1;
/* XXX: precision analysis ? */
prec1 = prec + (K + 2 * l + 18) + K + 8;
if (a->expn > 0)
prec1 += a->expn;
// printf("n=%ld K=%ld prec1=%ld\n", n, K, prec1);
bf_const_log2(T, prec1, BF_RNDF);
bf_mul_si(T, T, n, prec1, BF_RNDN);
bf_sub(T, a, T, prec1, BF_RNDN);
/* reduce the range of T */
bf_mul_2exp(T, -K, BF_PREC_INF, BF_RNDZ);
/* Taylor expansion around zero :
1 + x + x^2/2 + ... + x^n/n!
= (1 + x * (1 + x/2 * (1 + ... (x/n))))
*/
{
bf_t U_s, *U = &U_s;
bf_init(s, U);
bf_set_ui(r, 1);
for(i = l ; i >= 1; i--) {
bf_set_ui(U, i);
bf_div(U, T, U, prec1, BF_RNDN);
bf_mul(r, r, U, prec1, BF_RNDN);
bf_add_si(r, r, 1, prec1, BF_RNDN);
}
bf_delete(U);
}
bf_delete(T);
/* undo the range reduction */
for(i = 0; i < K; i++) {
bf_mul(r, r, r, prec1, BF_RNDN | BF_FLAG_EXT_EXP);
}
/* undo the argument reduction */
bf_mul_2exp(r, n, BF_PREC_INF, BF_RNDZ | BF_FLAG_EXT_EXP);
return BF_ST_INEXACT;
}
|
/* Compute the exponential using faithful rounding at precision 'prec'.
Note: the algorithm is from MPFR */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4280-L4347
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
check_exp_underflow_overflow
|
static int check_exp_underflow_overflow(bf_context_t *s, bf_t *r,
const bf_t *a_low, const bf_t *a_high,
limb_t prec, bf_flags_t flags)
{
bf_t T_s, *T = &T_s;
bf_t log2_s, *log2 = &log2_s;
slimb_t e_min, e_max;
if (a_high->expn <= 0)
return 0;
e_max = (limb_t)1 << (bf_get_exp_bits(flags) - 1);
e_min = -e_max + 3;
if (flags & BF_FLAG_SUBNORMAL)
e_min -= (prec - 1);
bf_init(s, T);
bf_init(s, log2);
bf_const_log2(log2, LIMB_BITS, BF_RNDU);
bf_mul_ui(T, log2, e_max, LIMB_BITS, BF_RNDU);
/* a_low > e_max * log(2) implies exp(a) > e_max */
if (bf_cmp_lt(T, a_low) > 0) {
/* overflow */
bf_delete(T);
bf_delete(log2);
return bf_set_overflow(r, 0, prec, flags);
}
/* a_high < (e_min - 2) * log(2) implies exp(a) < (e_min - 2) */
bf_const_log2(log2, LIMB_BITS, BF_RNDD);
bf_mul_si(T, log2, e_min - 2, LIMB_BITS, BF_RNDD);
if (bf_cmp_lt(a_high, T)) {
int rnd_mode = flags & BF_RND_MASK;
/* underflow */
bf_delete(T);
bf_delete(log2);
if (rnd_mode == BF_RNDU) {
/* set the smallest value */
bf_set_ui(r, 1);
r->expn = e_min;
} else {
bf_set_zero(r, 0);
}
return BF_ST_UNDERFLOW | BF_ST_INEXACT;
}
bf_delete(log2);
bf_delete(T);
return 0;
}
|
/* crude overflow and underflow tests for exp(a). a_low <= a <= a_high */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4350-L4398
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_pow_generic
|
static int bf_pow_generic(bf_t *r, const bf_t *x, limb_t prec, void *opaque)
{
bf_context_t *s = r->ctx;
const bf_t *y = opaque;
bf_t T_s, *T = &T_s;
limb_t prec1;
bf_init(s, T);
/* XXX: proof for the added precision */
prec1 = prec + 32;
bf_log(T, x, prec1, BF_RNDF | BF_FLAG_EXT_EXP);
bf_mul(T, T, y, prec1, BF_RNDF | BF_FLAG_EXT_EXP);
if (bf_is_nan(T))
bf_set_nan(r);
else
bf_exp_internal(r, T, prec1, NULL); /* no overflow/underlow test needed */
bf_delete(T);
return BF_ST_INEXACT;
}
|
/* x and y finite and x > 0 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4570-L4588
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_pow_int
|
static int bf_pow_int(bf_t *r, const bf_t *x, limb_t prec, void *opaque)
{
bf_context_t *s = r->ctx;
const bf_t *y = opaque;
bf_t T_s, *T = &T_s;
limb_t prec1;
int ret;
slimb_t y1;
bf_get_limb(&y1, y, 0);
if (y1 < 0)
y1 = -y1;
/* XXX: proof for the added precision */
prec1 = prec + ceil_log2(y1) * 2 + 8;
ret = bf_pow_ui(r, x, y1 < 0 ? -y1 : y1, prec1, BF_RNDN | BF_FLAG_EXT_EXP);
if (y->sign) {
bf_init(s, T);
bf_set_ui(T, 1);
ret |= bf_div(r, T, r, prec1, BF_RNDN | BF_FLAG_EXT_EXP);
bf_delete(T);
}
return ret;
}
|
/* x and y finite, x > 0, y integer and y fits on one limb */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4591-L4613
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
check_exact_power2n
|
static BOOL check_exact_power2n(bf_t *r, const bf_t *x, slimb_t n)
{
bf_context_t *s = r->ctx;
bf_t T_s, *T = &T_s;
slimb_t e, i, er;
limb_t v;
/* x = m*2^e with m odd integer */
e = bf_get_exp_min(x);
/* fast check on the exponent */
if (n > (LIMB_BITS - 1)) {
if (e != 0)
return FALSE;
er = 0;
} else {
if ((e & (((limb_t)1 << n) - 1)) != 0)
return FALSE;
er = e >> n;
}
/* every perfect odd square = 1 modulo 8 */
v = get_bits(x->tab, x->len, x->len * LIMB_BITS - x->expn + e);
if ((v & 7) != 1)
return FALSE;
bf_init(s, T);
bf_set(T, x);
T->expn -= e;
for(i = 0; i < n; i++) {
if (i != 0)
bf_set(T, r);
if (bf_sqrtrem(r, NULL, T) != 0)
return FALSE;
}
r->expn += er;
return TRUE;
}
|
/* x must be a finite non zero float. Return TRUE if there is a
floating point number r such as x=r^(2^n) and return this floating
point number 'r'. Otherwise return FALSE and r is undefined. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4618-L4653
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_pow
|
int bf_pow(bf_t *r, const bf_t *x, const bf_t *y, limb_t prec, bf_flags_t flags)
{
bf_context_t *s = r->ctx;
bf_t T_s, *T = &T_s;
bf_t ytmp_s;
BOOL y_is_int, y_is_odd;
int r_sign, ret, rnd_mode;
slimb_t y_emin;
if (x->len == 0 || y->len == 0) {
if (y->expn == BF_EXP_ZERO) {
/* pow(x, 0) = 1 */
bf_set_ui(r, 1);
} else if (x->expn == BF_EXP_NAN) {
bf_set_nan(r);
} else {
int cmp_x_abs_1;
bf_set_ui(r, 1);
cmp_x_abs_1 = bf_cmpu(x, r);
if (cmp_x_abs_1 == 0 && (flags & BF_POW_JS_QUIRKS) &&
(y->expn >= BF_EXP_INF)) {
bf_set_nan(r);
} else if (cmp_x_abs_1 == 0 &&
(!x->sign || y->expn != BF_EXP_NAN)) {
/* pow(1, y) = 1 even if y = NaN */
/* pow(-1, +/-inf) = 1 */
} else if (y->expn == BF_EXP_NAN) {
bf_set_nan(r);
} else if (y->expn == BF_EXP_INF) {
if (y->sign == (cmp_x_abs_1 > 0)) {
bf_set_zero(r, 0);
} else {
bf_set_inf(r, 0);
}
} else {
y_emin = bf_get_exp_min(y);
y_is_odd = (y_emin == 0);
if (y->sign == (x->expn == BF_EXP_ZERO)) {
bf_set_inf(r, y_is_odd & x->sign);
if (y->sign) {
/* pow(0, y) with y < 0 */
return BF_ST_DIVIDE_ZERO;
}
} else {
bf_set_zero(r, y_is_odd & x->sign);
}
}
}
return 0;
}
bf_init(s, T);
bf_set(T, x);
y_emin = bf_get_exp_min(y);
y_is_int = (y_emin >= 0);
rnd_mode = flags & BF_RND_MASK;
if (x->sign) {
if (!y_is_int) {
bf_set_nan(r);
bf_delete(T);
return BF_ST_INVALID_OP;
}
y_is_odd = (y_emin == 0);
r_sign = y_is_odd;
/* change the directed rounding mode if the sign of the result
is changed */
if (r_sign && (rnd_mode == BF_RNDD || rnd_mode == BF_RNDU))
flags ^= 1;
bf_neg(T);
} else {
r_sign = 0;
}
bf_set_ui(r, 1);
if (bf_cmp_eq(T, r)) {
/* abs(x) = 1: nothing more to do */
ret = 0;
} else {
/* check the overflow/underflow cases */
{
bf_t al_s, *al = &al_s;
bf_t ah_s, *ah = &ah_s;
limb_t precl = LIMB_BITS;
bf_init(s, al);
bf_init(s, ah);
/* compute bounds of log(abs(x)) * y with a low precision */
/* XXX: compute bf_log() once */
/* XXX: add a fast test before this slow test */
bf_log(al, T, precl, BF_RNDD);
bf_log(ah, T, precl, BF_RNDU);
bf_mul(al, al, y, precl, BF_RNDD ^ y->sign);
bf_mul(ah, ah, y, precl, BF_RNDU ^ y->sign);
ret = check_exp_underflow_overflow(s, r, al, ah, prec, flags);
bf_delete(al);
bf_delete(ah);
if (ret)
goto done;
}
if (y_is_int) {
slimb_t T_bits, e;
int_pow:
T_bits = T->expn - bf_get_exp_min(T);
if (T_bits == 1) {
/* pow(2^b, y) = 2^(b*y) */
bf_mul_si(T, y, T->expn - 1, LIMB_BITS, BF_RNDZ);
bf_get_limb(&e, T, 0);
bf_set_ui(r, 1);
ret = bf_mul_2exp(r, e, prec, flags);
} else if (prec == BF_PREC_INF) {
slimb_t y1;
/* specific case for infinite precision (integer case) */
bf_get_limb(&y1, y, 0);
assert(!y->sign);
/* x must be an integer, so abs(x) >= 2 */
if (y1 >= ((slimb_t)1 << BF_EXP_BITS_MAX)) {
bf_delete(T);
return bf_set_overflow(r, 0, BF_PREC_INF, flags);
}
ret = bf_pow_ui(r, T, y1, BF_PREC_INF, BF_RNDZ);
} else {
if (y->expn <= 31) {
/* small enough power: use exponentiation in all cases */
} else if (y->sign) {
/* cannot be exact */
goto general_case;
} else {
if (rnd_mode == BF_RNDF)
goto general_case; /* no need to track exact results */
/* see if the result has a chance to be exact:
if x=a*2^b (a odd), x^y=a^y*2^(b*y)
x^y needs a precision of at least floor_log2(a)*y bits
*/
bf_mul_si(r, y, T_bits - 1, LIMB_BITS, BF_RNDZ);
bf_get_limb(&e, r, 0);
if (prec < e)
goto general_case;
}
ret = bf_ziv_rounding(r, T, prec, flags, bf_pow_int, (void *)y);
}
} else {
if (rnd_mode != BF_RNDF) {
bf_t *y1;
if (y_emin < 0 && check_exact_power2n(r, T, -y_emin)) {
/* the problem is reduced to a power to an integer */
#if 0
printf("\nn=%" PRId64 "\n", -(int64_t)y_emin);
bf_print_str("T", T);
bf_print_str("r", r);
#endif
bf_set(T, r);
y1 = &ytmp_s;
y1->tab = y->tab;
y1->len = y->len;
y1->sign = y->sign;
y1->expn = y->expn - y_emin;
y = y1;
goto int_pow;
}
}
general_case:
ret = bf_ziv_rounding(r, T, prec, flags, bf_pow_generic, (void *)y);
}
}
done:
bf_delete(T);
r->sign = r_sign;
return ret;
}
|
/* prec = BF_PREC_INF is accepted for x and y integers and y >= 0 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4656-L4824
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_sqrt_sin
|
static void bf_sqrt_sin(bf_t *r, const bf_t *x, limb_t prec1)
{
bf_context_t *s = r->ctx;
bf_t T_s, *T = &T_s;
bf_init(s, T);
bf_set(T, x);
bf_mul(r, T, T, prec1, BF_RNDN);
bf_mul_2exp(T, 1, BF_PREC_INF, BF_RNDZ);
bf_add(T, T, r, prec1, BF_RNDN);
bf_neg(T);
bf_sqrt(r, T, prec1, BF_RNDF);
bf_delete(T);
}
|
/* compute sqrt(-2*x-x^2) to get |sin(x)| from cos(x) - 1. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L4827-L4839
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bf_atan_internal
|
static int bf_atan_internal(bf_t *r, const bf_t *a, limb_t prec,
void *opaque)
{
bf_context_t *s = r->ctx;
BOOL add_pi2 = (BOOL)(intptr_t)opaque;
bf_t T_s, *T = &T_s;
bf_t U_s, *U = &U_s;
bf_t V_s, *V = &V_s;
bf_t X2_s, *X2 = &X2_s;
int cmp_1;
slimb_t prec1, i, K, l;
/* XXX: precision analysis */
K = bf_isqrt((prec + 1) / 2);
l = prec / (2 * K) + 1;
prec1 = prec + K + 2 * l + 32;
// printf("prec=%d K=%d l=%d prec1=%d\n", (int)prec, (int)K, (int)l, (int)prec1);
bf_init(s, T);
cmp_1 = (a->expn >= 1); /* a >= 1 */
if (cmp_1) {
bf_set_ui(T, 1);
bf_div(T, T, a, prec1, BF_RNDN);
} else {
bf_set(T, a);
}
/* abs(T) <= 1 */
/* argument reduction */
bf_init(s, U);
bf_init(s, V);
bf_init(s, X2);
for(i = 0; i < K; i++) {
/* T = T / (1 + sqrt(1 + T^2)) */
bf_mul(U, T, T, prec1, BF_RNDN);
bf_add_si(U, U, 1, prec1, BF_RNDN);
bf_sqrt(V, U, prec1, BF_RNDN);
bf_add_si(V, V, 1, prec1, BF_RNDN);
bf_div(T, T, V, prec1, BF_RNDN);
}
/* Taylor series:
x - x^3/3 + ... + (-1)^ l * y^(2*l + 1) / (2*l+1)
*/
bf_mul(X2, T, T, prec1, BF_RNDN);
bf_set_ui(r, 0);
for(i = l; i >= 1; i--) {
bf_set_si(U, 1);
bf_set_ui(V, 2 * i + 1);
bf_div(U, U, V, prec1, BF_RNDN);
bf_neg(r);
bf_add(r, r, U, prec1, BF_RNDN);
bf_mul(r, r, X2, prec1, BF_RNDN);
}
bf_neg(r);
bf_add_si(r, r, 1, prec1, BF_RNDN);
bf_mul(r, r, T, prec1, BF_RNDN);
/* undo the argument reduction */
bf_mul_2exp(r, K, BF_PREC_INF, BF_RNDZ);
bf_delete(U);
bf_delete(V);
bf_delete(X2);
i = add_pi2;
if (cmp_1 > 0) {
/* undo the inversion : r = sign(a)*PI/2 - r */
bf_neg(r);
i += 1 - 2 * a->sign;
}
/* add i*(pi/2) with -1 <= i <= 2 */
if (i != 0) {
bf_const_pi(T, prec1, BF_RNDF);
if (i != 2)
bf_mul_2exp(T, -1, BF_PREC_INF, BF_RNDZ);
T->sign = (i < 0);
bf_add(r, T, r, prec1, BF_RNDN);
}
bf_delete(T);
return BF_ST_INEXACT;
}
|
/* if add_pi2 is true, add pi/2 to the result (used for acos(x) to
avoid cancellation) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5053-L5137
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
shrd
|
static inline __maybe_unused limb_t shrd(limb_t low, limb_t high, long shift)
{
if (shift != 0)
low = (low >> shift) | (high << (LIMB_BITS - shift));
return low;
}
|
/* LIMB_BITS != 64 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5395-L5400
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
fast_udiv_init
|
static inline __maybe_unused void fast_udiv_init(FastDivData *s, limb_t d)
{
int l;
limb_t q, r, m1;
if (d == 1)
l = 0;
else
l = 64 - clz64(d - 1);
divdq(q, r, ((limb_t)1 << l) - d, 0, d);
(void)r;
m1 = q + 1;
// printf("d=%lu l=%d m1=0x%016lx\n", d, l, m1);
s->m1 = m1;
s->shift1 = l;
if (s->shift1 > 1)
s->shift1 = 1;
s->shift2 = l - 1;
if (s->shift2 < 0)
s->shift2 = 0;
}
|
/* From "Division by Invariant Integers using Multiplication" by
Torborn Granlund and Peter L. Montgomery */
/* d must be != 0 */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5464-L5483
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
fast_shr_dec
|
static inline limb_t fast_shr_dec(limb_t a, int shift)
{
return fast_udiv(a, &mp_pow_div[shift]);
}
|
/* divide by 10^shift with 0 <= shift <= LIMB_DIGITS */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5557-L5560
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_mul1_dec
|
limb_t mp_mul1_dec(limb_t *tabr, const limb_t *taba, mp_size_t n,
limb_t b, limb_t l)
{
mp_size_t i;
limb_t t0, t1, r;
for(i = 0; i < n; i++) {
muldq(t1, t0, taba[i], b);
adddq(t1, t0, 0, l);
divdq_base(l, r, t1, t0);
tabr[i] = r;
}
return l;
}
|
/* taba[] = taba[] * b + l. 0 <= b, l <= base - 1. Return the high carry */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5645-L5658
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_add_mul1_dec
|
limb_t mp_add_mul1_dec(limb_t *tabr, const limb_t *taba, mp_size_t n,
limb_t b)
{
mp_size_t i;
limb_t l, t0, t1, r;
l = 0;
for(i = 0; i < n; i++) {
muldq(t1, t0, taba[i], b);
adddq(t1, t0, 0, l);
adddq(t1, t0, 0, tabr[i]);
divdq_base(l, r, t1, t0);
tabr[i] = r;
}
return l;
}
|
/* tabr[] += taba[] * b. 0 <= b <= base - 1. Return the value to add
to the high word */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5662-L5677
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_sub_mul1_dec
|
limb_t mp_sub_mul1_dec(limb_t *tabr, const limb_t *taba, mp_size_t n,
limb_t b)
{
limb_t base = BF_DEC_BASE;
mp_size_t i;
limb_t l, t0, t1, r, a, v, c;
/* XXX: optimize */
l = 0;
for(i = 0; i < n; i++) {
muldq(t1, t0, taba[i], b);
adddq(t1, t0, 0, l);
divdq_base(l, r, t1, t0);
v = tabr[i];
a = v - r;
c = a > v;
if (c)
a += base;
/* never bigger than base because r = 0 when l = base - 1 */
l += c;
tabr[i] = a;
}
return l;
}
|
/* tabr[] -= taba[] * b. 0 <= b <= base - 1. Return the value to
substract to the high word. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5681-L5704
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_mul_basecase_dec
|
void mp_mul_basecase_dec(limb_t *result,
const limb_t *op1, mp_size_t op1_size,
const limb_t *op2, mp_size_t op2_size)
{
mp_size_t i;
limb_t r;
result[op1_size] = mp_mul1_dec(result, op1, op1_size, op2[0], 0);
for(i=1;i<op2_size;i++) {
r = mp_add_mul1_dec(result + i, op1, op1_size, op2[i]);
result[i + op1_size] = r;
}
}
|
/* size of the result : op1_size + op2_size. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5707-L5720
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_div1_dec
|
limb_t mp_div1_dec(limb_t *tabr, const limb_t *taba, mp_size_t na,
limb_t b, limb_t r)
{
limb_t base = BF_DEC_BASE;
mp_size_t i;
limb_t t0, t1, q;
int shift;
#if (BF_DEC_BASE % 2) == 0
if (b == 2) {
limb_t base_div2;
/* Note: only works if base is even */
base_div2 = base >> 1;
if (r)
r = base_div2;
for(i = na - 1; i >= 0; i--) {
t0 = taba[i];
tabr[i] = (t0 >> 1) + r;
r = 0;
if (t0 & 1)
r = base_div2;
}
if (r)
r = 1;
} else
#endif
if (na >= UDIV1NORM_THRESHOLD) {
shift = clz(b);
if (shift == 0) {
/* normalized case: b >= 2^(LIMB_BITS-1) */
limb_t b_inv;
b_inv = udiv1norm_init(b);
for(i = na - 1; i >= 0; i--) {
muldq(t1, t0, r, base);
adddq(t1, t0, 0, taba[i]);
q = udiv1norm(&r, t1, t0, b, b_inv);
tabr[i] = q;
}
} else {
limb_t b_inv;
b <<= shift;
b_inv = udiv1norm_init(b);
for(i = na - 1; i >= 0; i--) {
muldq(t1, t0, r, base);
adddq(t1, t0, 0, taba[i]);
t1 = (t1 << shift) | (t0 >> (LIMB_BITS - shift));
t0 <<= shift;
q = udiv1norm(&r, t1, t0, b, b_inv);
r >>= shift;
tabr[i] = q;
}
}
} else {
for(i = na - 1; i >= 0; i--) {
muldq(t1, t0, r, base);
adddq(t1, t0, 0, taba[i]);
divdq(q, r, t1, t0, b);
tabr[i] = q;
}
}
return r;
}
|
/* taba[] = (taba[] + r*base^na) / b. 0 <= b < base. 0 <= r <
b. Return the remainder. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5724-L5785
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_div_dec
|
static int mp_div_dec(bf_context_t *s, limb_t *tabq,
limb_t *taba, mp_size_t na,
const limb_t *tabb1, mp_size_t nb)
{
limb_t base = BF_DEC_BASE;
limb_t r, mult, t0, t1, a, c, q, v, *tabb;
mp_size_t i, j;
limb_t static_tabb[DIV_STATIC_ALLOC_LEN];
#ifdef DEBUG_DIV_SLOW
mp_print_str_dec("a", taba, na);
mp_print_str_dec("b", tabb1, nb);
#endif
/* normalize tabb */
r = tabb1[nb - 1];
assert(r != 0);
i = na - nb;
if (r >= BF_DEC_BASE / 2) {
mult = 1;
tabb = (limb_t *)tabb1;
q = 1;
for(j = nb - 1; j >= 0; j--) {
if (taba[i + j] != tabb[j]) {
if (taba[i + j] < tabb[j])
q = 0;
break;
}
}
tabq[i] = q;
if (q) {
mp_sub_dec(taba + i, taba + i, tabb, nb, 0);
}
i--;
} else {
mult = base / (r + 1);
if (likely(nb <= DIV_STATIC_ALLOC_LEN)) {
tabb = static_tabb;
} else {
tabb = bf_malloc(s, sizeof(limb_t) * nb);
if (!tabb)
return -1;
}
mp_mul1_dec(tabb, tabb1, nb, mult, 0);
taba[na] = mp_mul1_dec(taba, taba, na, mult, 0);
}
#ifdef DEBUG_DIV_SLOW
printf("mult=" FMT_LIMB "\n", mult);
mp_print_str_dec("a_norm", taba, na + 1);
mp_print_str_dec("b_norm", tabb, nb);
#endif
for(; i >= 0; i--) {
if (unlikely(taba[i + nb] >= tabb[nb - 1])) {
/* XXX: check if it is really possible */
q = base - 1;
} else {
muldq(t1, t0, taba[i + nb], base);
adddq(t1, t0, 0, taba[i + nb - 1]);
divdq(q, r, t1, t0, tabb[nb - 1]);
}
// printf("i=%d q1=%ld\n", i, q);
r = mp_sub_mul1_dec(taba + i, tabb, nb, q);
// mp_dump("r1", taba + i, nb, bd);
// printf("r2=%ld\n", r);
v = taba[i + nb];
a = v - r;
c = a > v;
if (c)
a += base;
taba[i + nb] = a;
if (c != 0) {
/* negative result */
for(;;) {
q--;
c = mp_add_dec(taba + i, taba + i, tabb, nb, 0);
/* propagate carry and test if positive result */
if (c != 0) {
if (++taba[i + nb] == base) {
break;
}
}
}
}
tabq[i] = q;
}
#ifdef DEBUG_DIV_SLOW
mp_print_str_dec("q", tabq, na - nb + 1);
mp_print_str_dec("r", taba, nb);
#endif
/* remove the normalization */
if (mult != 1) {
mp_div1_dec(taba, taba, nb, mult, 0);
if (unlikely(tabb != static_tabb))
bf_free(s, tabb);
}
return 0;
}
|
/* return q = a / b and r = a % b.
taba[na] must be allocated if tabb1[nb - 1] < B / 2. tabb1[nb - 1]
must be != zero. na must be >= nb. 's' can be NULL if tabb1[nb - 1]
>= B / 2.
The remainder is is returned in taba and contains nb libms. tabq
contains na - nb + 1 limbs. No overlap is permitted.
Running time of the standard method: (na - nb + 1) * nb
Return 0 if OK, -1 if memory alloc error
*/
/* XXX: optimize */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5831-L5934
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_shr_dec
|
static limb_t mp_shr_dec(limb_t *tab_r, const limb_t *tab, mp_size_t n,
limb_t shift, limb_t high)
{
mp_size_t i;
limb_t l, a, q, r;
assert(shift >= 1 && shift < LIMB_DIGITS);
l = high;
for(i = n - 1; i >= 0; i--) {
a = tab[i];
fast_shr_rem_dec(q, r, a, shift);
tab_r[i] = q + l * mp_pow_dec[LIMB_DIGITS - shift];
l = r;
}
return l;
}
|
/* divide by 10^shift */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5937-L5952
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_shl_dec
|
static limb_t mp_shl_dec(limb_t *tab_r, const limb_t *tab, mp_size_t n,
limb_t shift, limb_t low)
{
mp_size_t i;
limb_t l, a, q, r;
assert(shift >= 1 && shift < LIMB_DIGITS);
l = low;
for(i = 0; i < n; i++) {
a = tab[i];
fast_shr_rem_dec(q, r, a, LIMB_DIGITS - shift);
tab_r[i] = r * mp_pow_dec[shift] + l;
l = q;
}
return l;
}
|
/* multiply by 10^shift */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5955-L5970
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_sqrtrem_rec_dec
|
static limb_t mp_sqrtrem_rec_dec(limb_t *tabs, limb_t *taba, limb_t n,
limb_t *tmp_buf)
{
limb_t l, h, rh, ql, qh, c, i;
if (n == 1)
return mp_sqrtrem2_dec(tabs, taba);
#ifdef DEBUG_SQRTREM_DEC
mp_print_str_dec("a", taba, 2 * n);
#endif
l = n / 2;
h = n - l;
qh = mp_sqrtrem_rec_dec(tabs + l, taba + 2 * l, h, tmp_buf);
#ifdef DEBUG_SQRTREM_DEC
mp_print_str_dec("s1", tabs + l, h);
mp_print_str_h_dec("r1", taba + 2 * l, h, qh);
mp_print_str_h_dec("r2", taba + l, n, qh);
#endif
/* the remainder is in taba + 2 * l. Its high bit is in qh */
if (qh) {
mp_sub_dec(taba + 2 * l, taba + 2 * l, tabs + l, h, 0);
}
/* instead of dividing by 2*s, divide by s (which is normalized)
and update q and r */
mp_div_dec(NULL, tmp_buf, taba + l, n, tabs + l, h);
qh += tmp_buf[l];
for(i = 0; i < l; i++)
tabs[i] = tmp_buf[i];
ql = mp_div1_dec(tabs, tabs, l, 2, qh & 1);
qh = qh >> 1; /* 0 or 1 */
if (ql)
rh = mp_add_dec(taba + l, taba + l, tabs + l, h, 0);
else
rh = 0;
#ifdef DEBUG_SQRTREM_DEC
mp_print_str_h_dec("q", tabs, l, qh);
mp_print_str_h_dec("u", taba + l, h, rh);
#endif
mp_add_ui_dec(tabs + l, qh, h);
#ifdef DEBUG_SQRTREM_DEC
mp_print_str_dec("s2", tabs, n);
#endif
/* q = qh, tabs[l - 1 ... 0], r = taba[n - 1 ... l] */
/* subtract q^2. if qh = 1 then q = B^l, so we can take shortcuts */
if (qh) {
c = qh;
} else {
mp_mul_basecase_dec(taba + n, tabs, l, tabs, l);
c = mp_sub_dec(taba, taba, taba + n, 2 * l, 0);
}
rh -= mp_sub_ui_dec(taba + 2 * l, c, n - 2 * l);
if ((slimb_t)rh < 0) {
mp_sub_ui_dec(tabs, 1, n);
rh += mp_add_mul1_dec(taba, tabs, n, 2);
rh += mp_add_ui_dec(taba, 1, n);
}
return rh;
}
|
//#define DEBUG_SQRTREM_DEC
/* tmp_buf must contain (n / 2 + 1 limbs) */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L5997-L6057
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
mp_sqrtrem_dec
|
int mp_sqrtrem_dec(bf_context_t *s, limb_t *tabs, limb_t *taba, limb_t n)
{
limb_t tmp_buf1[8];
limb_t *tmp_buf;
mp_size_t n2;
n2 = n / 2 + 1;
if (n2 <= countof(tmp_buf1)) {
tmp_buf = tmp_buf1;
} else {
tmp_buf = bf_malloc(s, sizeof(limb_t) * n2);
if (!tmp_buf)
return -1;
}
taba[n] = mp_sqrtrem_rec_dec(tabs, taba, n, tmp_buf);
if (tmp_buf != tmp_buf1)
bf_free(s, tmp_buf);
return 0;
}
|
/* 'taba' has 2*n limbs with n >= 1 and taba[2*n-1] >= B/4. Return (s,
r) with s=floor(sqrt(a)) and r=a-s^2. 0 <= r <= 2 * s. tabs has n
limbs. r is returned in the lower n limbs of taba. Its r[n] is the
returned value of the function. */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6063-L6080
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
clz_dec
|
static int clz_dec(limb_t a)
{
if (a == 0)
return LIMB_DIGITS;
switch(LIMB_BITS - 1 - clz(a)) {
case 0: /* 1-1 */
return LIMB_DIGITS - 1;
case 1: /* 2-3 */
return LIMB_DIGITS - 1;
case 2: /* 4-7 */
return LIMB_DIGITS - 1;
case 3: /* 8-15 */
if (a < 10)
return LIMB_DIGITS - 1;
else
return LIMB_DIGITS - 2;
case 4: /* 16-31 */
return LIMB_DIGITS - 2;
case 5: /* 32-63 */
return LIMB_DIGITS - 2;
case 6: /* 64-127 */
if (a < 100)
return LIMB_DIGITS - 2;
else
return LIMB_DIGITS - 3;
case 7: /* 128-255 */
return LIMB_DIGITS - 3;
case 8: /* 256-511 */
return LIMB_DIGITS - 3;
case 9: /* 512-1023 */
if (a < 1000)
return LIMB_DIGITS - 3;
else
return LIMB_DIGITS - 4;
case 10: /* 1024-2047 */
return LIMB_DIGITS - 4;
case 11: /* 2048-4095 */
return LIMB_DIGITS - 4;
case 12: /* 4096-8191 */
return LIMB_DIGITS - 4;
case 13: /* 8192-16383 */
if (a < 10000)
return LIMB_DIGITS - 4;
else
return LIMB_DIGITS - 5;
case 14: /* 16384-32767 */
return LIMB_DIGITS - 5;
case 15: /* 32768-65535 */
return LIMB_DIGITS - 5;
case 16: /* 65536-131071 */
if (a < 100000)
return LIMB_DIGITS - 5;
else
return LIMB_DIGITS - 6;
case 17: /* 131072-262143 */
return LIMB_DIGITS - 6;
case 18: /* 262144-524287 */
return LIMB_DIGITS - 6;
case 19: /* 524288-1048575 */
if (a < 1000000)
return LIMB_DIGITS - 6;
else
return LIMB_DIGITS - 7;
case 20: /* 1048576-2097151 */
return LIMB_DIGITS - 7;
case 21: /* 2097152-4194303 */
return LIMB_DIGITS - 7;
case 22: /* 4194304-8388607 */
return LIMB_DIGITS - 7;
case 23: /* 8388608-16777215 */
if (a < 10000000)
return LIMB_DIGITS - 7;
else
return LIMB_DIGITS - 8;
case 24: /* 16777216-33554431 */
return LIMB_DIGITS - 8;
case 25: /* 33554432-67108863 */
return LIMB_DIGITS - 8;
case 26: /* 67108864-134217727 */
if (a < 100000000)
return LIMB_DIGITS - 8;
else
return LIMB_DIGITS - 9;
#if LIMB_BITS == 64
case 27: /* 134217728-268435455 */
return LIMB_DIGITS - 9;
case 28: /* 268435456-536870911 */
return LIMB_DIGITS - 9;
case 29: /* 536870912-1073741823 */
if (a < 1000000000)
return LIMB_DIGITS - 9;
else
return LIMB_DIGITS - 10;
case 30: /* 1073741824-2147483647 */
return LIMB_DIGITS - 10;
case 31: /* 2147483648-4294967295 */
return LIMB_DIGITS - 10;
case 32: /* 4294967296-8589934591 */
return LIMB_DIGITS - 10;
case 33: /* 8589934592-17179869183 */
if (a < 10000000000)
return LIMB_DIGITS - 10;
else
return LIMB_DIGITS - 11;
case 34: /* 17179869184-34359738367 */
return LIMB_DIGITS - 11;
case 35: /* 34359738368-68719476735 */
return LIMB_DIGITS - 11;
case 36: /* 68719476736-137438953471 */
if (a < 100000000000)
return LIMB_DIGITS - 11;
else
return LIMB_DIGITS - 12;
case 37: /* 137438953472-274877906943 */
return LIMB_DIGITS - 12;
case 38: /* 274877906944-549755813887 */
return LIMB_DIGITS - 12;
case 39: /* 549755813888-1099511627775 */
if (a < 1000000000000)
return LIMB_DIGITS - 12;
else
return LIMB_DIGITS - 13;
case 40: /* 1099511627776-2199023255551 */
return LIMB_DIGITS - 13;
case 41: /* 2199023255552-4398046511103 */
return LIMB_DIGITS - 13;
case 42: /* 4398046511104-8796093022207 */
return LIMB_DIGITS - 13;
case 43: /* 8796093022208-17592186044415 */
if (a < 10000000000000)
return LIMB_DIGITS - 13;
else
return LIMB_DIGITS - 14;
case 44: /* 17592186044416-35184372088831 */
return LIMB_DIGITS - 14;
case 45: /* 35184372088832-70368744177663 */
return LIMB_DIGITS - 14;
case 46: /* 70368744177664-140737488355327 */
if (a < 100000000000000)
return LIMB_DIGITS - 14;
else
return LIMB_DIGITS - 15;
case 47: /* 140737488355328-281474976710655 */
return LIMB_DIGITS - 15;
case 48: /* 281474976710656-562949953421311 */
return LIMB_DIGITS - 15;
case 49: /* 562949953421312-1125899906842623 */
if (a < 1000000000000000)
return LIMB_DIGITS - 15;
else
return LIMB_DIGITS - 16;
case 50: /* 1125899906842624-2251799813685247 */
return LIMB_DIGITS - 16;
case 51: /* 2251799813685248-4503599627370495 */
return LIMB_DIGITS - 16;
case 52: /* 4503599627370496-9007199254740991 */
return LIMB_DIGITS - 16;
case 53: /* 9007199254740992-18014398509481983 */
if (a < 10000000000000000)
return LIMB_DIGITS - 16;
else
return LIMB_DIGITS - 17;
case 54: /* 18014398509481984-36028797018963967 */
return LIMB_DIGITS - 17;
case 55: /* 36028797018963968-72057594037927935 */
return LIMB_DIGITS - 17;
case 56: /* 72057594037927936-144115188075855871 */
if (a < 100000000000000000)
return LIMB_DIGITS - 17;
else
return LIMB_DIGITS - 18;
case 57: /* 144115188075855872-288230376151711743 */
return LIMB_DIGITS - 18;
case 58: /* 288230376151711744-576460752303423487 */
return LIMB_DIGITS - 18;
case 59: /* 576460752303423488-1152921504606846975 */
if (a < 1000000000000000000)
return LIMB_DIGITS - 18;
else
return LIMB_DIGITS - 19;
#endif
default:
return 0;
}
}
|
/* return the number of leading zero digits, from 0 to LIMB_DIGITS */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6083-L6267
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
BugChecker
|
github_2023
|
vitoplantamura
|
c
|
bfdec_print_str
|
void bfdec_print_str(const char *str, const bfdec_t *a)
{
slimb_t i;
printf("%s=", str);
if (a->expn == BF_EXP_NAN) {
printf("NaN");
} else {
if (a->sign)
putchar('-');
if (a->expn == BF_EXP_ZERO) {
putchar('0');
} else if (a->expn == BF_EXP_INF) {
printf("Inf");
} else {
printf("0.");
for(i = a->len - 1; i >= 0; i--)
printf("%0*" PRIu_LIMB, LIMB_DIGITS, a->tab[i]);
printf("e%" PRId_LIMB, a->expn);
}
}
printf("\n");
}
|
/* for debugging */
|
https://github.com/vitoplantamura/BugChecker/blob/8b81e76efe457b59be3a6e752efd43916ba0cabb/BugChecker/QuickJS/libbf.c#L6270-L6292
|
8b81e76efe457b59be3a6e752efd43916ba0cabb
|
End of preview. Expand
in Data Studio
README.md exists but content is empty.
- Downloads last month
- 84