Assume a C or C++ program wishes to send/receive logical(C_BOOL) from a fortran procedure. This is broken with NVHPC at least 24.3. The issue is when .not. doesn’t properly invert the value since NVHPC designates logical as odd and even per the reference manual instead of 1 and 0 like other compilers.
nvfortran reference guide:
The logical constants .TRUE. and .FALSE. are defined to be the four-byte values -1 and 0 respectively.
A logical expression is defined to be .TRUE. if its least significant bit is 1 and .FALSE. otherwise.
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
bool logical_not(bool);
int main(void) {
int c = 0;
bool b = true;
b = logical_not(b);
if(b){
fprintf(stderr, "not(true) != false\n");
c++;
}
b = logical_not(b);
if(!b){
fprintf(stderr, "not(false) != true\n");
c++;
}
if(c){
fprintf(stderr, "ERROR: C_BOOL\n");
return EXIT_FAILURE;
}
printf("OK: C_BOOL\n");
return EXIT_SUCCESS;
}
c_bool.f90
module dummy
use, intrinsic :: iso_c_binding, only: C_BOOL
implicit none
contains
logical(C_BOOL) function logical_not(L) bind(C)
logical(C_BOOL), intent(in), value :: L
logical_not = .not.L
end function logical_not
end module dummy
I believe what you’re seeing is a difference in the Fortran standard. We’re using the F2008 and earlier definition, while F2018 changed to using the 0/1 definition.
Along with the LLVM community’s “F18” project, we’re in the process of a complete reimplementation of the Fortran front-end to be fully F2018 compliant. While I don’t have a timeline, once stable, we’ll likely be replacing nvfortran with this new flang-based front-end.
Though in the meantime, we’ve only put a few F18 features into nvfortran, this not being one of them. Though I did add a report, TPR #35624, requesting engineering see if this is something we can add to nvfortran. If it’s easy, then they likely add it, otherwise, we’ll need to wait a bit until we make the switch to the flang-based front-end.
Ah, thanks for the reminder. I forgot about the -Munixlogial flag. This changes logicals to treat any nozero logical value as true so can be used as a work around for you.