Home My Page Projects Modern C
Summary Activity Forums Tracker News SCM Files

Forum: discussion

Monitor Forum | Start New Thread Start New Thread
RE: Advice for floating point flexibility [ Reply ]
By: Jens Gustedt on 2020-08-03 06:55
[forum:153531]
Hello César,

> A crazy idea came to my mind: writing a C18 to C18 preprocessor that would parse
> the whole C18 syntax, detect arithmetic operators on FP types, even being able to
> perform arithmetic optimizations in the mean time, and finally outputting C18-valid
> code calling arithmetic functions from libraries when the types are not builtin.

> But this idea looks like quite a bit of work, as it's not a simple preprocessor, but a
> complete parser (I don't think such translation could be implemented without a full
> parser).

> Do you have any advice/idea that could help me?

glad you asked, yes!

I recently have written a whole source-to-source rewriting framework, shnell, that could help you with this. It doesn't have a complete C parser, only a lexer, but that might already help.

https://gustedt.gitlabpages.inria.fr/shnell/

There are even examples for expression rewriting that might be a start for what you want to do:

https://gustedt.gitlabpages.inria.fr/shnell/compute.html
https://gustedt.gitlabpages.inria.fr/shnell/bc.html

Don't hesitate to contact me by mail if you have further question or need more detailed instructions.

Jens

Advice for floating point flexibility [ Reply ]
By: Nobody on 2020-08-02 17:07
[forum:153530]
Hi!

First of all, thanks a lot for your book and your site, which are great resources for C programmers!

There's one topic in which I've been thinking for some months, but didn't find a good solution, and maybe you could give me some advice: I want that my (simulation) programs can operate not only with the standard floating point types (float, double), but also with other FP types implemented in software in 3rd party libraries (ie: 128bit IEEE floating point in software, for example).

Yes, I know in C++ you just overload the arithmetic operators and you are done, but, first, I don't find this as a reason to use C++ instead of C, because my programs fit nicely in C. And second, you are not "done", really, because C++ compilers cannot optimize arithmetic expressions on overloaded operators (ie: commutative, associative, etc, properties cannot be specified when you overload operators).

After thinking a lot, I didn't find any way for writing in C an arithmetic expression such as "a/(b-c)" instead of "divide(a,subtract(b,c))", where "a", "b", and "c" could be not only float or double, but a custom FP type implemented in software (or even a bigint if you prefer).

A crazy idea came to my mind: writing a C18 to C18 preprocessor that would parse the whole C18 syntax, detect arithmetic operators on FP types, even being able to perform arithmetic optimizations in the mean time, and finally outputting C18-valid code calling arithmetic functions from libraries when the types are not builtin.

But this idea looks like quite a bit of work, as it's not a simple preprocessor, but a complete parser (I don't think such translation could be implemented without a full parser).

Do you have any advice/idea that could help me?

Thanks a lot,

César