llvm / llvm/llvm-project

[flang] c_long_double support

Open
#177,142 0 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

For some reason flang allows the use of `c_long_double` in some contexts, but not in others:

```fortran
program check_c_types
use, intrinsic :: iso_c_binding
implicit none

#if WITH_INTERFACE
interface
function strtold( str, endptr ) result(d) bind(C, name="strtold" )
!! :: long double strtold(const char *str, char **endptr)
import
character(kind=c_char,len=1),dimension(*),intent(in) :: str
type(c_ptr), intent(inout) :: endptr
real(c_long_double) :: d
end function strtold
end interface
#endif

print *, "--- Floating Point Type Properties ---"
print *

! Properties for C_LONG_DOUBLE
! Note: Some compilers/platforms may map this to the same as C_DOUBLE
print *, "Type: C_LONG_DOUBLE"
print *, " Precision (digits): ", precision(0.0_c_long_double)
print *, " Range (exponent): ", range(0.0_c_long_double)
print *, " Smallest Positive: ", tiny(0.0_c_long_double)
print *, " Largest Positive: ", huge(0.0_c_long_double)
print *, " Size in bytes: ", c_sizeof(0.0_c_long_double)
print *, " Kind value: ", c_long_double

end program check_c_types
```

```
$ flang check_c_types.F90 && ./a.out
--- Floating Point Type Properties ---

Type: C_LONG_DOUBLE
Precision (digits): 33
Range (exponent): 4931
Smallest Positive: 3.3621031431120935062626778173217526E-4932
Largest Positive: 1.189731495357231765085759326628007E+4932
Size in bytes: 16
Kind value: 16
```

```
$ flang -DWITH_INTERFACE check_c_types.F90 && ./a.out
error: Semantic errors in check_c_types.F90
./check_c_types.F90:12:13: error: REAL(KIND=16) is not an enabled type for this target
real(c_long_double) :: d
^^^^^^^^^^^^^^^^^^^^^^^
```

The issue surfaced when trying to compile json-fortran: https://github.com/jacobwilliams/json-fortran/issues/619

Contributor guide

Open the contributing guide

Research direction

Start with the provided check_c_types.F90 reproducer and run it both with and without -DWITH_INTERFACE using flang. Compare how c_long_double is accepted in the intrinsic operations versus the bind(C) interface declaration. Done means the same valid c_long_double usage no longer produces the reported enabled-type error, with the json-fortran case also compiling.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.