https://gcc.gnu.org/bugzilla/show_bug.cgi?id=121652
--- Comment #4 from Vineet Gupta <vineetg at gcc dot gnu.org> --- Also I'm thinking to model this based on existing libc implementation which has been around for a while and should hopefully be correct. What glibc does is early check for nan and return x + x if so. See [1] This implicitly - doesn't restore flags for nan case (this PR/121652) - restores flag otherwise (retains fix for PR/121534) https://github.com/bminor/glibc/blob/master/sysdeps/riscv/rv64/rvd/s_round.c This requires the gcc builtin to call equivalent of __builtin_isnan(). However looks like there's a different problem lurking under the hood so let me run it by this group who care about these subtleties. Consider following test: -O2 -march=rv64gc -mabi=lp64d ` int myisnan(double __x) { return __builtin_isnan(__x); } ` generates ` myisnan: feq.d a0,fa0,fa0 xori a0,a0,1 ret ` FEQ.D is a quite comparison meaning it *can* set the invalid operation exception flag if either input is a signaling NaN. For the purpose of this PR I can implement isnan using fclass insn, but the general question stands (I'll open a seperate PR for that, since its an independent issue of its own).