Skip to content

Commit 65909a3

Browse files
[Fortran] Add floating point to integer conversion test (#220)
Semantics for floating point conversions to integer types was changed in flang to use the saturated floating point intrinsics to more closely match the standard semantics in llvm-project#130686 . This tests conversions from tiny, huge, nan, and inf to various integer types. Tests for 128b types are enabled if the fortran compiler supports it.
1 parent e15b90f commit 65909a3

File tree

7 files changed

+333
-0
lines changed

7 files changed

+333
-0
lines changed

Fortran/UnitTests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ add_subdirectory(assign-goto)
44
add_subdirectory(execute_command_line)
55
add_subdirectory(fcvs21_f95) # NIST Fortran Compiler Validation Suite
66
add_subdirectory(finalization)
7+
add_subdirectory(fp_conversions)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
include(CheckFortranSourceCompiles)
2+
3+
CHECK_FORTRAN_SOURCE_COMPILES("
4+
real(kind=16)::r
5+
integer(kind=16)::i
6+
end
7+
" FORTRAN_HAS_R16)
8+
9+
list(APPEND FFLAGS -funsigned)
10+
11+
if(FORTRAN_HAS_R16)
12+
message(STATUS "Fortran compiler supports real(kind=16)")
13+
set(Source fp_convert_r16.f90)
14+
llvm_singlesource()
15+
else()
16+
message(STATUS "Fortran compiler does not support real(kind=16)")
17+
endif()
18+
19+
set(Source fp_convert.f90)
20+
llvm_singlesource()
21+
file(COPY lit.local.cfg DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
module fp_convert_m
2+
implicit none
3+
type answer
4+
integer(kind=1) :: i8
5+
integer(kind=2) :: i16
6+
integer(kind=4) :: i32
7+
integer(kind=8) :: i64
8+
unsigned(kind=1) :: u8
9+
unsigned(kind=2) :: u16
10+
unsigned(kind=4) :: u32
11+
unsigned(kind=8) :: u64
12+
end type answer
13+
14+
contains
15+
subroutine print_answer(a)
16+
type(answer), intent(in) :: a
17+
print *, a%i8
18+
print *, a%i16
19+
print *, a%i32
20+
print *, a%i64
21+
print *, a%u8
22+
print *, a%u16
23+
print *, a%u32
24+
print *, a%u64
25+
end subroutine print_answer
26+
27+
function do_conversion(value) result(result)
28+
real(kind=8), intent(in) :: value
29+
type(answer) :: result
30+
result%i8 = int(value, kind=1)
31+
result%i16 = int(value, kind=2)
32+
result%i32 = int(value, kind=4)
33+
result%i64 = int(value, kind=8)
34+
35+
result%u8 = uint(value, kind=1)
36+
result%u16 = uint(value, kind=2)
37+
result%u32 = uint(value, kind=4)
38+
result%u64 = uint(value, kind=8)
39+
end function do_conversion
40+
41+
subroutine testcase(value)
42+
real(kind=8), intent(in) :: value
43+
type(answer) :: result
44+
result = do_conversion(value)
45+
call print_answer(result)
46+
end subroutine testcase
47+
end module fp_convert_m
48+
49+
program fp_convert
50+
use ieee_arithmetic, only: ieee_value, ieee_quiet_nan, ieee_positive_inf, ieee_negative_inf
51+
use fp_convert_m
52+
implicit none
53+
54+
real(kind=8) :: r64, nan, inf, ninf
55+
56+
nan = ieee_value(nan, ieee_quiet_nan)
57+
inf = ieee_value(inf, ieee_positive_inf)
58+
ninf = ieee_value(ninf, ieee_negative_inf)
59+
60+
print *, "huge"
61+
call testcase(huge(r64))
62+
63+
print *, "-huge"
64+
call testcase(-huge(r64))
65+
66+
print *, "tiny"
67+
call testcase(tiny(r64))
68+
69+
print *, "-tiny"
70+
call testcase(-tiny(r64))
71+
72+
print *, "inf"
73+
call testcase(inf)
74+
75+
print *, "-inf"
76+
call testcase(ninf)
77+
78+
print *, "nan"
79+
call testcase(nan)
80+
81+
end program fp_convert
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
huge
2+
127
3+
32767
4+
2147483647
5+
9223372036854775807
6+
255
7+
65535
8+
4294967295
9+
18446744073709551615
10+
-huge
11+
-128
12+
-32768
13+
-2147483648
14+
-9223372036854775808
15+
0
16+
0
17+
0
18+
0
19+
tiny
20+
0
21+
0
22+
0
23+
0
24+
0
25+
0
26+
0
27+
0
28+
-tiny
29+
0
30+
0
31+
0
32+
0
33+
0
34+
0
35+
0
36+
0
37+
inf
38+
127
39+
32767
40+
2147483647
41+
9223372036854775807
42+
255
43+
65535
44+
4294967295
45+
18446744073709551615
46+
-inf
47+
-128
48+
-32768
49+
-2147483648
50+
-9223372036854775808
51+
0
52+
0
53+
0
54+
0
55+
nan
56+
0
57+
0
58+
0
59+
0
60+
0
61+
0
62+
0
63+
0
64+
exit 0
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
module fp_convert_r16_m
2+
implicit none
3+
type answer
4+
integer(kind=1) :: i8
5+
integer(kind=2) :: i16
6+
integer(kind=4) :: i32
7+
integer(kind=8) :: i64
8+
integer(kind=16) :: i128
9+
unsigned(kind=1) :: u8
10+
unsigned(kind=2) :: u16
11+
unsigned(kind=4) :: u32
12+
unsigned(kind=8) :: u64
13+
unsigned(kind=16) :: u128
14+
end type answer
15+
contains
16+
17+
subroutine print_answer(a)
18+
type(answer), intent(in) :: a
19+
print *, a%i8
20+
print *, a%i16
21+
print *, a%i32
22+
print *, a%i64
23+
print *, a%i128
24+
print *, a%u8
25+
print *, a%u16
26+
print *, a%u32
27+
print *, a%u64
28+
print *, a%u128
29+
end subroutine print_answer
30+
31+
function do_conversion(value) result(result)
32+
real(kind=16), intent(in) :: value
33+
type(answer) :: result
34+
result%i8 = int(value, kind=1)
35+
result%i16 = int(value, kind=2)
36+
result%i32 = int(value, kind=4)
37+
result%i64 = int(value, kind=8)
38+
result%i128 = int(value, kind=16)
39+
result%u8 = uint(value, kind=1)
40+
result%u16 = uint(value, kind=2)
41+
result%u32 = uint(value, kind=4)
42+
result%u64 = uint(value, kind=8)
43+
result%u128 = uint(value, kind=16)
44+
end function do_conversion
45+
46+
subroutine testcase(value)
47+
real(kind=16), intent(in) :: value
48+
type(answer) :: result
49+
result = do_conversion(value)
50+
call print_answer(result)
51+
end subroutine
52+
end module fp_convert_r16_m
53+
54+
program fp_convert_r16
55+
use ieee_arithmetic, only: ieee_value, ieee_quiet_nan, ieee_positive_inf, ieee_negative_inf
56+
use fp_convert_r16_m
57+
implicit none
58+
59+
real(kind=16) :: r128, nan, inf, ninf
60+
61+
nan = ieee_value(nan, ieee_quiet_nan)
62+
inf = ieee_value(inf, ieee_positive_inf)
63+
ninf = ieee_value(ninf, ieee_negative_inf)
64+
65+
print *, "huge"
66+
call testcase(huge(r128))
67+
68+
print *, "-huge"
69+
call testcase(-huge(r128))
70+
71+
print *, "tiny"
72+
call testcase(tiny(r128))
73+
74+
print *, "-tiny"
75+
call testcase(-tiny(r128))
76+
77+
print *, "inf"
78+
call testcase(inf)
79+
80+
print *, "-inf"
81+
call testcase(ninf)
82+
83+
print *, "nan"
84+
call testcase(nan)
85+
86+
end program fp_convert_r16
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
huge
2+
127
3+
32767
4+
2147483647
5+
9223372036854775807
6+
170141183460469231731687303715884105727
7+
255
8+
65535
9+
4294967295
10+
18446744073709551615
11+
340282366920938463463374607431768211455
12+
-huge
13+
-128
14+
-32768
15+
-2147483648
16+
-9223372036854775808
17+
-170141183460469231731687303715884105728
18+
0
19+
0
20+
0
21+
0
22+
0
23+
tiny
24+
0
25+
0
26+
0
27+
0
28+
0
29+
0
30+
0
31+
0
32+
0
33+
0
34+
-tiny
35+
0
36+
0
37+
0
38+
0
39+
0
40+
0
41+
0
42+
0
43+
0
44+
0
45+
inf
46+
127
47+
32767
48+
2147483647
49+
9223372036854775807
50+
170141183460469231731687303715884105727
51+
255
52+
65535
53+
4294967295
54+
18446744073709551615
55+
340282366920938463463374607431768211455
56+
-inf
57+
-128
58+
-32768
59+
-2147483648
60+
-9223372036854775808
61+
-170141183460469231731687303715884105728
62+
0
63+
0
64+
0
65+
0
66+
0
67+
nan
68+
0
69+
0
70+
0
71+
0
72+
0
73+
0
74+
0
75+
0
76+
0
77+
0
78+
exit 0
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config.traditional_output = True
2+
config.single_source = True

0 commit comments

Comments
 (0)