MCPcopy Create free account
hub / github.com/Moddable-OpenSource/moddable / xsum_small_div_unsigned

Function xsum_small_div_unsigned

xs/sources/xsum.c:2033–2186  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2031/* FIND RESULT OF DIVIDING SMALL ACCUMULATOR BY UNSIGNED INTEGER. */
2032
2033xsum_flt xsum_small_div_unsigned
2034 (xsum_small_accumulator *restrict sacc, unsigned div)
2035{
2036 xsum_flt result;
2037 unsigned rem;
2038 double fltv;
2039 int sign;
2040 int i, j;
2041
2042 if (xsum_debug) c_printf("\nDIVIDE SMALL ACCUMULATOR BY UNSIGNED INTEGER\n");
2043
2044 /* Return NaN or an Inf if that's what's in the superaccumulator. */
2045
2046 if (sacc->NaN != 0)
2047 { COPY64(fltv, sacc->NaN);
2048 return fltv;
2049 }
2050
2051 if (sacc->Inf != 0)
2052 { COPY64 (fltv, sacc->Inf);
2053 return fltv;
2054 }
2055
2056 /* Make a copy of the superaccumulator, so we can change it here without
2057 changing *sacc. */
2058
2059 xsum_small_accumulator tacc = *sacc;
2060
2061 /* Carry propagate in the temporary copy of the superaccumulator.
2062 Sets 'i' to the index of the topmost nonzero chunk. */
2063
2064 i = xsum_carry_propagate(&tacc);
2065
2066 /* Check for division by zero, and if so, return +Inf, -Inf, or NaN,
2067 depending on whether the superaccumulator is positive, negative,
2068 or zero. */
2069
2070 if (div == 0)
2071 { if (xsum_debug)
2072 { c_printf("divide by zero, top chunk has index %d, value %lld\n",
2073 i, tacc.chunk[i]);
2074 }
2075 return tacc.chunk[i] > 0 ? INFINITY : tacc.chunk[i] < 0 ? -INFINITY : NAN;
2076 }
2077
2078 /* Record sign of accumulator, and if it's negative, negate and
2079 re-propagate so that it will be positive. */
2080
2081 sign = +1;
2082
2083 if (tacc.chunk[i] < 0)
2084 { xsum_small_negate(&tacc);
2085 i = xsum_carry_propagate(&tacc);
2086 if (xsum_debug)
2087 { c_printf("Negated accumulator to make it non-negative\n");
2088 if (tacc.chunk[i] < 0) c_abort();
2089 }
2090 sign = -1;

Callers 2

xsum_small_div_intFunction · 0.85
xsum_large_div_unsignedFunction · 0.85

Calls 4

xsum_carry_propagateFunction · 0.85
xsum_small_negateFunction · 0.85
xsum_small_displayFunction · 0.85
xsum_small_roundFunction · 0.85

Tested by

no test coverage detected