[Mesa-dev] [PATCH 12/28] util: add fp64 -> fp32 conversion support for RTNE and RTZ rounding modes

Matt Turner mattst88 at gmail.com
Tue Dec 18 02:11:26 UTC 2018


On Mon, Dec 10, 2018 at 11:25 AM Samuel Iglesias Gonsálvez
<siglesias at igalia.com> wrote:
>
> On 07/12/2018 03:03, Matt Turner wrote:
> > On Wed, Dec 5, 2018 at 7:56 AM Samuel Iglesias Gonsálvez
> > <siglesias at igalia.com> wrote:
> >>
> >> Signed-off-by: Samuel Iglesias Gonsálvez <siglesias at igalia.com>
> >> ---
> >>  src/util/Makefile.sources |   2 +
> >>  src/util/double.c         | 197 ++++++++++++++++++++++++++++++++++++++
> >>  src/util/double.h         |  46 +++++++++
> >>  src/util/meson.build      |   2 +
> >>  4 files changed, 247 insertions(+)
> >>  create mode 100644 src/util/double.c
> >>  create mode 100644 src/util/double.h
> >
> > Why do we need software routines for this? Couldn't we set/reset the
> > rounding mode (fegetround/fegetround) around a double -> float
> > conversion?
> >
>
> Yes, that was my first idea. However, I found an issue with GCC that
> forces me to do these software routines instead.
>
> I implemented _mesa_double_to_float_rtz() as:
>
> float
> _mesa_double_to_float_rtz(double val)
> {
>    int curr_method = fegetround();
>    float result;
>    fesetround(FE_TOWARDZERO);
>    result = val;
>    fesetround(curr_method);
>    return result;
> }
>
> If I add a printf, I got the proper value. However if I remove it, the
> result value is using the default's rounding mode (FE_TONEAREST). I
> think it is reordering instructions or optimizing something. I have set
> #pragma STDC FENV_ACCESS ON but it doesn't work either.
>
> Updated news: I give it another spin. I defined result as volatile
> variable and that seems to work :) My GCC knowledge is limited, so if
> someone brings a better idea to fix this, I will be delighted to test
> it. If not, I will send a v2 version with the volatile and
> fesetround()/fegetround() functions.

Dang, I had no idea it wasn't really possible to safely set the
rounding mode for conversions like this. I see lots of articles saying
"#pragma STDC FENV_ACCESS ON" isn't supported. I think your original
code might be the best thing to do.


More information about the mesa-dev mailing list