```Date: Tue, 14 Oct 2014 14:26:01 +0800
From: "bobodog" <8192542@...com>
To: "musl" <musl@...ts.openwall.com>
Subject: RE：[musl ] [math] I Found math library's bug in ceil, floor, round functions, Using arm toolchains

HI, ALL
I found basic math functions bug, include ceil ,floor, round ... when using arm toolchains.

Here is the patch file, just for ceil.c :

diff --git a/src/math/ceil.c b/src/math/ceil.c
index 22dc224..8634145 100644
--- a/src/math/ceil.c
+++ b/src/math/ceil.c
@@ -4,15 +4,16 @@ double ceil(double x)
{
union {double f; uint64_t i;} u = {x};
int e = u.i >> 52 & 0x7ff;
+	uint64_t n = 0x1p52;
double_t y;

if (e >= 0x3ff+52 || x == 0)
return x;
/* y = int(x) - x, where int(x) is an integer neighbor of x */
if (u.i >> 63)
-		y = (double)(x - 0x1p52) + 0x1p52 - x;
+		y = (double)(x - n) + n - x;
else
-		y = (double)(x + 0x1p52) - 0x1p52 - x;
+		y = (double)(x + n) - n - x;
/* special case because of non-nearest rounding modes */
if (e <= 0x3ff-1) {
FORCE_EVAL(y);‍

To solve this problem, must define a variate:  uint64_t n = 0x1p52;
using n to instead of 0x1p52, then all result are correctly.‍

The compile flags:
MCFLAGS		:= -mcpu=cortex-a8 -mtune=cortex-a8 -march=armv7-a -mfpu=neon -ftree-vectorize -ffast-math -mfloat-abi=softfp‍

I belive, when using this flags, the bug will happen. any one tell me how to solve it. we need optimizing flags to compile musl libc.‍

Thanks Rich Felker's help!

------------------
____________________________

------------------ 原始邮件 ------------------

1) enter the musl's source code
cd /home/jjj/test/musl/

git pull origin master

3) export some toolchains
export PATH=\$PATH:/home/jjj/9tripod/android_exynos4412_ics_rtm_v10/prebuilt/linux-x86/toolchain/arm-2009q3/bin/

4) configure musl to arm target
CC=arm-none-linux-gnueabi-gcc ./configure --target=arm --prefix=/home/jjj/test/musl/usr/local/musl/

5) compiler musl and install
make
make install

6) export REALGCC to arm linux toolchains
export REALGCC=arm-none-linux-gnueabi-gcc

7) write test cases and save to test.c
#include <stdio.h>
#include <math.h>

int main(int argc, char ** argv)
{
double n = 0.0;
int i;

for(i = 0; i < 20; i++)
{
n = n - 0.125;
printf("ceil(%f) = %f, floor(%f) = %f, round(%f) = %f\n", n, ceil(n), n, floor(n), n, round(n));
}

return 0;
}

8) compile test cases
./tools/musl-gcc test.c -o test -static

9) file test, will showing: test: ELF 32-bit LSB executable, ARM, version 1 (SYSV), statically linked, not stripped
file test

10) running test case using qemu-arm
qemu-arm test

11) result...
ceil(-0.125000) = -0.000000, floor(-0.125000) = -1.000000, round(-0.125000) = -0.000000
ceil(-0.250000) = -0.000000, floor(-0.250000) = -1.000000, round(-0.250000) = -0.000000
ceil(-0.375000) = -0.000000, floor(-0.375000) = -1.000000, round(-0.375000) = -0.000000
ceil(-0.500000) = -0.000000, floor(-0.500000) = -1.000000, round(-0.500000) = -1.000000
ceil(-0.625000) = -0.000000, floor(-0.625000) = -1.000000, round(-0.625000) = -1.000000
ceil(-0.750000) = -0.000000, floor(-0.750000) = -1.000000, round(-0.750000) = -1.000000
ceil(-0.875000) = -0.000000, floor(-0.875000) = -1.000000, round(-0.875000) = -1.000000
ceil(-1.000000) = -1.000000, floor(-1.000000) = -1.000000, round(-1.000000) = -1.000000
ceil(-1.125000) = -1.000000, floor(-1.125000) = -2.000000, round(-1.125000) = -1.000000
ceil(-1.250000) = -1.000000, floor(-1.250000) = -2.000000, round(-1.250000) = -1.000000
ceil(-1.375000) = -1.000000, floor(-1.375000) = -2.000000, round(-1.375000) = -1.000000
ceil(-1.500000) = -1.000000, floor(-1.500000) = -2.000000, round(-1.500000) = -2.000000
ceil(-1.625000) = -1.000000, floor(-1.625000) = -2.000000, round(-1.625000) = -2.000000
ceil(-1.750000) = -1.000000, floor(-1.750000) = -2.000000, round(-1.750000) = -2.000000
ceil(-1.875000) = -1.000000, floor(-1.875000) = -2.000000, round(-1.875000) = -2.000000
ceil(-2.000000) = -2.000000, floor(-2.000000) = -2.000000, round(-2.000000) = -2.000000
ceil(-2.125000) = -2.000000, floor(-2.125000) = -3.000000, round(-2.125000) = -2.000000
ceil(-2.250000) = -2.000000, floor(-2.250000) = -3.000000, round(-2.250000) = -2.000000
ceil(-2.375000) = -2.000000, floor(-2.375000) = -3.000000, round(-2.375000) = -2.000000
ceil(-2.500000) = -2.000000, floor(-2.500000) = -3.000000, round(-2.500000) = -3.000000

Using qemu-arm, the result is correct. we will test in real hardware and review result, and find bug somewhere
‍

------------------
____________________________

------------------ 原始邮件 ------------------

On Tue, Oct 14, 2014 at 10:51:58AM +0800, bobodog wrote:
> Test it in qemu-system-arm, with soft fp, realview-pb-a8 platform‍
>
> we porting musl to our project, and just write test functions to
> test cases, i don't kown, how to test musl in origin test cases.

Can you show a complete test program with correct prototypes for
printf, ceil, floor, and round that exhibits the problem for you?

Rich
Content of type "text/html" skipped
```

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.