Project

General

Profile

inf and nan in IR

Added by Rochus Keller about 1 year ago

When I compile _PDCLIB_strtod_main.c (attached) I see two problems.

The first seems to be in the machine code generator. The expression "1.0 / 0.0" is correctly represented as "inf" in the IR (attached), but from the machine code generator I get the following error:

error: invalid operand in 'mov f8 [$fp-48], f8 inf'

The second seems to be in cdemitter Context::Divide. The expression "0.0 / 0.0" is aparently NAN according to the standard. But when Context::Divide sees an lhs of 0, it just returns it instead of checking rhs.

Can you please help, thanks.


Replies (5)

RE: inf and nan in IR - Added by Florian Negele about 1 year ago

Thanks, I will look into that. Technically speaking division by zero is undefined in C and in intermediate code, so this behaviour is actually valid. The code should rather use the INFINITY macro from the math.h header to properly represent infinity.

RE: inf and nan in IR - Added by Rochus Keller about 1 year ago

Meanwhile I also tried to process with cdamd32 while I replaced inf by nan which gives me the following error:

./ecsd _PDCLIB_strtod_main.cod _PDCLIB_strtod_main.cod:154: error: invalid operand in 'mov f8 [$fp-48], f8 nan'

so this behaviour is actually valid

As far as I understand the ECS manual, both INF and NAN are valid values, so I would expect that a mov operation of these values would work.

division by zero is undefined in C

In ISO 9899 it indeed is undefined; but the compilers I usually use seem to behave all the same: x/0.0 is inf and 0.0/0.0 is nan. So I would assume that chibicc/ecc should also behave like this.

The code should rather use the INFINITY macro from the math.h

What I currently try to do is actually implementing a standard library for chibicc/ecc, and thus defining what these macros are is actually part of the task.

RE: inf and nan in IR - Added by Florian Negele about 1 year ago

The attached patch should fix both issues, thanks for reporting.

inf.patch (952 Bytes) inf.patch

RE: inf and nan in IR - Added by Florian Negele about 1 year ago

Being undefined in C, C++, and intermediate code, your code should at least test against INFINITY and NAN and provide the expected outcome yourself rather than adding an implicit dependency on the compiler used to compile your code.

RE: inf and nan in IR - Added by Rochus Keller about 1 year ago

Yes, that's what I did in codegen.cpp (unfortunately not feasible in codegen.c), but by the end of the day INFINITY and NAN are still implementation dependent; but now everything compiles as expected (up to a few symbols I need from runtime.cpp), so thank you again very much for your support!

    (1-5/5)