We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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}
__asm static int Sign(int x)
{
CMP r0, #0
IT NE
MOVNE r0, #1
IT MI
MOVMI r0, #-1
BX LR
}
Any suggestions?