This is the mail archive of the
java-discuss@sources.redhat.com
mailing list for the Java project.
Don't optimize by hand (Was Re: Lack of fabsf on Solaris (patch included))
Ok, this is becoming very, very interresting.
I've tried compiling 3 variants of this code, ABS1()
and ABS3() being legal C, ABS2() giving a warning.
All of them compile properly ang give me valid code
on my ARM platform (Green Hills compiler, optimized
for space, thumb mode).
But the surprising thing is that ABS1 is the variant
that generates the better, optimum code!!! (only 3
instruction, 6 byte of code, no data).
Conclusion(1): trust the compiler and write clean code!
Conclusion(2): I suggest implementing ABS1 for Math.abs(float)
Cedric
float ABS1(float x) {
return x < 0.0F ? -x : x;
}
0x1080720 ABS1: 0040 LSL R0, R0, 1
0x1080722 ABS1+0x2: 0840 LSR R0, R0, 1
0x1080724 ABS1+0x4: 46f7 MOV PC, LR
float ABS2(float x) {
*((int *)&x) &= 0x7FFFFFFF;
return x;
}
0x1080726 ABS2: b081 SUB SP, SP, 4
0x1080728 ABS2+0x2: 0040 LSL R0, R0, 1
0x108072a ABS2+0x4: 0840 LSR R0, R0, 1
0x108072c ABS2+0x6: 9000 STR R0,[SP,0]
0x108072e ABS2+0x8: b001 ADD SP, SP, 4
0x1080730 ABS2+0xa: 46f7 MOV PC, LR
float ABS3(float x) {
union { float f; long x; } tmp;
tmp.f = x;
tmp.x &= 0x7FFFFFFF;
return tmp.f;
}
0x1080732 ABS3: b081 SUB SP, SP, 4
0x1080734 ABS3+0x2: 1c01 MOV R1, R0
0x1080736 ABS3+0x4: 4668 MOV R0, SP
0x1080738 ABS3+0x6: 0049 LSL R1, R1, 1
0x108073a ABS3+0x8: 0849 LSR R1, R1, 1
0x108073c ABS3+0xa: 6001 STR R1,[R0,0]
0x108073e ABS3+0xc: 1c08 MOV R0, R1
0x1080740 ABS3+0xe: b001 ADD SP, SP, 4
0x1080742 ABS3+0x10: 46f7 MOV PC, LR