Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Natural logarithm (log) seems broken #202

Closed
ggreif opened this issue Jun 10, 2020 · 4 comments
Closed

Natural logarithm (log) seems broken #202

ggreif opened this issue Jun 10, 2020 · 4 comments

Comments

@ggreif
Copy link
Contributor

ggreif commented Jun 10, 2020

printf("%f ---> %f\n", 7.4225, log(7.4225)); will output
7.422500 ---> inf,
whereas
printf("%f ---> %f\n", 7.4225, log10(7.4225)); gives
7.422500 ---> 0.870550, which looks like the correct answer.

printf("%f ---> %f\n", 7.4225, log2(7.4225)); gives
7.422500 ---> -nan, which looks wrong too.

I didn't try the *f and *l versions of these.

My compiler is clang-10 from nixpkgs, which is version 10.0.0.

ggreif added a commit to dfinity/motoko that referenced this issue Jun 10, 2020
@ggreif
Copy link
Contributor Author

ggreif commented Jun 10, 2020

Just for the record, I tried to reproduce this with https://webassembly.sh:

File uploaded successfully to /tmp
→ /tmp/log.c
$ cat /tmp/log.c
int main(void) { extern double log(double); printf("%f ---> %f\n", 7.4225, log(7.4225)); return 0; }
$ clang -cc1 -triple wasm32-unkown-wasi -isysroot /sys -internal-isystem /sys/include -emit-obj -o ./log.o /tmp/log.c
/tmp/log.c:1:45: warning: implicitly declaring library function 'printf' with type 'int (const char *, ...)'
int main(void) { extern double log(double); printf("%f ---> %f\n", 7.4225, log(7.4225)); return 0; }
                                            ^
/tmp/log.c:1:45: note: include the header <stdio.h> or explicitly provide a declaration for 'printf'
1 warning generated.
$ wasm-ld -L/sys/lib/wasm32-wasi /sys/lib/wasm32-wasi/crt1.o ./log.o -lc -o ./log.wasm
$ wasmer log.wasm
7.422500 ---> 2.004516

And there it produces the correct result.

@sunfishcode
Copy link
Member

I don't have a nix machine handy, but at a quick try with the latest clang and wasi-libc, the testcase produces the correct result for me. Would it be possible to upload the wasm file somwhere?

@ggreif
Copy link
Contributor Author

ggreif commented Jun 10, 2020

Compiled as

clang-10 --compile -fpic -fvisibility=hidden --std=c11 --target=wasm32-emscripten -fvisibility=hidden -fno-builtin -ffreestanding --optimize=3  -isystem /nix/store/n2zmrqyhm1avzwz9jf8kliz4ws29pph1-source/libc-top-half/musl/arch/wasm32 -isystem /nix/store/n2zmrqyhm1avzwz9jf8kliz4ws29pph1-source/libc-top-half/musl/src/include -isystem /nix/store/n2zmrqyhm1avzwz9jf8kliz4ws29pph1-source/libc-top-half/musl/src/internal -isystem /nix/store/mz8fajfb2sq5v9czy4rn70v7fz409l8n-musl-wasi-sysroot/include -I /nix/store/n2zmrqyhm1avzwz9jf8kliz4ws29pph1-source/libc-top-half/musl/../headers/private -D_ERRNO_H -DEOVERFLOW=75 -DEINVAL=22 -Derrno='(*({ static int bla = 0; &bla; }))' -DNL_ARGMAX=9 -D'TOKENPASTE0(x, y)=x ## y' -D'TOKENPASTE(x, y)=TOKENPASTE0(x, y)' -D'__fwritex=TOKENPASTE(__fwritex_,__COUNTER__)' -D'__fwritex_2(s, l, f)=(f->write((f), (s), (l)))' -D'__towrite=TOKENPASTE(__towrite_,__COUNTER__)' -D'__towrite_3(f)=(0)' -D__wasilibc_printscan_no_long_double -D__wasilibc_printscan_full_support_option='""' -D__wasi__ -D__NEED_va_list -D__NEED_off_t -Dsqrt=__builtin_sqrt -Dfabs=__builtin_fabs /nix/store/n2zmrqyhm1avzwz9jf8kliz4ws29pph1-source/libc-top-half/musl/src/math/log.c --output _build/wasm/musl_log.o

(Yes, I know about --target=wasm32-emscripten, that is needed in my setup for technical reasons (i.e. dynamic linking), but I'd be surprised if that turned out to be the problem).
musl_log.o.zip

@ggreif
Copy link
Contributor Author

ggreif commented Jun 10, 2020

Oops, I found my error. The file log_data.c was not linked into my library. Sorry for the noise!

@ggreif ggreif closed this as completed Jun 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants