Skip to content

Commit 2bc2d93

Browse files
[Fortran] Conditionally add real16 test
I replicated the floating point conversion test for real(kind=16). Instead of trying to keep both tests in the same source file, I broke it into two tests and only enabled one when the fortran compiler can compile a test program using real16.
1 parent 233bcb4 commit 2bc2d93

File tree

5 files changed

+262
-91
lines changed

5 files changed

+262
-91
lines changed
Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +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+
19
list(APPEND FFLAGS -funsigned)
210

3-
llvm_singlesource()
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()
418

19+
set(Source fp_convert.f90)
20+
llvm_singlesource()
521
file(COPY lit.local.cfg DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
Lines changed: 24 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module fp_convert_m
2-
use iso_fortran_env
32
implicit none
4-
integer :: test_case = 1
53
type answer
64
integer(kind=1) :: i8
75
integer(kind=2) :: i16
@@ -12,72 +10,23 @@ module fp_convert_m
1210
unsigned(kind=4) :: u32
1311
unsigned(kind=8) :: u64
1412
end type answer
15-
type(answer) :: answers(6)
16-
17-
interface operator(==)
18-
module procedure answer_eq
19-
end interface operator(==)
2013

2114
contains
22-
23-
subroutine init_answers()
24-
! huge
25-
answers(1) = answer( &
26-
127, 32767, 2147483647, 9223372036854775807_8, &
27-
unsigned(255, kind=1), &
28-
unsigned(65535, kind=2), &
29-
unsigned(4294967295, kind=4), &
30-
unsigned(18446744073709551615, kind=8))
31-
32-
! -huge
33-
answers(2) = answer( &
34-
-128, -32768, -2147483648, -9223372036854775808_8, &
35-
unsigned(0, kind=1), &
36-
unsigned(0, kind=2), &
37-
unsigned(0, kind=4), &
38-
unsigned(0, kind=8))
39-
40-
! tiny
41-
answers(3) = answer( &
42-
0, 0, 0, 0, &
43-
unsigned(0, kind=1), &
44-
unsigned(0, kind=2), &
45-
unsigned(0, kind=4), &
46-
unsigned(0, kind=8))
47-
48-
! -tiny
49-
answers(4) = answers(3)
50-
51-
! inf
52-
answers(5) = answer( &
53-
127, 32767, 2147483647, 9223372036854775807_8, &
54-
unsigned(255, kind=1), &
55-
unsigned(65535, kind=2), &
56-
unsigned(4294967295, kind=4), &
57-
unsigned(18446744073709551615, kind=8))
58-
59-
! -inf
60-
answers(6) = answer( &
61-
-128, -32768, -2147483648, -9223372036854775808_8, &
62-
unsigned(0, kind=1), &
63-
unsigned(0, kind=2), &
64-
unsigned(0, kind=4), &
65-
unsigned(0, kind=8))
66-
end subroutine init_answers
6715
subroutine print_answer(a)
6816
type(answer), intent(in) :: a
69-
print *, a%i8, a%i16, a%i32, a%i64, a%u8, a%u16, a%u32, a%u64
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
7025
end subroutine print_answer
7126

72-
logical function answer_eq(a, b)
73-
type(answer), intent(in) :: a, b
74-
answer_eq = a%i8 == b%i8 .and. a%i16 == b%i16 .and. a%i32 == b%i32 .and. a%i64 == b%i64 &
75-
.and. a%u8 == b%u8 .and. a%u16 == b%u16 .and. a%u32 == b%u32 .and. a%u64 == b%u64
76-
end function answer_eq
77-
78-
subroutine do_conversion(value, result)
27+
function do_conversion(value) result(result)
7928
real(kind=8), intent(in) :: value
80-
type(answer), intent(out) :: result
29+
type(answer) :: result
8130
result%i8 = int(value, kind=1)
8231
result%i16 = int(value, kind=2)
8332
result%i32 = int(value, kind=4)
@@ -87,24 +36,14 @@ subroutine do_conversion(value, result)
8736
result%u16 = uint(value, kind=2)
8837
result%u32 = uint(value, kind=4)
8938
result%u64 = uint(value, kind=8)
90-
end subroutine
39+
end function do_conversion
9140

92-
subroutine testcase(value, answers)
41+
subroutine testcase(value)
9342
real(kind=8), intent(in) :: value
94-
type(answer), intent(in) :: answers
9543
type(answer) :: result
96-
call do_conversion(value, result)
97-
if (result == answers) then
98-
print *, "PASS", test_case
99-
else
100-
print *, "FAIL", test_case
101-
print *, "Expected:"
102-
call print_answer(answers)
103-
print *, "Got:"
104-
call print_answer(result)
105-
end if
106-
test_case = test_case + 1
107-
end subroutine
44+
result = do_conversion(value)
45+
call print_answer(result)
46+
end subroutine testcase
10847
end module fp_convert_m
10948

11049
program fp_convert
@@ -114,28 +53,29 @@ program fp_convert
11453

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

117-
call init_answers()
118-
11956
nan = ieee_value(nan, ieee_quiet_nan)
12057
inf = ieee_value(inf, ieee_positive_inf)
12158
ninf = ieee_value(ninf, ieee_negative_inf)
12259

12360
print *, "huge"
124-
call testcase(huge(r64), answers(1))
61+
call testcase(huge(r64))
12562

12663
print *, "-huge"
127-
call testcase(-huge(r64), answers(2))
64+
call testcase(-huge(r64))
12865

12966
print *, "tiny"
130-
call testcase(tiny(r64), answers(3))
67+
call testcase(tiny(r64))
13168

13269
print *, "-tiny"
133-
call testcase(-tiny(r64), answers(4))
70+
call testcase(-tiny(r64))
13471

13572
print *, "inf"
136-
call testcase(inf, answers(5))
73+
call testcase(inf)
13774

13875
print *, "-inf"
139-
call testcase(ninf, answers(6))
76+
call testcase(ninf)
77+
78+
print *, "nan"
79+
call testcase(nan)
14080

14181
end program fp_convert
Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,64 @@
11
huge
2-
PASS 1
2+
127
3+
32767
4+
2147483647
5+
9223372036854775807
6+
255
7+
65535
8+
4294967295
9+
18446744073709551615
310
-huge
4-
PASS 2
11+
-128
12+
-32768
13+
-2147483648
14+
-9223372036854775808
15+
0
16+
0
17+
0
18+
0
519
tiny
6-
PASS 3
20+
0
21+
0
22+
0
23+
0
24+
0
25+
0
26+
0
27+
0
728
-tiny
8-
PASS 4
29+
0
30+
0
31+
0
32+
0
33+
0
34+
0
35+
0
36+
0
937
inf
10-
PASS 5
38+
127
39+
32767
40+
2147483647
41+
9223372036854775807
42+
255
43+
65535
44+
4294967295
45+
18446744073709551615
1146
-inf
12-
PASS 6
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
1364
exit 0
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
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_m
53+
54+
program fp_convert
55+
use ieee_arithmetic, only: ieee_value, ieee_quiet_nan, ieee_positive_inf, ieee_negative_inf
56+
use fp_convert_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

0 commit comments

Comments
 (0)