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

How to get absolute value of a 32-bit signed integer as fast as possible?

Hi.

I wonder how to calculate absolute value of a 32-bit signed integer in C as fast as possible. I saw that there is a FPU instruction VABS.F32, which do that in one cycle (above the floats). I thought, if it is possible to use it also with integers (sign bit is in both cases MSB bit). Maybe in some way with inline or embedded assembly?

Or do you have any other advice how to get absolute value of an integer in C in the fastest way.

Not to forget - I am speaking regarding Cortex-M4 processor.

Thanks

Parents
  • Hello.

    I found out that sequence of ASR, EOR and SUB takes the same amount of instructions as if I simply write a C code like:   if(x < 0) x = -x; (of course with optimization level 3 in Keil, otherwise "if" version takes much more instructions)

    It is true, that if branching is preferred to be avoid, then ASR, EOR, SUB combination is better then "if". Otherwise, I am not really sure.

    Thank you all for the replies.

Reply
  • Hello.

    I found out that sequence of ASR, EOR and SUB takes the same amount of instructions as if I simply write a C code like:   if(x < 0) x = -x; (of course with optimization level 3 in Keil, otherwise "if" version takes much more instructions)

    It is true, that if branching is preferred to be avoid, then ASR, EOR, SUB combination is better then "if". Otherwise, I am not really sure.

    Thank you all for the replies.

Children
No data