From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: Wilco Dijkstra <Wilco.Dijkstra@arm.com>,
"libc-alpha@sourceware.org" <libc-alpha@sourceware.org>,
"H . J . Lu" <hjl.tools@gmail.com>
Cc: kirill <kirill.okhotnikov@gmail.com>
Subject: Re: [PATCH v2 4/5] math: Improve fmodf
Date: Thu, 16 Mar 2023 15:38:53 -0300 [thread overview]
Message-ID: <3d3ff2cd-2187-8b54-cb4d-bbf6d9c7ed88@linaro.org> (raw)
In-Reply-To: <PAWPR08MB8982D4A9C7D6738466DB802483BC9@PAWPR08MB8982.eurprd08.prod.outlook.com>
On 16/03/23 15:11, Wilco Dijkstra wrote:
> Hi Adhemerval,
>
>
> + /* Common case where exponents are close: ey >= -103 and |x/y| < 2^8, */
> + if (__glibc_likely (ey > MANTISSA_WIDTH && ex - ey <= EXPONENT_WIDTH))
> + {
> + uint64_t mx = (hx & MANTISSA_MASK) | (MANTISSA_MASK + 1);
> + uint64_t my = (hy & MANTISSA_MASK) | (MANTISSA_MASK + 1);
> +
> + uint32_t d = (ex == ey) ? (mx - my) : (mx << (ex - ey)) % my;
> + return make_float (d, ey - 1, sx);
> + }
>
> So we do need to handle zero case here, I think fmodf (5, 1) will now
> return 0.5... We could add support in make_float (see below).
Ok, although at least for fmodf (5, 1) current algorithm does return 0.
>
> + /* Special case, both x and y are subnormal. */
> + if (__glibc_unlikely (ex == 0 && ey == 0))
> + return asfloat (sx | hx % hy);
>
> This will handle zero case correctly.
>
> + /* Convert |x| and |y| to 'mx + 2^ex' and 'my + 2^ey'. Assume that hx is
> + not subnormal by conditions above. */
> + uint32_t mx = get_mantissa (hx) | (MANTISSA_MASK + 1);
> + ex--;
> +
> + uint32_t my = get_mantissa (hy) | (MANTISSA_MASK + 1);
>
> Odd uses of get_mantissa here but not in the fast path above... They are computing
> the same value so can be shared.
>
> + mx %= my;
> +
> + if (__glibc_unlikely (mx == 0))
> + return asfloat (sx);
> +
> + if (exp_diff == 0)
> + return make_float (my, ey, sx);
>
> That should be make_float (mx, ey, sx) - no testcase caught this?!?
>
> (note both issues are in double version too)
Unfortunately no, I will add proper tests for this.
>
> --- a/sysdeps/ieee754/flt-32/math_config.h
> +++ b/sysdeps/ieee754/flt-32/math_config.h
>
> + NB: zero is not supported. */
> +static inline double
> +make_float (uint32_t x, int ep, uint32_t s)
> +{
> + int lz = __builtin_clz (x) - EXPONENT_WIDTH;
> + x <<= lz;
> + ep -= lz;
> +
> + if (__glibc_unlikely (ep < 0))
>
> We could do (ep < 0 || x == 0) to handle the zero case correctly (this works
> eventhough clz (0) is not well defined).
Ack.
>
> + {
> + x >>= -ep;
> + ep = 0;
> + }
> + return asfloat (s + x + (ep << MANTISSA_WIDTH));
> +}
> +
>
> Cheers,
> Wilco
next prev parent reply other threads:[~2023-03-16 18:38 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-03-15 20:59 [PATCH v2 0/5] Improve fmod and fmodf Adhemerval Zanella
2023-03-15 20:59 ` [PATCH v2 1/5] benchtests: Add fmod benchmark Adhemerval Zanella
2023-03-15 20:59 ` [PATCH v2 2/5] benchtests: Add fmodf benchmark Adhemerval Zanella
2023-03-15 20:59 ` [PATCH v2 3/5] math: Improve fmod Adhemerval Zanella
2023-03-16 0:58 ` H.J. Lu
2023-03-16 14:28 ` Adhemerval Zanella Netto
2023-03-16 16:13 ` Wilco Dijkstra
2023-03-16 20:39 ` Adhemerval Zanella Netto
2023-03-17 14:55 ` Wilco Dijkstra
2023-03-17 16:07 ` H.J. Lu
2023-03-17 18:22 ` Wilco Dijkstra
2023-03-15 20:59 ` [PATCH v2 4/5] math: Improve fmodf Adhemerval Zanella
2023-03-16 18:11 ` Wilco Dijkstra
2023-03-16 18:38 ` Adhemerval Zanella Netto [this message]
2023-03-16 19:15 ` Wilco Dijkstra
2023-03-16 19:45 ` Adhemerval Zanella Netto
2023-03-15 20:59 ` [PATCH v2 5/5] math: Remove the error handling wrapper from fmod and fmodf Adhemerval Zanella
2023-03-16 17:21 ` Wilco Dijkstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3d3ff2cd-2187-8b54-cb4d-bbf6d9c7ed88@linaro.org \
--to=adhemerval.zanella@linaro.org \
--cc=Wilco.Dijkstra@arm.com \
--cc=hjl.tools@gmail.com \
--cc=kirill.okhotnikov@gmail.com \
--cc=libc-alpha@sourceware.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).