3

I know there is logical_kinds in iso_fortran_env, but there seems to be no equivalent of e.g. int32 or int64 for logicals and, as far as I know, there is no guarantee the kind parameters for logicals and integers are the same (Cf. the -kind=unique flag for the NAG compiler). So, if I have something like:

integer(kind=int64) :: my_int

Is there some way to find out what is the appropriate kind (if any) for a logical variable that has the same size as my_int? In particular, something that would work in a parameter definition.

Jellby
  • 2,360
  • 3
  • 27
  • 56
  • Unless `int64` is the default kind for integer, why do you expect any such logical type to exist of that size? You could enumerate over all logical kinds to test storage size, but there's no guarantee there would be a match. What do you want to happen when there is not corresponding type? – francescalus Jan 27 '21 at 18:00
  • You can see more about what "portably" you can say about logical kinds in [this other question](https://stackoverflow.com/q/22125504/3157076), and if you want to look at automating enumeration to look at properties, we have [another question](https://stackoverflow.com/q/25534202/3157076) you can adapt. – francescalus Jan 27 '21 at 18:07
  • @francescalus I'm dealing with legacy code that assumes logicals and integers have the same size and does some nasty low-level stuff (binary dumps, raw memory copying, etc.). The code uses `real*8` and is compiled with `-i8`, and I want to convert it to using proper kinds. – Jellby Jan 27 '21 at 18:07
  • 2
    "I'm dealing with legacy code that assumes logicals and integers have the same size" For default kind integer and logical this is fine and is guaranteed, but I'm afraid the moment you throw -i8 at it all bets are off. You will have to look at your compiler documentation in that case – Ian Bush Jan 27 '21 at 18:10
  • @IanBush I know. The code assumes it will work, and apparently it has worked so far with the main compilers at least. But that's why I want to update and not rely on `-i8` or other out-of-spec extensions. – Jellby Jan 27 '21 at 18:23
  • It is not just assumption, it MUST work. The standard does require that default integer and default logical have the same storage size. – Vladimir F Героям слава Jan 27 '21 at 18:33
  • @VladimirF but not with `-i8`, which is how this code is supposed (expected) to be compiled. – Jellby Jan 27 '21 at 20:01
  • 2
    The problem is the moment you start using the -i8 flag nothing is portable any more - it is totally up to the compiler precisely what -i8 means and all guarantees given by the standard go out the window. I personally think that flags that change the kinds of variable types are just plain evil and wish compilers didn't support them - coding it correctly will save you much grief and pain in the long run – Ian Bush Jan 27 '21 at 20:06
  • @Jellby If you specifically only change the default real kind and not other types, you are in your own. You are breaking what the standard promises. If that breaks something, it is your fault. – Vladimir F Героям слава Jan 27 '21 at 20:14
  • 1
    Well, that's what I want to fix. I know it works with `-i8` and I want to make it work similarly with `kind=...`, because I can't rely on `-i8`. – Jellby Jan 27 '21 at 20:21
  • OK, now I get what you are after. In that case you can just use `LOGICAL(kind=your_integer_kind)` and make some assert that will stop everything if the storage size does not match. It should. – Vladimir F Героям слава Jan 27 '21 at 20:36

0 Answers0