This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Fast integer sign() function on Cortex M4

Hello,

I'm looking for an efficient way of generating a function that returns the sign of an integer (+1 if x>0, -1 if x<0, and 0 if x==0).

The function below seems to work OK (I'm using Keil uVision) but I cannot help feeling that there is an even quicker way.

__asm static int Sign(int x)
{
  CMP r0, #0
  IT NE
  MOVNE r0, #1
  IT MI
  MOVMI r0, #-1
  BX LR
}

Any suggestions?