We need a long term sustainable solution for the intrinsic (and non-intrinsic) runtime math library in LFortran
. If we design things well, it could be used by Flang
(@pmk) and other compilers also.
In stdlib
, we plan to implement special functions that are not intrinsic. As an example, bessel functions of integer kind are intrinsic, but half integer kind are not. The quality of the implementation however should be the same and I wonder if we should simply consider both intrinsic and non-instrinsic functions under the same umbrella, the only difference that intrinsic would be used by the compiler itself, and non-intrinsic would be called via stdlib
.
Compilers typically have at least two implementations: accurate but slower (no -ffast-math
), and slightly less accurate but faster (-ffast-math
). Users can choose.
I don’t know how to design such a switch for non-intrinsic functions in stdlib
.
I have brainstormed this a bit more here: Runtime library math functions (#244) · Issues · lfortran / lfortran · GitLab.
In particular, in stdlib
we would have a good Fortran API. So it would make sense to me to implement intrinsic functions (sin
, cos
, …) in Fortran also at the API level using a similar design. There can be several implementations, one calling into openlibm using iso_c_binding
, another into libpgmath, another into the standard C library, and another can be a pure Fortran implementation.
If a compiler can then use such a Fortran high level API, for example you point it to such an implementation as a Fortran module, then users could easily switch and provide their own preferred implementation. The compiler optimizations could optimize such a “wrapper” away so that there is no additional overhead over calling into libpgmath
directly (for example). Furthermore, for non-instrinsic functions they would simply call them from stdlib
, but the interface and internal implementation would be almost identical.
To rephrase it: libm
is an interface to a math library at the C level. Is it technically possible to provide an interface at the Fortran level? The Fortran API for all instrinsic (math) functions is described in the Table 16.1 in the Fortran 2018 standard. Can a compiler simply compile+link a standalone Fortran implementation, or must such functions be hardwired in the compiler at a lower level for performance (or other) reasons?
There are other instrinsic functions that are not math related, and each compiler must implement those as well, but that is a different issue.
CCing @milancurcic for opinion regarding stdlib
and @pmk for Flang
.