OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [libgfortran/] [runtime/] [select.c] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 jlechner
/* Implement the SELECT statement for character variables.
2
   Contributed by Andy Vaught
3
 
4
This file is part of the GNU Fortran 95 runtime library (libgfortran).
5
 
6
Libgfortran is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
In addition to the permissions in the GNU General Public License, the
12
Free Software Foundation gives you unlimited permission to link the
13
compiled version of this file into combinations with other programs,
14
and to distribute those combinations without any restriction coming
15
from the use of this file.  (The General Public License restrictions
16
do apply in other respects; for example, they cover modification of
17
the file, and distribution when not linked into a combine
18
executable.)
19
 
20
Libgfortran is distributed in the hope that it will be useful,
21
but WITHOUT ANY WARRANTY; without even the implied warranty of
22
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
GNU General Public License for more details.
24
 
25
You should have received a copy of the GNU General Public License
26
along with libgfortran; see the file COPYING.  If not, write to
27
the Free Software Foundation, 51 Franklin Street, Fifth Floor,
28
Boston, MA 02110-1301, USA.  */
29
 
30
#include "libgfortran.h"
31
 
32
typedef struct
33
{
34
  char *low;
35
  int low_len;
36
  char *high;
37
  int high_len;
38
  void *address;
39
}
40
select_struct;
41
 
42
extern void * select_string (select_struct *table, int table_len,
43
                             void *default_jump, const char *selector,
44
                             int selector_len);
45
export_proto(select_string);
46
 
47
 
48
/* select_string()-- Given a selector string and a table of
49
 * select_struct structures, return the address to jump to. */
50
 
51
void *
52
select_string (select_struct *table, int table_len, void *default_jump,
53
               const char *selector, int selector_len)
54
{
55
  select_struct *t;
56
  int i, low, high, mid;
57
 
58
  if (table_len == 0)
59
    return default_jump;
60
 
61
  /* Record the default address if present */
62
 
63
  if (table->low == NULL && table->high == NULL)
64
    {
65
      default_jump = table->address;
66
 
67
      table++;
68
      table_len--;
69
      if (table_len == 0)
70
        return default_jump;
71
    }
72
 
73
  /* Try the high and low bounds if present. */
74
 
75
  if (table->low == NULL)
76
    {
77
      if (compare_string (table->high_len, table->high,
78
                                selector_len, selector) >= 0)
79
        return table->address;
80
 
81
      table++;
82
      table_len--;
83
      if (table_len == 0)
84
        return default_jump;
85
    }
86
 
87
  t = table + table_len - 1;
88
 
89
  if (t->high == NULL)
90
    {
91
      if (compare_string (t->low_len, t->low,
92
                                selector_len, selector) <= 0)
93
        return t->address;
94
 
95
      table_len--;
96
      if (table_len == 0)
97
        return default_jump;
98
    }
99
 
100
  /* At this point, the only table entries are bounded entries.  Find
101
     the right entry with a binary chop. */
102
 
103
  low = -1;
104
  high = table_len;
105
 
106
  while (low + 1 < high)
107
    {
108
      mid = (low + high) / 2;
109
 
110
      t = table + mid;
111
      i = compare_string (t->low_len, t->low, selector_len, selector);
112
 
113
      if (i == 0)
114
        return t->address;
115
 
116
      if (i < 0)
117
        low = mid;
118
      else
119
        high = mid;
120
    }
121
 
122
  /* The string now lies between the low indeces of the now-adjacent
123
     high and low entries.  Because it is less than the low entry of
124
     'high', it can't be that one.  If low is still -1, then no
125
     entries match.  Otherwise, we have to check the high entry of
126
     'low'. */
127
 
128
  if (low == -1)
129
    return default_jump;
130
 
131
  t = table + low;
132
  if (compare_string (selector_len, selector,
133
                            t->high_len, t->high) <= 0)
134
    return t->address;
135
 
136
  return default_jump;
137
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.