| 1 |
! { dg-do run } |
| 2 |
! { dg-additional-options "-mfp-rounding-mode=d" { target alpha*-*-* } } |
| 3 |
|
| 4 |
use, intrinsic :: ieee_features |
| 5 |
use, intrinsic :: ieee_arithmetic |
| 6 |
implicit none |
| 7 |
|
| 8 |
! k1 and k2 will be large real kinds, if supported, and single/double |
| 9 |
! otherwise |
| 10 |
integer, parameter :: k1 = & |
| 11 |
max(ieee_selected_real_kind(precision(0.d0) + 1), kind(0.)) |
| 12 |
integer, parameter :: k2 = & |
| 13 |
max(ieee_selected_real_kind(precision(0._k1) + 1), kind(0.d0)) |
| 14 |
|
| 15 |
interface check_equal |
| 16 |
procedure check_equal1, check_equal2 |
| 17 |
end interface |
| 18 |
|
| 19 |
interface check_not_equal |
| 20 |
procedure check_not_equal1, check_not_equal2 |
| 21 |
end interface |
| 22 |
|
| 23 |
interface divide |
| 24 |
procedure divide1, divide2 |
| 25 |
end interface |
| 26 |
|
| 27 |
real(kind=k1) :: x1, x2, x3 |
| 28 |
real(kind=k2) :: y1, y2, y3 |
| 29 |
type(ieee_round_type) :: mode |
| 30 |
|
| 31 |
if (ieee_support_rounding(ieee_up, x1) .and. & |
| 32 |
ieee_support_rounding(ieee_down, x1) .and. & |
| 33 |
ieee_support_rounding(ieee_nearest, x1) .and. & |
| 34 |
ieee_support_rounding(ieee_to_zero, x1)) then |
| 35 |
|
| 36 |
x1 = 1 |
| 37 |
x2 = 3 |
| 38 |
x1 = divide(x1, x2, ieee_up) |
| 39 |
|
| 40 |
x3 = 1 |
| 41 |
x2 = 3 |
| 42 |
x3 = divide(x3, x2, ieee_down) |
| 43 |
call check_not_equal(x1, x3) |
| 44 |
call check_equal(x3, nearest(x1, -1._k1)) |
| 45 |
call check_equal(x1, nearest(x3, 1._k1)) |
| 46 |
|
| 47 |
call check_equal(1._k1/3._k1, divide(1._k1, 3._k1, ieee_nearest)) |
| 48 |
call check_equal(-1._k1/3._k1, divide(-1._k1, 3._k1, ieee_nearest)) |
| 49 |
|
| 50 |
call check_equal(divide(3._k1, 7._k1, ieee_to_zero), & |
| 51 |
divide(3._k1, 7._k1, ieee_down)) |
| 52 |
call check_equal(divide(-3._k1, 7._k1, ieee_to_zero), & |
| 53 |
divide(-3._k1, 7._k1, ieee_up)) |
| 54 |
|
| 55 |
end if |
| 56 |
|
| 57 |
if (ieee_support_rounding(ieee_up, y1) .and. & |
| 58 |
ieee_support_rounding(ieee_down, y1) .and. & |
| 59 |
ieee_support_rounding(ieee_nearest, y1) .and. & |
| 60 |
ieee_support_rounding(ieee_to_zero, y1)) then |
| 61 |
|
| 62 |
y1 = 1 |
| 63 |
y2 = 3 |
| 64 |
y1 = divide(y1, y2, ieee_up) |
| 65 |
|
| 66 |
y3 = 1 |
| 67 |
y2 = 3 |
| 68 |
y3 = divide(y3, y2, ieee_down) |
| 69 |
call check_not_equal(y1, y3) |
| 70 |
call check_equal(y3, nearest(y1, -1._k2)) |
| 71 |
call check_equal(y1, nearest(y3, 1._k2)) |
| 72 |
|
| 73 |
call check_equal(1._k2/3._k2, divide(1._k2, 3._k2, ieee_nearest)) |
| 74 |
call check_equal(-1._k2/3._k2, divide(-1._k2, 3._k2, ieee_nearest)) |
| 75 |
|
| 76 |
call check_equal(divide(3._k2, 7._k2, ieee_to_zero), & |
| 77 |
divide(3._k2, 7._k2, ieee_down)) |
| 78 |
call check_equal(divide(-3._k2, 7._k2, ieee_to_zero), & |
| 79 |
divide(-3._k2, 7._k2, ieee_up)) |
| 80 |
|
| 81 |
end if |
| 82 |
|
| 83 |
contains |
| 84 |
|
| 85 |
real(kind=k1) function divide1 (x, y, rounding) result(res) |
| 86 |
use, intrinsic :: ieee_arithmetic |
| 87 |
real(kind=k1), intent(in) :: x, y |
| 88 |
type(ieee_round_type), intent(in) :: rounding |
| 89 |
type(ieee_round_type) :: old |
| 90 |
|
| 91 |
call ieee_get_rounding_mode (old) |
| 92 |
call ieee_set_rounding_mode (rounding) |
| 93 |
|
| 94 |
res = x / y |
| 95 |
|
| 96 |
call ieee_set_rounding_mode (old) |
| 97 |
end function |
| 98 |
|
| 99 |
real(kind=k2) function divide2 (x, y, rounding) result(res) |
| 100 |
use, intrinsic :: ieee_arithmetic |
| 101 |
real(kind=k2), intent(in) :: x, y |
| 102 |
type(ieee_round_type), intent(in) :: rounding |
| 103 |
type(ieee_round_type) :: old |
| 104 |
|
| 105 |
call ieee_get_rounding_mode (old) |
| 106 |
call ieee_set_rounding_mode (rounding) |
| 107 |
|
| 108 |
res = x / y |
| 109 |
|
| 110 |
call ieee_set_rounding_mode (old) |
| 111 |
end function |
| 112 |
|
| 113 |
subroutine check_equal1 (x, y) |
| 114 |
real(kind=k1), intent(in) :: x, y |
| 115 |
if (x /= y) then |
| 116 |
print *, x, y |
| 117 |
STOP 1 |
| 118 |
end if |
| 119 |
end subroutine |
| 120 |
|
| 121 |
subroutine check_equal2 (x, y) |
| 122 |
real(kind=k2), intent(in) :: x, y |
| 123 |
if (x /= y) then |
| 124 |
print *, x, y |
| 125 |
STOP 2 |
| 126 |
end if |
| 127 |
end subroutine |
| 128 |
|
| 129 |
subroutine check_not_equal1 (x, y) |
| 130 |
real(kind=k1), intent(in) :: x, y |
| 131 |
if (x == y) then |
| 132 |
print *, x, y |
| 133 |
STOP 3 |
| 134 |
end if |
| 135 |
end subroutine |
| 136 |
|
| 137 |
subroutine check_not_equal2 (x, y) |
| 138 |
real(kind=k2), intent(in) :: x, y |
| 139 |
if (x == y) then |
| 140 |
print *, x, y |
| 141 |
STOP 4 |
| 142 |
end if |
| 143 |
end subroutine |
| 144 |
|
| 145 |
end |