public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: libc-alpha@sourceware.org,
	Adhemerval Zanella <adhemerval.zanella@linaro.org>
Subject: Re: [PATCH 5/7] riscv: Fix feenvupdate with FE_DFL_ENV (BZ 31022)
Date: Mon, 06 Nov 2023 01:21:46 +0100	[thread overview]
Message-ID: <87031122.BzKH3j3Lxt@nimes> (raw)
In-Reply-To: <20231103122416.2724355-6-adhemerval.zanella@linaro.org>

[-- Attachment #1: Type: text/plain, Size: 1062 bytes --]

This code gave me puzzles:

  long int env = (long int) envp - (long int) FE_DFL_ENV;
  if (env != 0)
    env = *envp;

* What is the intent of the code?
* Can signed integer overflow happen in the first line? (It cannot, due to
  the value of FE_DFL_ENV being -1 and envp being otherwise aligned mod 4.
  But one should not have to think that far.)
* It assumes that intptr_t is the same as 'long int'. Which is not
  universally true.

I would suggest to replace the function with this code:

libc_feupdateenv_riscv (const fenv_t *envp)
{
  long int env = (envp != FE_DFL_ENV ? *envp : 0);

  _FPU_SETCW (env | riscv_getflags ());
}

* It is clearer.
* It does not make assumptions regarding FE_DFL_ENV or alignment.
* It produces the exact same code; see attached foo.c and foo.s,
  generated by "riscv64-linux-gnu-gcc -O2 -S foo.c".

Probably the idiom that you copied was meant to allow the use of a
conditional load instruction. But I would avoid such micro-optimizations
and instead rely on the compiler to choose the best instructions for a
task.

Bruno

[-- Attachment #2: foo.c --]
[-- Type: text/x-csrc, Size: 632 bytes --]

typedef unsigned int fenv_t;
#define FE_DFL_ENV      ((__const fenv_t *) -1)
# define _FPU_SETCW(cw) __asm__ volatile ("fssr %z0" : : "rJ" (cw))

static __attribute__ ((__always_inline__)) int
riscv_getflags (void)
{
  int flags;
  asm volatile ("frflags %0" : "=r" (flags));
  return flags;
}

void
libc_feupdateenv_riscv_az (const fenv_t *envp)
{
  long int env = (long int) envp - (long int) FE_DFL_ENV;
  if (env != 0)
    env = *envp;

  _FPU_SETCW (env | riscv_getflags ());
}

void
libc_feupdateenv_riscv_bh (const fenv_t *envp)
{
  long int env = (envp != FE_DFL_ENV ? *envp : 0);

  _FPU_SETCW (env | riscv_getflags ());
}

[-- Attachment #3: foo.s --]
[-- Type: text/plain, Size: 839 bytes --]

	.file	"foo.c"
	.option pic
	.text
	.align	1
	.globl	libc_feupdateenv_riscv_az
	.type	libc_feupdateenv_riscv_az, @function
libc_feupdateenv_riscv_az:
	li	a5,-1
	li	a4,0
	beq	a0,a5,.L2
	lwu	a4,0(a0)
.L2:
#APP
# 9 "foo.c" 1
	frflags a5
# 0 "" 2
#NO_APP
	sext.w	a5,a5
	or	a5,a5,a4
#APP
# 20 "foo.c" 1
	fssr a5
# 0 "" 2
#NO_APP
	ret
	.size	libc_feupdateenv_riscv_az, .-libc_feupdateenv_riscv_az
	.align	1
	.globl	libc_feupdateenv_riscv_bh
	.type	libc_feupdateenv_riscv_bh, @function
libc_feupdateenv_riscv_bh:
	li	a5,-1
	li	a4,0
	beq	a0,a5,.L6
	lwu	a4,0(a0)
.L6:
#APP
# 9 "foo.c" 1
	frflags a5
# 0 "" 2
#NO_APP
	sext.w	a5,a5
	or	a5,a5,a4
#APP
# 28 "foo.c" 1
	fssr a5
# 0 "" 2
#NO_APP
	ret
	.size	libc_feupdateenv_riscv_bh, .-libc_feupdateenv_riscv_bh
	.ident	"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0"
	.section	.note.GNU-stack,"",@progbits

  reply	other threads:[~2023-11-06  0:21 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-03 12:24 [PATCH 0/7] Multiple floating-point environment fixes Adhemerval Zanella
2023-11-03 12:24 ` [PATCH 1/7] powerpc: Do not raise exception traps for fesetexcept/fesetexceptflag (BZ 30988) Adhemerval Zanella
2023-11-03 12:24 ` [PATCH 2/7] i686: Do not raise exception traps on fesetexcept (BZ 30989) Adhemerval Zanella
2023-11-06  0:15   ` Bruno Haible
2023-11-06 11:24     ` Adhemerval Zanella Netto
2023-11-03 12:24 ` [PATCH 3/7] x86: Do not raises floating-point exception traps on fesetexceptflag (BZ 30990) Adhemerval Zanella
2023-11-06  0:17   ` Bruno Haible
2023-11-06 11:24     ` Adhemerval Zanella Netto
2023-11-03 12:24 ` [PATCH 4/7] manual: Clarify undefined behavior of feenableexcept (BZ 31019) Adhemerval Zanella
2023-11-03 16:20   ` Joseph Myers
2023-11-06  0:17     ` Bruno Haible
2023-11-06 11:25       ` Adhemerval Zanella Netto
2023-11-03 12:24 ` [PATCH 5/7] riscv: Fix feenvupdate with FE_DFL_ENV (BZ 31022) Adhemerval Zanella
2023-11-06  0:21   ` Bruno Haible [this message]
2023-11-06 11:28     ` Adhemerval Zanella Netto
2023-11-06  0:24   ` Bruno Haible
2023-11-06 11:32     ` Adhemerval Zanella Netto
2024-01-09 13:44   ` Szabolcs Nagy
2024-01-09 18:16     ` Adhemerval Zanella Netto
2023-11-03 12:24 ` [PATCH 6/7] alpha: Fix fesetexceptflag (BZ 30998) Adhemerval Zanella
2023-11-03 12:24 ` [PATCH 7/7] hppa: Fix undefined behaviour in feclearexcept (BZ 30983) Adhemerval Zanella
2023-11-06  0:25 ` [PATCH 0/7] Multiple floating-point environment fixes Bruno Haible

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=87031122.BzKH3j3Lxt@nimes \
    --to=bruno@clisp.org \
    --cc=adhemerval.zanella@linaro.org \
    --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).