<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 19, 2017 at 2:47 PM, Matt Arsenault <span dir="ltr"><<a href="mailto:arsenm2@gmail.com" target="_blank">arsenm2@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">This fails conformance for me:<br>
<br>
<br></blockquote><div><br>Interestingly enough some (but not all) of the test inputs start to pass when I forcefully enable subnormal support in libclc (--enable-runtime-subnormal and also updating generic/lib/shared/<a href="http://subnormal_config.cl">subnormal_config.cl</a> to enable 32-bit subnormals).<br><br></div><div>I'll be playing with this a bit as time permits.<br><br></div><div>--Aaron<br></div><div><br> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><br>
> On Jan 18, 2017, at 20:04, Aaron Watry via Libclc-dev <<a href="mailto:libclc-dev@lists.llvm.org">libclc-dev@lists.llvm.org</a>> wrote:<br>
><br>
> Mostly ported from the amd-builtins branch.<br>
><br>
> The amd-builtins branch uses __amdil_improved_fdiv_f32 and FTZ which aren't available in generic CLC.<br>
><br>
> __amdil_improved_fdiv_f32 points to native_divide which does native_recip(y)*x.<br>
><br>
> Since we don't have native_divide or native_recip yet, I've just stuck an actual division here.<br>
><br>
> I've taken a shot at a replacement for FTZ(x), but feel free to suggest alternatives.<br>
><br>
> Tested via piglit on a Radeon HD 7850 using the tests just sent to that list.<br>
><br>
> v2: Use __builtin_canonicalizef(float) instead of custom flush-to-zero function<br>
><br>
> Signed-off-by: Aaron Watry <<a href="mailto:awatry@gmail.com">awatry@gmail.com</a>><br>
> CC: Tom Stellard <<a href="mailto:thomas.stellard@amd.com">thomas.stellard@amd.com</a>><br>
> CC: Matt Arsenault <<a href="mailto:Matthew.Arsenault@amd.com">Matthew.Arsenault@amd.com</a>><br>
> ---<br>
> generic/include/clc/clc.h | 1 +<br>
> generic/include/clc/math/<wbr>remainder.h | 2 +<br>
> generic/include/clc/math/<wbr>remainder.inc | 1 +<br>
> generic/lib/SOURCES | 1 +<br>
> generic/lib/math/<a href="http://remainder.cl" rel="noreferrer" target="_blank">remainder.cl</a> | 508 ++++++++++++++++++++++++++++++<wbr>+++<br>
> 5 files changed, 513 insertions(+)<br>
> create mode 100644 generic/include/clc/math/<wbr>remainder.h<br>
> create mode 100644 generic/include/clc/math/<wbr>remainder.inc<br>
> create mode 100644 generic/lib/math/<a href="http://remainder.cl" rel="noreferrer" target="_blank">remainder.cl</a><br>
><br>
> diff --git a/generic/include/clc/clc.h b/generic/include/clc/clc.h<br>
> index 024bf27..2d4af4b 100644<br>
> --- a/generic/include/clc/clc.h<br>
> +++ b/generic/include/clc/clc.h<br>
> @@ -82,6 +82,7 @@<br>
> #include <clc/math/nextafter.h><br>
> #include <clc/math/pow.h><br>
> #include <clc/math/pown.h><br>
> +#include <clc/math/remainder.h><br>
> #include <clc/math/rint.h><br>
> #include <clc/math/round.h><br>
> #include <clc/math/sin.h><br>
> diff --git a/generic/include/clc/math/<wbr>remainder.h b/generic/include/clc/math/<wbr>remainder.h<br>
> new file mode 100644<br>
> index 0000000..97d9fad<br>
> --- /dev/null<br>
> +++ b/generic/include/clc/math/<wbr>remainder.h<br>
> @@ -0,0 +1,2 @@<br>
> +#define __CLC_BODY <clc/math/remainder.inc><br>
> +#include <clc/math/gentype.inc><br>
> diff --git a/generic/include/clc/math/<wbr>remainder.inc b/generic/include/clc/math/<wbr>remainder.inc<br>
> new file mode 100644<br>
> index 0000000..00d0d69<br>
> --- /dev/null<br>
> +++ b/generic/include/clc/math/<wbr>remainder.inc<br>
> @@ -0,0 +1 @@<br>
> +_CLC_OVERLOAD _CLC_DECL __CLC_GENTYPE remainder(__CLC_GENTYPE x, __CLC_GENTYPE y);<br>
> diff --git a/generic/lib/SOURCES b/generic/lib/SOURCES<br>
> index 517daba..39208bf 100644<br>
> --- a/generic/lib/SOURCES<br>
> +++ b/generic/lib/SOURCES<br>
> @@ -113,6 +113,7 @@ math/<a href="http://tables.cl" rel="noreferrer" target="_blank">tables.cl</a><br>
> math/<a href="http://clc_nextafter.cl" rel="noreferrer" target="_blank">clc_nextafter.cl</a><br>
> math/<a href="http://nextafter.cl" rel="noreferrer" target="_blank">nextafter.cl</a><br>
> math/<a href="http://pown.cl" rel="noreferrer" target="_blank">pown.cl</a><br>
> +math/<a href="http://remainder.cl" rel="noreferrer" target="_blank">remainder.cl</a><br>
> math/<a href="http://sin.cl" rel="noreferrer" target="_blank">sin.cl</a><br>
> math/<a href="http://sincos.cl" rel="noreferrer" target="_blank">sincos.cl</a><br>
> math/<a href="http://sincos_helpers.cl" rel="noreferrer" target="_blank">sincos_helpers.cl</a><br>
> diff --git a/generic/lib/math/<a href="http://remainder.cl" rel="noreferrer" target="_blank">remainder.<wbr>cl</a> b/generic/lib/math/<a href="http://remainder.cl" rel="noreferrer" target="_blank">remainder.<wbr>cl</a><br>
> new file mode 100644<br>
> index 0000000..df42db0<br>
> --- /dev/null<br>
> +++ b/generic/lib/math/<a href="http://remainder.cl" rel="noreferrer" target="_blank">remainder.<wbr>cl</a><br>
> @@ -0,0 +1,508 @@<br>
> +#include <clc/clc.h><br>
> +<br>
> +#include "math.h"<br>
> +#include "config.h"<br>
> +#include "../clcmacro.h"<br>
> +<br>
> +inline float _clc_remainder_<wbr>scaleFullRangef32(float y, float t) {<br>
> + float ay, ty, r = 0;<br>
> + int k, iiy, iy, exp_iy0, exp_iy, manty, signy, miy;<br>
> + int delta, shift, ir;<br>
> +<br>
> + ay = fabs(t);<br>
> + k = ay > 1024 ? 1024 : (int) ay;<br>
> + k = t < 0 ? -k : k;<br>
> + t = (float) k;<br>
> +<br>
> + iiy = as_int(y);<br>
> + iy = iiy & EXSIGNBIT_SP32;<br>
> + signy = iiy & SIGNBIT_SP32;<br>
> + ay = as_float(iy);<br>
> +<br>
> + exp_iy0 = iy & EXPBITS_SP32;<br>
> + manty = iy & MANTBITS_SP32;<br>
> +<br>
> + //sub-normal<br>
> + ty = exp_iy0 == 0 ? (float) manty : as_float(iy);<br>
> + k = exp_iy0 == 0 ? k - 149 : k;<br>
> + ay = ty;<br>
> + iy = as_int(ay);<br>
> + exp_iy0 = iy & EXPBITS_SP32;<br>
> + exp_iy = (exp_iy0 >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;<br>
> + // add k to y's exponent<br>
> + r = as_float(iy + (k << EXPSHIFTBITS_SP32));<br>
> + r = (exp_iy + k) > 127 ? as_float(PINFBITPATT_SP32) : r;<br>
> + // add k to y's exponent<br>
> + delta = -126 - (exp_iy + k);<br>
> +<br>
> + // sub-normal<br>
> + miy = iy & MANTBITS_SP32;<br>
> + miy |= IMPBIT_SP32;<br>
> + shift = delta > 23 ? 24 : delta;<br>
> + shift = delta < 0 ? 0 : shift;<br>
> + miy >>= shift;<br>
> + r = delta > 0 ? as_float(miy) : r;<br>
> + r = t > (float) (2 * EMAX_SP32) ? as_float(PINFBITPATT_SP32) : r;<br>
> + ir = as_int(r);<br>
> + r = ir <= PINFBITPATT_SP32 ? as_float(as_int(r) | signy) : r;<br>
> + return r;<br>
> +}<br>
> +/* Scales the float x by 2.0**n.<br>
> +Assumes 2*EMIN <= n <= 2*EMAX, though this condition is not checked. */<br>
> +inline float _clc_remainder_scaleFloat_2(<wbr>float x, int n) {<br>
> + float t1, t2;<br>
> + int n1, n2;<br>
> + n1 = n / 2;<br>
> + n2 = n - n1;<br>
> + /* Construct the numbers t1 = 2.0**n1 and t2 = 2.0**n2 */<br>
> + t1 = as_float((n1 + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);<br>
> + t2 = as_float((n2 + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);<br>
> + return (x * t1) * t2;<br>
> +}<br>
> +/* Scales the float x by 2.0**n.<br>
> + Assumes EMIN <= n <= EMAX, though this condition is not checked. */<br>
> +inline float _clc_remainder_scaleFloat_1(<wbr>float x, int n) {<br>
> + float t;<br>
> + /* Construct the number t = 2.0**n */<br>
> + t = as_float((n + EXPBIAS_SP32) << EXPSHIFTBITS_SP32);<br>
> + return x * t;<br>
> +}<br>
> +/* Computes the exact product of x and y, the result being the<br>
> +nearly double length number (z,zz) */<br>
> +inline void _clc_remainder_mul12f(float x, float y, float *z, float *zz) {<br>
> + float hx, tx, hy, ty;<br>
> + // Split x into hx (head) and tx (tail). Do the same for y.<br>
> + uint u;<br>
> + u = as_uint(x);<br>
> + u &= 0xfffff000;<br>
> + hx = as_float(u);<br>
> + tx = x - hx;<br>
> + u = as_uint(y);<br>
> + u &= 0xfffff000;<br>
> + hy = as_float(u);<br>
> + ty = y - hy;<br>
> + *z = x * y;<br>
> + *zz = (((hx * hy - *z) + hx * ty) + tx * hy) + tx * ty;<br>
> +}<br>
> +<br>
> +_CLC_OVERLOAD _CLC_DEF float remainder(float x, float y) {<br>
> + if (!__clc_fp32_subnormals_<wbr>supported()) {<br>
> + const int loop_scale = 12;<br>
> + const float fscale = 1.0f / (float) (1 << loop_scale);<br>
> +<br>
> + int ntimes;<br>
> + float ret = 0;<br>
> + int ui_x, ui_y, ui_ax, ui_ay, xexp, yexp, signx;<br>
> + float af_x, af_y, af_ybase, fx, fxp, fxm, fy, w, scale, t, c, cc, v;<br>
> + float yscale, scaled_w, saved_w, div, sdiv, ratio, sratio, fxexp, sub_fx;<br>
> + int iw_scaled, wexp, it, i, ifx, ex, ey;<br>
> + ;<br>
> + float xr, xr0, xr_base, yr;<br>
> + uint q;<br>
> +<br>
> + ui_x = as_int(x);<br>
> + ui_y = as_int(y);<br>
> + ui_ax = ui_x & EXSIGNBIT_SP32;<br>
> + ui_ay = ui_y & EXSIGNBIT_SP32;<br>
> +<br>
> + /* special case handle */<br>
> + if (ui_ax > PINFBITPATT_SP32)<br>
> + return x;<br>
> + if (ui_ax == PINFBITPATT_SP32)<br>
> + return as_float(QNANBITPATT_SP32);<br>
> + if (ui_ay > PINFBITPATT_SP32)<br>
> + return y;<br>
> + if (ui_ay == PINFBITPATT_SP32)<br>
> + return x;<br>
> + if (ui_ay == 0 && ui_ax == 0)<br>
> + return as_float(QNANBITPATT_SP32);<br>
> + if (ui_ax == 0)<br>
> + return x;<br>
> + if (ui_ay == 0)<br>
> + return as_float(QNANBITPATT_SP32);<br>
> +<br>
> + signx = ui_x & SIGNBIT_SP32;<br>
> + af_x = as_float(ui_ax);<br>
> + af_ybase = af_y = as_float(ui_ay);<br>
> + yexp = (int) ((ui_y & EXPBITS_SP32) >> EXPSHIFTBITS_SP32);<br>
> +<br>
> + yscale = (float) ((yexp < 48 && ui_ay != 0) ? (48 - yexp) : 0);<br>
> + if (yscale != 0) {<br>
> + af_y = _clc_remainder_<wbr>scaleFullRangef32(af_ybase, yscale);<br>
> + }<br>
> +<br>
> + ui_y = as_int(af_y);<br>
> + yexp = (int) ((ui_y & EXPBITS_SP32) >> EXPSHIFTBITS_SP32);<br>
> + xexp = (int) ((ui_x & EXPBITS_SP32) >> EXPSHIFTBITS_SP32);<br>
> + fx = af_x;<br>
> + fy = af_y;<br>
> +<br>
> + /* Set ntimes to the number of times we need to do a<br>
> + partial remainder. If the exponent of x is an exact multiple<br>
> + of 24 larger than the exponent of y, and the mantissa of x is<br>
> + less than the mantissa of y, ntimes will be one too large<br>
> + but it doesn't matter - it just means that we'll go round<br>
> + the loop below one extra time. */<br>
> + ntimes = (xexp - yexp) / loop_scale;<br>
> + ntimes = xexp <= yexp ? 0 : ntimes;<br>
> +<br>
> + /* Set w = y * 2^(ntimes*loop_scale) */<br>
> + w = _clc_remainder_scaleFloat_2(<wbr>fy, ntimes * loop_scale);<br>
> + w = ntimes == 0 ? fy : w;<br>
> +<br>
> + /* Set scale = 2^(-loop_scale) */<br>
> + scale = ntimes == 0 ? 1.0f : fscale;<br>
> +<br>
> + // make sure recip does not overflow<br>
> + wexp = (int) ((as_int(w) & EXPBITS_SP32) >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;<br>
> + saved_w = w;<br>
> + scaled_w = _clc_remainder_scaleFloat_1(w, -14);<br>
> + iw_scaled = wexp > 105 & wexp <= 127;<br>
> + w = (iw_scaled & ntimes) > 0 ? scaled_w : w;<br>
> +<br>
> + /* Each time round the loop we compute a partial remainder.<br>
> + This is done by subtracting a large multiple of w<br>
> + from x each time, where w is a scaled up version of y.<br>
> + The subtraction can be performed exactly when performed<br>
> + in double precision, and the result at each stage can<br>
> + fit exactly in a single precision number. */<br>
> + for (i = 0; i < ntimes; i++) {<br>
> + /* Set fx = fx - w * t, where t is equal to trunc(dx/w). */<br>
> + div = fx / w; //was __amdil_improved_div_f32 => native_div(x, y) => native_recip(y)*x<br>
> + sdiv = _clc_remainder_scaleFloat_1(<wbr>div, -14);<br>
> + div = iw_scaled ? sdiv : div;<br>
> + t = floor(div);<br>
> + w = saved_w;<br>
> + iw_scaled = 0;<br>
> +<br>
> + /* At this point, t may be one too large due to rounding of fx/w */<br>
> +<br>
> + /* Compute w * t in quad precision */<br>
> + _clc_remainder_mul12f(w, t, &c, &cc);<br>
> +<br>
> + /* Subtract w * t from fx */<br>
> + v = fx - c;<br>
> + fx = v + (((fx - v) - c) - cc);<br>
> +<br>
> + /* If t was one too large, fx will be negative. Add back one w */<br>
> + /* It might be possible to speed up this loop by finding<br>
> + a way to compute correctly truncated t directly from fx and w.<br>
> + We would then avoid the need for this check on negative fx. */<br>
> + fxp = fx + w;<br>
> + fxm = fx - w;<br>
> + fx = fx < 0.0f ? fxp : fx;<br>
> + fx = fx >= w ? fxm : fx;<br>
> +<br>
> + /* Scale w down by for the next iteration */<br>
> + w *= scale;<br>
> + saved_w = w;<br>
> + }<br>
> +<br>
> + /* One more time */<br>
> + // iw = as_int(w);<br>
> + ifx = as_int(fx);<br>
> + fxexp = (int) ((ifx & EXPBITS_SP32) >> EXPSHIFTBITS_SP32);<br>
> + // wexp = (int) ((iw & EXPBITS_SP32) >> EXPSHIFTBITS_SP32);<br>
> + sub_fx = fx;<br>
> + // make sure recip does not overflow<br>
> + wexp = (int) ((as_int(w) & EXPBITS_SP32) >> EXPSHIFTBITS_SP32) - EXPBIAS_SP32;<br>
> + saved_w = w;<br>
> + scaled_w = _clc_remainder_scaleFloat_1(w, -14);<br>
> + iw_scaled = wexp > 105 & wexp <= 127;<br>
> + w = iw_scaled ? scaled_w : w;<br>
> + ratio = fx / w; //was the amdil equivalent of native_divide(x, y) => native_recip(y)*x;<br>
> + sratio = _clc_remainder_scaleFloat_1(<wbr>ratio, -14);<br>
> + ratio = iw_scaled ? sratio : ratio;<br>
> + t = floor(ratio);<br>
> + it = (int) t;<br>
> +<br>
> + w = saved_w;<br>
> + _clc_remainder_mul12f(w, t, &c, &cc);<br>
> +<br>
> + v = fx - c;<br>
> + fx = v + (((fx - v) - c) - cc);<br>
> +<br>
> + if (fx < 0.0f) {<br>
> + fx += w;<br>
> + it--;<br>
> + }<br>
> +<br>
> + if (fx >= w) {<br>
> + fx -= w;<br>
> + it++;<br>
> + }<br>
> +<br>
> + // sub-normal fax<br>
> + fx = fxexp == 0 ? sub_fx : fx;<br>
> +<br>
> + float scaleback = 0;<br>
> +<br>
> + // in case fx == 0 and we'got a divisor<br>
> + it = (yscale > 30) ? 0 : ((unsigned int) it << (int) yscale);<br>
> +<br>
> + if (as_int(fx) != 0 && yscale != 0) {<br>
> + xr = fx;<br>
> + xr_base = fx;<br>
> + yr = af_ybase;<br>
> + q = 0;<br>
> + ex = ilogb(fx);<br>
> + ey = ilogb(af_ybase);<br>
> +<br>
> + yr = (float) _clc_remainder_<wbr>scaleFullRangef32(af_ybase, (float) -ey);<br>
> + xr = (float) _clc_remainder_<wbr>scaleFullRangef32(fx, (float) -ex);<br>
> +<br>
> + for (i = ex - ey; i > 0; i--) {<br>
> + q <<= 1;<br>
> + xr0 = xr;<br>
> + xr = (xr0 >= yr) ? xr0 - yr : xr0;<br>
> + q = (xr0 >= yr) ? q + 1 : q;<br>
> + xr += xr;<br>
> + }<br>
> + q <<= 1;<br>
> + xr0 = xr;<br>
> + xr = (xr0 >= yr) ? xr0 - yr : xr0;<br>
> + q = (xr0 >= yr) ? q + 1 : q;<br>
> + xr = _clc_remainder_<wbr>scaleFullRangef32(xr, (float) ey);<br>
> +<br>
> + fx = (ex - ey >= 0) ? xr : xr_base;<br>
> + q = (ex - ey >= 0) ? q : 0;<br>
> + it += q;<br>
> +<br>
> + xexp = (int) ((as_int(fx) & EXPBITS_SP32) >> EXPSHIFTBITS_SP32);<br>
> +<br>
> + w = af_ybase;<br>
> + if (xexp < 24) {<br>
> + fx = _clc_remainder_<wbr>scaleFullRangef32(fx, 48);<br>
> + w = _clc_remainder_<wbr>scaleFullRangef32(af_ybase, 48);<br>
> + scaleback = -48;<br>
> + }<br>
> + }<br>
> + /* At this point, dx lies in the range [0,dy) */<br>
> + /* For the remainder function, we need to adjust dx<br>
> + so that it lies in the range (-y/2, y/2] by carefully<br>
> + subtracting w (== fy == y) if necessary. */<br>
> + if (fx * 2.f > w || ((fx * 2.f == w) && (it & 1))) {<br>
> + fx -= w;<br>
> + it++;<br>
> + }<br>
> + if (scaleback != 0) {<br>
> + fx = _clc_remainder_<wbr>scaleFullRangef32(fx, scaleback);<br>
> + }<br>
> +<br>
> + ret = (signx) ? as_float(as_int(fx) ^ SIGNBIT_SP32) : fx;<br>
> +<br>
> + return ret;<br>
> +<br>
> + }<br>
> +<br>
> + //Otherwise, Subnormals are supported<br>
> +<br>
> + x = __builtin_canonicalizef(x);<br>
> + y = __builtin_canonicalizef(y);<br>
> +<br>
> + int ux = as_int(x);<br>
> + int ax = ux & EXSIGNBIT_SP32;<br>
> + float xa = as_float(ax);<br>
> + int sx = ux ^ ax;<br>
> + int ex = ax >> EXPSHIFTBITS_SP32;<br>
> +<br>
> + int uy = as_int(y);<br>
> + int ay = uy & EXSIGNBIT_SP32;<br>
> + float ya = as_float(ay);<br>
> + int ey = ay >> EXPSHIFTBITS_SP32;<br>
> +<br>
> + float xr = as_float(0x3f800000 | (ax & 0x007fffff));<br>
> + float yr = as_float(0x3f800000 | (ay & 0x007fffff));<br>
> + int c;<br>
> + int k = ex - ey;<br>
> +<br>
> + uint q = 0;<br>
> +<br>
> +#define _CLC_BIT c = xr >= yr; q = (q << 1) | c; xr -= c ? yr : 0.0f; xr += xr<br>
> +<br>
> + while (k > 3) {<br>
> + _CLC_BIT;<br>
> + _CLC_BIT;<br>
> + _CLC_BIT;<br>
> + _CLC_BIT;<br>
> + k -= 4;<br>
> + }<br>
> +<br>
> + while (k > 0) {<br>
> + _CLC_BIT;<br>
> + --k;<br>
> + }<br>
> +<br>
> +#undef _CLC_BIT<br>
> +<br>
> + c = xr > yr;<br>
> + q = (q << 1) | c;<br>
> + xr -= c ? yr : 0.0f;<br>
> +<br>
> + int lt = ex < ey;<br>
> +<br>
> + q = lt ? 0 : q;<br>
> + xr = lt ? xa : xr;<br>
> + yr = lt ? ya : yr;<br>
> +<br>
> + c = (yr < 2.0f * xr) | ((yr == 2.0f * xr) & ((q & 0x1) == 0x1));<br>
> + xr -= c ? yr : 0.0f;<br>
> + q += c;<br>
> +<br>
> + float s = as_float(ey << EXPSHIFTBITS_SP32);<br>
> + xr *= lt ? 1.0f : s;<br>
> +<br>
> + c = ax == ay;<br>
> + xr = c ? 0.0f : xr;<br>
> +<br>
> + xr = as_float(sx ^ as_int(xr));<br>
> +<br>
> + c = ax > PINFBITPATT_SP32 | ay > PINFBITPATT_SP32 | ax == PINFBITPATT_SP32 | ay == 0;<br>
> + xr = c ? as_float(QNANBITPATT_SP32) : xr;<br>
> +<br>
> + return xr;<br>
> +}<br>
> +<br>
> +_CLC_BINARY_VECTORIZE(_CLC_<wbr>OVERLOAD _CLC_DEF, float, remainder, float, float);<br>
> +<br>
> +#ifdef cl_khr_fp64<br>
> +<br>
> +#pragma OPENCL EXTENSION cl_khr_fp64 : enable<br>
> +<br>
> +inline double<br>
> +_clc_remainder_ldexp(double x, int n) {<br>
> + // XXX Have to go twice here because the hardware can't handle the full range (yet)<br>
> + int nh = n >> 1;<br>
> + return ldexp(ldexp(x, nh), n - nh);<br>
> +}<br>
> +<br>
> +_CLC_OVERLOAD _CLC_DEF double remainder(double y, double x) {<br>
> + ulong ux = as_ulong(x);<br>
> + ulong ax = ux & ~SIGNBIT_DP64;<br>
> + ulong xsgn = ux ^ ax;<br>
> + double dx = as_double(ax);<br>
> + int xexp = convert_int(ax >> EXPSHIFTBITS_DP64);<br>
> + int xexp1 = 11 - (int) clz(ax & MANTBITS_DP64);<br>
> + xexp1 = xexp < 1 ? xexp1 : xexp;<br>
> +<br>
> + ulong uy = as_ulong(y);<br>
> + ulong ay = uy & ~SIGNBIT_DP64;<br>
> + double dy = as_double(ay);<br>
> + int yexp = convert_int(ay >> EXPSHIFTBITS_DP64);<br>
> + int yexp1 = 11 - (int) clz(ay & MANTBITS_DP64);<br>
> + yexp1 = yexp < 1 ? yexp1 : yexp;<br>
> +<br>
> + int qsgn = ((ux ^ uy) & SIGNBIT_DP64) == 0UL ? 1 : -1;<br>
> +<br>
> + // First assume |x| > |y|<br>
> +<br>
> + // Set ntimes to the number of times we need to do a<br>
> + // partial remainder. If the exponent of x is an exact multiple<br>
> + // of 53 larger than the exponent of y, and the mantissa of x is<br>
> + // less than the mantissa of y, ntimes will be one too large<br>
> + // but it doesn't matter - it just means that we'll go round<br>
> + // the loop below one extra time.<br>
> + int ntimes = max(0, (xexp1 - yexp1) / 53);<br>
> + double w = _clc_remainder_ldexp(dy, ntimes * 53);<br>
> + w = ntimes == 0 ? dy : w;<br>
> + double scale = ntimes == 0 ? 1.0 : 0x1.0p-53;<br>
> +<br>
> + // Each time round the loop we compute a partial remainder.<br>
> + // This is done by subtracting a large multiple of w<br>
> + // from x each time, where w is a scaled up version of y.<br>
> + // The subtraction must be performed exactly in quad<br>
> + // precision, though the result at each stage can<br>
> + // fit exactly in a double precision number.<br>
> + int i;<br>
> + double t, v, p, pp;<br>
> +<br>
> + for (i = 0; i < ntimes; i++) {<br>
> + // Compute integral multiplier<br>
> + t = trunc(dx / w);<br>
> +<br>
> + // Compute w * t in quad precision<br>
> + p = w * t;<br>
> + pp = fma(w, t, -p);<br>
> +<br>
> + // Subtract w * t from dx<br>
> + v = dx - p;<br>
> + dx = v + (((dx - v) - p) - pp);<br>
> +<br>
> + // If t was one too large, dx will be negative. Add back one w.<br>
> + dx += dx < 0.0 ? w : 0.0;<br>
> +<br>
> + // Scale w down by 2^(-53) for the next iteration<br>
> + w *= scale;<br>
> + }<br>
> +<br>
> + // One more time<br>
> + // Variable todd says whether the integer t is odd or not<br>
> + t = floor(dx / w);<br>
> + long lt = (long) t;<br>
> + int todd = lt & 1;<br>
> +<br>
> + p = w * t;<br>
> + pp = fma(w, t, -p);<br>
> + v = dx - p;<br>
> + dx = v + (((dx - v) - p) - pp);<br>
> + i = dx < 0.0;<br>
> + todd ^= i;<br>
> + dx += i ? w : 0.0;<br>
> +<br>
> + // At this point, dx lies in the range [0,dy)<br>
> +<br>
> + // For the fmod function, we're done apart from setting the correct sign.<br>
> + //<br>
> + // For the remainder function, we need to adjust dx<br>
> + // so that it lies in the range (-y/2, y/2] by carefully<br>
> + // subtracting w (== dy == y) if necessary. The rigmarole<br>
> + // with todd is to get the correct sign of the result<br>
> + // when x/y lies exactly half way between two integers,<br>
> + // when we need to choose the even integer.<br>
> +<br>
> + int al = (2.0 * dx > w) | (todd & (2.0 * dx == w));<br>
> + double dxl = dx - (al ? w : 0.0);<br>
> +<br>
> + int ag = (dx > 0.5 * w) | (todd & (dx == 0.5 * w));<br>
> + double dxg = dx - (ag ? w : 0.0);<br>
> +<br>
> + dx = dy < 0x1.0p+1022 ? dxl : dxg;<br>
> +<br>
> + double ret = as_double(xsgn ^ as_ulong(dx));<br>
> + dx = as_double(ax);<br>
> +<br>
> + // Now handle |x| == |y|<br>
> + int c = dx == dy;<br>
> + t = as_double(xsgn);<br>
> + ret = c ? t : ret;<br>
> +<br>
> + // Next, handle |x| < |y|<br>
> + c = dx < dy;<br>
> + ret = c ? x : ret;<br>
> +<br>
> + c &= (yexp < 1023 & 2.0 * dx > dy) | (dx > 0.5 * dy);<br>
> + // we could use a conversion here instead since qsgn = +-1<br>
> + p = qsgn == 1 ? -1.0 : 1.0;<br>
> + t = fma(y, p, x);<br>
> + ret = c ? t : ret;<br>
> +<br>
> + // We don't need anything special for |x| == 0<br>
> +<br>
> + // |y| is 0<br>
> + c = dy == 0.0;<br>
> + ret = c ? as_double(QNANBITPATT_DP64) : ret;<br>
> +<br>
> + // y is +-Inf, NaN<br>
> + c = yexp > BIASEDEMAX_DP64;<br>
> + t = y == y ? x : y;<br>
> + ret = c ? t : ret;<br>
> +<br>
> + // x is +=Inf, NaN<br>
> + c = xexp > BIASEDEMAX_DP64;<br>
> + ret = c ? as_double(QNANBITPATT_DP64) : ret;<br>
> +<br>
> + return ret;<br>
> +}<br>
> +<br>
> +_CLC_BINARY_VECTORIZE(_CLC_<wbr>OVERLOAD _CLC_DEF, double, remainder, double, double);<br>
> +#endif<br>
> \ No newline at end of file<br>
> --<br>
> 2.9.3<br>
><br>
> ______________________________<wbr>_________________<br>
> Libclc-dev mailing list<br>
> <a href="mailto:Libclc-dev@lists.llvm.org">Libclc-dev@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/libclc-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/libclc-dev</a><br>
<br>
<br></blockquote></div><br></div></div>