I have always found that the type kind system was imperfect, as the selected_xxx_kind()
functions do not say anything about the performances of the returned kinds: it can be a hardware native one (i.e. with existing CPU instructions to load/store them and perform arithmetic on them) or a software emulated one, and even if hardware native it can suffer from performances hits because of poor alignment.
I admit that this is a false problem in practice, at a time where virtually all machines implement the IEEE types. But I have used in the past IBM machines where 2 32bits and 2 64bits floating-points formats were available in the XLF compiler: the IEEE ones, and the legacy IBM ones. As far as I can remember, the performances with the IBM ones were lower, and it was present mainly for compatibility purposes (old codes relying on this particular format, and legacy files written with this format). With selected_real_kinds()
there would be no control at all on the returned kind…
The ISO_FORTRAN_ENV
does really solve this problem. Or maybe it partly solves it, but also introducing other problems. One can expect that ‘REAL64’ is hardware native (still without any guarantee), but now it relies on the assumption that there exists a 64 bits kind. Again, false problem in practice at times where all machines follow the IEEE conventions. Still, not fully satisfactory as Fortran is supposed to be hardware-agnostic. I’m a bit worried by all these codes with REAL32
, REAL64
… in case a machine appears that is no longer based on IEEE and even not on 8bit bytes in some future.
And then I discovered only today that C had types like:
int32_t
: occupies exactly 32 bitsintleast32_t
: the smallest type that occupies at least 32 bits; this one is close to the mechanism ofselected_int_kind()
(but the latter is better, as the criteria is a numerical one, not memory-size based)intfast32_t
: the fastest type that occupies at least 32 bits
There’s apparently not the equivalent for floating-point types, though…
The ISO_FORTRAN_ENV
module may provide similar kinds constants, even for the REAL
type : REAL32
, REALLEAST32
, REALFAST32
…
And/or, an additional specifier may appear in selected_xxx_kind
, e.g. selected_real_kind(p=15,priority="performance")
would return the fastest kind with at least 15 digits precision (priority
could be "size" | "balanced" | "performance"
)