OSDN Git Service

- update comment to clarify that this file is specific to AIX and 32-bit mode.
[pf3gnuchains/gcc-fork.git] / libchill / flsetps.c
1 /* Implement POWERSET runtime actions for CHILL.
2    Copyright (C) 1992,1993 Free Software Foundation, Inc.
3    Author: Wilfried Moser, et al
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* As a special exception, if you link this library with other files,
22    some of which are compiled with GCC, to produce an executable,
23    this library does not by itself cause the resulting executable
24    to be covered by the GNU General Public License.
25    This exception does not however invalidate any other reasons why
26    the executable file might be covered by the GNU General Public License.  */
27
28 #define __CHILL_LIB__
29
30 #include <stdio.h>
31 #include "powerset.h"
32
33 extern void __cause_ex1 (char *exname, char *file, int lineno);
34
35 /*
36  * function __flsetpowerset
37  *
38  * parameters:
39  *      ps              powerset
40  *      bitlength       length of powerset
41  *      minval          set low bound
42  *      filename        caller's file name
43  *      lineno          caller's line number
44  *
45  * returns:
46  *      int             largest enumeration value
47  * exceptions:
48  *      "empty"         if set is empty
49  *
50  * abstract:
51  *  Find last bit set in a powerset and return the corresponding value.
52  *
53  */
54 long
55 __flsetpowerset (ps, bitlength, minval, filename, lineno)
56      SET_WORD      *ps;
57      unsigned long  bitlength;
58      long           minval;
59      char          *filename;
60      int            lineno;
61 {
62   unsigned long bitno;
63
64   if (bitlength <= SET_CHAR_SIZE)
65     {
66       SET_CHAR cset = *((SET_CHAR *)ps);
67       if (cset != 0)
68         {
69           /* found a bit set .. calculate which */
70           for (bitno = SET_CHAR_SIZE; bitno >= 1; bitno--)
71             if (GET_BIT_IN_CHAR (cset, bitno - 1))
72               break;
73           /* return its index */
74           return bitno + minval - 1;
75         }
76     }
77   else if (bitlength <= SET_SHORT_SIZE)
78     {
79       SET_SHORT sset = *((SET_SHORT *)ps);
80       if (sset != 0)
81         {
82           /* found a bit set .. calculate which */
83           for (bitno = SET_SHORT_SIZE; bitno >= 1; bitno--)
84             if (GET_BIT_IN_SHORT (sset, bitno - 1))
85               break;
86           /* return its index */
87           return bitno + minval - 1;
88         }
89     }
90   else /* set composed of array of one or more WORDs */
91     {
92       SET_WORD  *endp = ps;
93       SET_WORD  *p = ps + BITS_TO_WORDS(bitlength) - 1;
94       unsigned long cnt;
95       
96       /* FIXME: bitorder problems? */
97       for (cnt = ((bitlength - 1) / SET_WORD_SIZE) * SET_WORD_SIZE;
98            p >= endp; p--, cnt -= SET_WORD_SIZE)
99         {
100           SET_WORD c = *p;
101           if (c)
102             {
103               /* found a bit set .. calculate which */
104               for (bitno = SET_WORD_SIZE; bitno >= 1; bitno--)
105                 if (GET_BIT_IN_WORD (c, bitno - 1))
106                   break;
107               return cnt + bitno + minval - 1;
108             }
109         }
110     }
111   /* no bits found - raise exception */
112   __cause_ex1 ("empty", filename, lineno);
113 }