OSDN Git Service

2004-07-23 Matthias Klose <doko@debian.org>
[pf3gnuchains/gcc-fork.git] / libgfortran / intrinsics / selected_kind.f90
1 !   Copyright 2003 Free Software Foundation, Inc.
2 !   Contributed by Kejia Zhao <kejia_zh@yahoo.com.cn>
3 !
4 !This file is part of the GNU Fortran 95 runtime library (libgfor).
5 !
6 !GNU libgfor is free software; you can redistribute it and/or
7 !modify it under the terms of the GNU Lesser General Public
8 !License as published by the Free Software Foundation; either
9 !version 2.1 of the License, or (at your option) any later version.
10 !
11 !GNU libgfor is distributed in the hope that it will be useful,
12 !but WITHOUT ANY WARRANTY; without even the implied warranty of
13 !MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 !GNU Lesser General Public License for more details.
15 !
16 !You should have received a copy of the GNU Lesser General Public
17 !License along with libgfor; see the file COPYING.  If not,
18 !write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 !Boston, MA 02111-1307, USA.
20 !
21
22 function selected_int_kind (r)
23   implicit none
24   integer, intent (in) :: r
25   integer :: selected_int_kind
26   integer :: i
27   ! Integer kind_range table
28   integer, parameter :: c = 4
29   type :: int_info
30     integer :: kind
31     integer :: range
32   end type int_info
33   type (int_info), parameter :: int_infos (c) = &
34       (/int_info (1, range (0_1)), &
35         int_info (2, range (0_2)), &
36         int_info (4, range (0_4)), &
37         int_info (8, range (0_8))/)
38
39   do i = 1, c
40     if (r <= int_infos (i) % range) then
41       selected_int_kind = int_infos (i) % kind
42       return
43     end if
44   end do
45   selected_int_kind = -1
46   return
47 end function
48
49 function selected_real_kind (p, r)
50   implicit none
51   integer, optional, intent (in) :: p, r
52   integer :: selected_real_kind
53   integer :: i, p2, r2
54   logical :: found_p, found_r
55   ! Real kind_precision_range table
56   integer, parameter :: c = 2
57   type :: real_info
58     integer :: kind
59     integer :: precision
60     integer :: range
61   end type real_info
62   type (real_info) :: real_infos (c) = &
63       (/real_info (4, precision (0.0_4), range (0.0_4)), &
64         real_info (8, precision (0.0_8), range (0.0_8))/)
65
66   selected_real_kind = 0
67   p2 = 0
68   r2 = 0
69   found_p = .false.
70   found_r = .false.
71
72   if (present (p)) p2 = p
73   if (present (r)) r2 = r
74
75   ! Assumes each type has a greater precision and range than previous one.
76
77   do i = 1, c
78     if (p2 <= real_infos (i) % precision) found_p = .true.
79     if (r2 <= real_infos (i) % range) found_r = .true.
80     if (found_p .and. found_r) then
81       selected_real_kind = real_infos (i) % kind
82       return
83     end if
84   end do
85
86   if (.not. (found_p)) selected_real_kind = selected_real_kind - 1
87   if (.not. (found_r)) selected_real_kind = selected_real_kind - 2
88
89   return
90 end function