OSDN Git Service

- update comment to clarify that this file is specific to AIX and 32-bit mode.
[pf3gnuchains/gcc-fork.git] / libchill / flsetclrps.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 /*
34  * function __flsetclrpowerset
35  *
36  * parameters:
37  *      ps              powerset
38  *      bitlength       length of powerset
39  *
40  * returns:
41  *      int             -1 .. nothing found
42  *                      >= 0 .. index of last set bit
43  * exceptions:
44  *  none
45  *
46  * abstract:
47  *  Find last bit set in a powerset and return the corresponding value
48  *  in *out and clear this bit. Return 0 for no more found, else 1.
49  *
50  */
51 int
52 __flsetclrpowerset (ps, bitlength, first_bit)
53      SET_WORD      *ps;
54      unsigned long  bitlength;
55      int first_bit;
56 {
57   register int bitno;
58
59 #ifndef USE_CHARS
60   if (bitlength <= SET_CHAR_SIZE)
61     {
62       for (bitno = bitlength - 1; bitno >= first_bit; bitno--)
63         if (GET_BIT_IN_CHAR (*((SET_CHAR *)ps), bitno))
64           break;
65       return bitno < first_bit ? -1 : bitno;
66     }
67   else if (bitlength <= SET_SHORT_SIZE)
68     {
69       for (bitno = bitlength - 1; bitno >= first_bit; bitno--)
70         if (GET_BIT_IN_SHORT (*((SET_SHORT *)ps), bitno))
71           break;
72       return bitno < first_bit ? -1 : bitno;
73     }
74   else
75 #endif
76     {
77       SET_WORD *p, c;
78       bitno = bitlength - 1;
79       if (bitno < first_bit)
80         return -1;
81       p = &ps[(unsigned) bitno / SET_WORD_SIZE];
82       c = *p;
83       if (((unsigned) bitlength % SET_WORD_SIZE) != 0)
84         MASK_UNUSED_WORD_BITS(&c, (unsigned) bitlength % SET_WORD_SIZE);
85       if (c)
86         goto found;
87       else
88         bitno -= ((unsigned) bitno % SET_WORD_SIZE) + 1;
89       while (bitno >= first_bit)
90         {
91           c = *--p;
92           if (c)
93             goto found;
94           bitno -= SET_WORD_SIZE;
95         }
96       return -1;
97     found:
98       for (; bitno >= first_bit; bitno--)
99         {
100           if (GET_BIT_IN_WORD (c, (unsigned) bitno % SET_WORD_SIZE))
101             return bitno;
102         }
103       return -1;
104     }
105 }