Floating point problems on Android ARM 32 bit

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Floating point problems on Android ARM 32 bit

Alexander Muth
Hello,
I  use cmake to build libtcc [1] under android which works fine with
x86, x86_64 and aarch64. But when I compile for 32 bit arm, there are
problems when doubles and floats are passed as parameter to or from
libtcc compiled code. I don't think that I have missed some Macros in my
cmake because on Linux 32bit arm it is working just fine.

e.g. the code:

#include <stdlib.h>
#include <math.h>
double internalTestFunction(double x) {
      return x+1;
}
int test1() {       /* OK on all architectures, returns 5 */
      return (int) internalTestFunction(4);
}
float test2() {      /* UNEXPECTED RESULT on arm 32 bit: expected 5.0
but returns different value (Maybe the value of the integer registers
misinterpreted because the return is not in integer registers?)*/
     return (float) internalTestFunction(4);
}
float* test2Ptr() {       /* OK on all architectures, returns 5: passing
float/double in libtcc compiled code and return pointer to float is
working just fine */
      float *resultPtr = malloc(sizeof(float));
     *resultPtr = (float) internalTestFunction(4);
     return resultPtr;
}
double test3() {         /* UNEXPECTED RESULT on arm 32 bit: expected
5.0 but returns different value (Maybe the value of the integer
registers misinterpreted because the return is not in integer registers?) */
     return internalTestFunction(4);
}
double* test3Ptr() {         /* OK on all architectures, returns 5:
passing float/double in libtcc compiled code and return pointer to
double is working just fine */
   double *resultPtr = malloc(sizeof(double));
   *resultPtr = internalTestFunction(4);
     return resultPtr;
}
[2]

It compiles without any errors on all tested architectures (x86, x86_64,
aarch64 and arm (32bit)) and is working as intended on all platforms
except 32 bit arm. test1(), test2Ptr() and test3Ptr() is working just
fine on all of these platforms (even 32 bit arm) but test2() and test3()
doesn't return the intended result.

I tried both "arm" and "thumb" for ANDROID_ARM_MODE (according to
https://developer.android.com/ndk/guides/cmake#android_arm_mode).

I thought it might be related to the fact that "-mfloat-abi=softfp" is
used as flag under android, so that double and float values are passed
to functions in integer registers (according to
https://developer.android.com/ndk/guides/abis#v7a) because if I return
pointers to the double and float values the pointed to values are correct.
Unfortunately I lack the know-how to debug or even fix this.

best regards,
Alexander

[1] repository with CMake project to build libtcc:
https://github.com/Alex2804/libtcc-cmake
[2] The code is in a repository with an Android Studio project:
https://github.com/Alex2804/libtcc-cmake-android/blob/master/app/src/main/cpp/native-lib.cpp 


_______________________________________________
Tinycc-devel mailing list
[hidden email]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel