[Bug target/39085] Floating point errors on Arm9 processor
--- Comment #6 from rearnsha at gcc dot gnu dot org 2009-03-25 21:54 --- (In reply to comment #2) str r3, [fp, #-32] str r4, [fp, #-28] ldfd f0, [fp, #-32] sqtd f0, f0 stfd f0, [fp, #-40] ldfd f0, [fp, #-40] ldfd f0, [fp, #-40] cmf f0, f0 beq .L2 sub r0, fp, #32 ldmia r0, {r0-r1} blsqrt stfd f0, [fp, #-40] You need to understand what this code is doing to work out the source of the bug. The compiler is inlining a copy of the square-root function with an FPA co-processor instruction sqtd f0, f0 which, if successful will replace the library call. If the square root succeeds it will not return a NaN, so the comparison of the result cmf f0, f0 will be that the two results are equal (a NaN never compares equal to itself). So the square root function (sqrt) will only be called if a NaN was generated by the machine instruction; it does this in order to set errno. Of course, the ARM9 does not have an FPA co-processor so the original sqtd instruction will be emulated by your operating system -- I strongly suspect that that is what is generating the incorrect results and you now need to investigate why. If you still believe the bug is in the compiler you need to provide some additional information, including how you configured the compiler and what options you used when compiling your code. -- rearnsha at gcc dot gnu dot org changed: What|Removed |Added CC||rearnsha at gcc dot gnu dot ||org Status|WAITING |RESOLVED Resolution||INVALID http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39085
[Bug target/39085] Floating point errors on Arm9 processor
--- Comment #5 from mp2 at ladybridge dot com 2009-03-13 17:36 --- Is there any progress on looking at this problem? It is causing a delay in releasing software on the Arm9 processor. Thanks. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39085
[Bug target/39085] Floating point errors on Arm9 processor
--- Comment #4 from mp2 at ladybridge dot com 2009-02-16 09:10 --- (In reply to comment #3) I would generate the assembly by using a local function name rather than using main(). The original problem was deep in a complex program. I was trying to bring it down to the shortest program that demonstrated the issue. From what I'm seeing, I don't know if it might be a typecasting issue did you try something like this? (float)out = sqrt((float)in); Tried this and it still fails. Also, the problem affects many mathematical functions, not just sqrt(). I first saw it with sin(). Thanks for your help. -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39085
[Bug target/39085] Floating point errors on Arm9 processor
--- Comment #2 from mp2 at ladybridge dot com 2009-02-15 12:14 --- (In reply to comment #1) sqrt is usually implemented as a library function and GCC does not implement it for you. Do you use newlib? My previous reply doesn't seem to have made it into the system. Here it is again... As far as I can see, there is no library named newlib or anything near it on this system. What is this library and where do I get it? I am not convinced that this is a library issue as two of the sqrt() functions in the program work though the last has been pre-evaluated to a constant by the gcc compiler. The problem only happens when the result is stored in a variable. I cannot find any good documentation for the Arm9 assembler but by inspired guesswork interpreting the instruction names, the code generated for the program looks very odd as there appears to be a conditional jump around the sqrt() function. int main() { double in, out; in = 2.0; out = sqrt(in); printf(%f\n, out); printf(%f\n, sqrt(in)); printf(%f\n, sqrt(2.0)); return 0; } Assembler code... .file a.c .section .rodata .align2 .LC0: .ascii%f\012\000 .text .align2 .global main .type main, %function main: @ args = 0, pretend = 0, frame = 24 @ frame_needed = 1, uses_anonymous_args = 0 mov ip, sp stmfd sp!, {r4, fp, ip, lr, pc} sub fp, ip, #4 sub sp, sp, #24 adr r3, .L4 ldmia r3, {r3-r4} str r3, [fp, #-32] str r4, [fp, #-28] ldfd f0, [fp, #-32] sqtd f0, f0 stfd f0, [fp, #-40] ldfd f0, [fp, #-40] ldfd f0, [fp, #-40] cmf f0, f0 beq .L2 sub r0, fp, #32 ldmia r0, {r0-r1} blsqrt stfd f0, [fp, #-40] .L2: sub r3, fp, #40 ldmia r3, {r3-r4} str r3, [fp, #-24] str r4, [fp, #-20] ldr r0, .L4+16 sub r1, fp, #24 ldmia r1, {r1-r2} blprintf sub r0, fp, #32 ldmia r0, {r0-r1} blsqrt stfd f0, [sp, #-8]! ldmfd sp!, {r3, r4} ldr r0, .L4+16 mov r1, r3 mov r2, r4 blprintf ldr r0, .L4+16 adr r1, .L4+8 ldmia r1, {r1-r2} blprintf mov r3, #0 mov r0, r3 sub sp, fp, #16 ldmfd sp, {r4, fp, sp, pc} .L5: .align2 .L4: .word 1073741824 .word 0 .word 1073127582 .word 1719614413 .word .LC0 .size main, .-main .identGCC: (GNU) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) Martin Phillips Ladybridge Systems Ltd 17b Coldstream Lane, Hardingstone, Northampton, NN4 6DB +44-(0)1604-709200 -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39085
[Bug target/39085] Floating point errors on Arm9 processor
--- Comment #3 from rbala at coresfoundation dot com 2009-02-15 20:43 --- Hi all, Correct me if I'm wrong. @mp2 - newlib is a source of 'C' libraries for various arch's. You won't find a directory as such called newlib. While building your toolchain, you compile all the files in the newlib source. Coming to your issue, I shall verify it again. I don't think it is a GCC issue., but I shall verify it. I saw the assembly code, but I shall again generate the assembly myself., atleast from my perspective, I would generate the assembly by using a local function name rather than using main(). I mean I wouldn't use the fn.name as main() when I was to debug a function's assembly, because main() being a generic fn. Name might induce some other fn.calls(I may be wrong here) but what I'm saying is, its just a good practice. From what I'm seeing, I don't know if it might be a typecasting issue, (don't know for sure), did you try something like this? (float)out = sqrt((float)in); (In reply to comment #2) (In reply to comment #1) sqrt is usually implemented as a library function and GCC does not implement it for you. Do you use newlib? My previous reply doesn't seem to have made it into the system. Here it is again... As far as I can see, there is no library named newlib or anything near it on this system. What is this library and where do I get it? I am not convinced that this is a library issue as two of the sqrt() functions in the program work though the last has been pre-evaluated to a constant by the gcc compiler. The problem only happens when the result is stored in a variable. I cannot find any good documentation for the Arm9 assembler but by inspired guesswork interpreting the instruction names, the code generated for the program looks very odd as there appears to be a conditional jump around the sqrt() function. int main() { double in, out; in = 2.0; out = sqrt(in); printf(%f\n, out); printf(%f\n, sqrt(in)); printf(%f\n, sqrt(2.0)); return 0; } Assembler code... .file a.c .section .rodata .align2 .LC0: .ascii%f\012\000 .text .align2 .global main .type main, %function main: @ args = 0, pretend = 0, frame = 24 @ frame_needed = 1, uses_anonymous_args = 0 mov ip, sp stmfd sp!, {r4, fp, ip, lr, pc} sub fp, ip, #4 sub sp, sp, #24 adr r3, .L4 ldmia r3, {r3-r4} str r3, [fp, #-32] str r4, [fp, #-28] ldfd f0, [fp, #-32] sqtd f0, f0 stfd f0, [fp, #-40] ldfd f0, [fp, #-40] ldfd f0, [fp, #-40] cmf f0, f0 beq .L2 sub r0, fp, #32 ldmia r0, {r0-r1} blsqrt stfd f0, [fp, #-40] .L2: sub r3, fp, #40 ldmia r3, {r3-r4} str r3, [fp, #-24] str r4, [fp, #-20] ldr r0, .L4+16 sub r1, fp, #24 ldmia r1, {r1-r2} blprintf sub r0, fp, #32 ldmia r0, {r0-r1} blsqrt stfd f0, [sp, #-8]! ldmfd sp!, {r3, r4} ldr r0, .L4+16 mov r1, r3 mov r2, r4 blprintf ldr r0, .L4+16 adr r1, .L4+8 ldmia r1, {r1-r2} blprintf mov r3, #0 mov r0, r3 sub sp, fp, #16 ldmfd sp, {r4, fp, sp, pc} .L5: .align2 .L4: .word 1073741824 .word 0 .word 1073127582 .word 1719614413 .word .LC0 .size main, .-main .identGCC: (GNU) 4.1.2 20061115 (prerelease) (Debian 4.1.1-21) Martin Phillips Ladybridge Systems Ltd 17b Coldstream Lane, Hardingstone, Northampton, NN4 6DB +44-(0)1604-709200 -- http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39085
[Bug target/39085] Floating point errors on Arm9 processor
--- Comment #1 from pinskia at gcc dot gnu dot org 2009-02-03 19:35 --- sqrt is usually implemented as a library function and GCC does not implement it for you. Do you use newlib? -- pinskia at gcc dot gnu dot org changed: What|Removed |Added Severity|major |normal Status|UNCONFIRMED |WAITING Component|c |target http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39085