Skip to content

[Fortran] Add floating point to integer conversion test #220

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Fortran/UnitTests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ add_subdirectory(assign-goto)
add_subdirectory(execute_command_line)
add_subdirectory(fcvs21_f95) # NIST Fortran Compiler Validation Suite
add_subdirectory(finalization)
add_subdirectory(fp_conversions)
21 changes: 21 additions & 0 deletions Fortran/UnitTests/fp_conversions/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
include(CheckFortranSourceCompiles)

CHECK_FORTRAN_SOURCE_COMPILES("
real(kind=16)::r
integer(kind=16)::i
end
" FORTRAN_HAS_R16)

list(APPEND FFLAGS -funsigned)

if(FORTRAN_HAS_R16)
message(STATUS "Fortran compiler supports real(kind=16)")
set(Source fp_convert_r16.f90)
llvm_singlesource()
else()
message(STATUS "Fortran compiler does not support real(kind=16)")
endif()

set(Source fp_convert.f90)
llvm_singlesource()
file(COPY lit.local.cfg DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
81 changes: 81 additions & 0 deletions Fortran/UnitTests/fp_conversions/fp_convert.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module fp_convert_m
implicit none
type answer
integer(kind=1) :: i8
integer(kind=2) :: i16
integer(kind=4) :: i32
integer(kind=8) :: i64
unsigned(kind=1) :: u8
unsigned(kind=2) :: u16
unsigned(kind=4) :: u32
unsigned(kind=8) :: u64
end type answer

contains
subroutine print_answer(a)
type(answer), intent(in) :: a
print *, a%i8
print *, a%i16
print *, a%i32
print *, a%i64
print *, a%u8
print *, a%u16
print *, a%u32
print *, a%u64
end subroutine print_answer

function do_conversion(value) result(result)
real(kind=8), intent(in) :: value
type(answer) :: result
result%i8 = int(value, kind=1)
result%i16 = int(value, kind=2)
result%i32 = int(value, kind=4)
result%i64 = int(value, kind=8)

result%u8 = uint(value, kind=1)
result%u16 = uint(value, kind=2)
result%u32 = uint(value, kind=4)
result%u64 = uint(value, kind=8)
end function do_conversion

subroutine testcase(value)
real(kind=8), intent(in) :: value
type(answer) :: result
result = do_conversion(value)
call print_answer(result)
end subroutine testcase
end module fp_convert_m

program fp_convert
use ieee_arithmetic, only: ieee_value, ieee_quiet_nan, ieee_positive_inf, ieee_negative_inf
use fp_convert_m
implicit none

real(kind=8) :: r64, nan, inf, ninf

nan = ieee_value(nan, ieee_quiet_nan)
inf = ieee_value(inf, ieee_positive_inf)
ninf = ieee_value(ninf, ieee_negative_inf)

print *, "huge"
call testcase(huge(r64))

print *, "-huge"
call testcase(-huge(r64))

print *, "tiny"
call testcase(tiny(r64))

print *, "-tiny"
call testcase(-tiny(r64))

print *, "inf"
call testcase(inf)

print *, "-inf"
call testcase(ninf)

print *, "nan"
call testcase(nan)

end program fp_convert
64 changes: 64 additions & 0 deletions Fortran/UnitTests/fp_conversions/fp_convert.reference_output
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
huge
127
32767
2147483647
9223372036854775807
255
65535
4294967295
18446744073709551615
-huge
-128
-32768
-2147483648
-9223372036854775808
0
0
0
0
tiny
0
0
0
0
0
0
0
0
-tiny
0
0
0
0
0
0
0
0
inf
127
32767
2147483647
9223372036854775807
255
65535
4294967295
18446744073709551615
-inf
-128
-32768
-2147483648
-9223372036854775808
0
0
0
0
nan
0
0
0
0
0
0
0
0
exit 0
86 changes: 86 additions & 0 deletions Fortran/UnitTests/fp_conversions/fp_convert_r16.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
module fp_convert_r16_m
implicit none
type answer
integer(kind=1) :: i8
integer(kind=2) :: i16
integer(kind=4) :: i32
integer(kind=8) :: i64
integer(kind=16) :: i128
unsigned(kind=1) :: u8
unsigned(kind=2) :: u16
unsigned(kind=4) :: u32
unsigned(kind=8) :: u64
unsigned(kind=16) :: u128
end type answer
contains

subroutine print_answer(a)
type(answer), intent(in) :: a
print *, a%i8
print *, a%i16
print *, a%i32
print *, a%i64
print *, a%i128
print *, a%u8
print *, a%u16
print *, a%u32
print *, a%u64
print *, a%u128
end subroutine print_answer

function do_conversion(value) result(result)
real(kind=16), intent(in) :: value
type(answer) :: result
result%i8 = int(value, kind=1)
result%i16 = int(value, kind=2)
result%i32 = int(value, kind=4)
result%i64 = int(value, kind=8)
result%i128 = int(value, kind=16)
result%u8 = uint(value, kind=1)
result%u16 = uint(value, kind=2)
result%u32 = uint(value, kind=4)
result%u64 = uint(value, kind=8)
result%u128 = uint(value, kind=16)
end function do_conversion

subroutine testcase(value)
real(kind=16), intent(in) :: value
type(answer) :: result
result = do_conversion(value)
call print_answer(result)
end subroutine
end module fp_convert_r16_m

program fp_convert_r16
use ieee_arithmetic, only: ieee_value, ieee_quiet_nan, ieee_positive_inf, ieee_negative_inf
use fp_convert_r16_m
implicit none

real(kind=16) :: r128, nan, inf, ninf

nan = ieee_value(nan, ieee_quiet_nan)
inf = ieee_value(inf, ieee_positive_inf)
ninf = ieee_value(ninf, ieee_negative_inf)

print *, "huge"
call testcase(huge(r128))

print *, "-huge"
call testcase(-huge(r128))

print *, "tiny"
call testcase(tiny(r128))

print *, "-tiny"
call testcase(-tiny(r128))

print *, "inf"
call testcase(inf)

print *, "-inf"
call testcase(ninf)

print *, "nan"
call testcase(nan)

end program fp_convert_r16
78 changes: 78 additions & 0 deletions Fortran/UnitTests/fp_conversions/fp_convert_r16.reference_output
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
huge
127
32767
2147483647
9223372036854775807
170141183460469231731687303715884105727
255
65535
4294967295
18446744073709551615
340282366920938463463374607431768211455
-huge
-128
-32768
-2147483648
-9223372036854775808
-170141183460469231731687303715884105728
0
0
0
0
0
tiny
0
0
0
0
0
0
0
0
0
0
-tiny
0
0
0
0
0
0
0
0
0
0
inf
127
32767
2147483647
9223372036854775807
170141183460469231731687303715884105727
255
65535
4294967295
18446744073709551615
340282366920938463463374607431768211455
-inf
-128
-32768
-2147483648
-9223372036854775808
-170141183460469231731687303715884105728
0
0
0
0
0
nan
0
0
0
0
0
0
0
0
0
0
exit 0
2 changes: 2 additions & 0 deletions Fortran/UnitTests/fp_conversions/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
config.traditional_output = True
config.single_source = True