"impossible constraint in 'asm'" error when adding single-precision hard-float 'sqrt'

I want to use the single-precision hard-float sqrt instruction as my FPU supports sqrt operation, and I made some changes to Newlib:

  1. Create a new C file named ef_sqrt.c under newlib/libm/machine/riscv . The content of the new file is
#include <math.h>

float
__ieee754_sqrtf(float x)
{
	asm ("fsqrt.s %0, %1" : "=f" (x) : "f" (x));
	return x;
} 
  1. Modify the corresponding Makefile.am and Makefile.in under newlib/libm/machine/riscv to include ef_sqrt.c .

When I tried to build the toolchain using riscv-none-embed-gcc-xpackļ¼ŒI got the ā€œimpossible constraint in ā€˜asmā€™ā€ error at the inline assembly line asm ("fsqrt.s %0, %1" : "=f" (x) : "f" (x)); in my new file.

Hi Remember,

Iā€™m not sure about the impossible constraint in ā€˜asmā€™ā€ error message but would this thread below help you?

I would guess that you have a multilib build, and the soft-float multilibs are giving you the impossible constraint error because they donā€™t have FP registers. The code needs to be conditional on the existance of FP registers.

By the way, Keith Packard recently posted patches upstream for fma and sqrt support. I would suggest using that code. See for instance

The forum software is doing something funny with URLs. It seems I have to switch into URL mode to add a link correctly. Sigh.

https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libm/machine/riscv/ef_sqrt.c;h=1f378f547df436f31ed9d048d9fb7ec95bff2a1d;hb=HEAD

1 Like

Thank you. That solves my problem.