OSDN Git Service

* allgmem.c: Do not include config.h anymore.
[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 #define __CHILL_LIB__
22
23 #include <stdio.h>
24 #include "powerset.h"
25
26 /*
27  * function __flsetclrpowerset
28  *
29  * parameters:
30  *      ps              powerset
31  *      bitlength       length of powerset
32  *
33  * returns:
34  *      int             -1 .. nothing found
35  *                      >= 0 .. index of last set bit
36  * exceptions:
37  *  none
38  *
39  * abstract:
40  *  Find last bit set in a powerset and return the corresponding value
41  *  in *out and clear this bit. Return 0 for no more found, else 1.
42  *
43  */
44 int
45 __flsetclrpowerset (ps, bitlength, first_bit)
46      SET_WORD      *ps;
47      unsigned long  bitlength;
48      int first_bit;
49 {
50   register int bitno;
51
52 #ifndef USE_CHARS
53   if (bitlength <= SET_CHAR_SIZE)
54     {
55       for (bitno = bitlength - 1; bitno >= first_bit; bitno--)
56         if (GET_BIT_IN_CHAR (*((SET_CHAR *)ps), bitno))
57           break;
58       return bitno < first_bit ? -1 : bitno;
59     }
60   else if (bitlength <= SET_SHORT_SIZE)
61     {
62       for (bitno = bitlength - 1; bitno >= first_bit; bitno--)
63         if (GET_BIT_IN_SHORT (*((SET_SHORT *)ps), bitno))
64           break;
65       return bitno < first_bit ? -1 : bitno;
66     }
67   else
68 #endif
69     {
70       SET_WORD *p, c;
71       bitno = bitlength - 1;
72       if (bitno < first_bit)
73         return -1;
74       p = &ps[(unsigned) bitno / SET_WORD_SIZE];
75       c = *p;
76       if (((unsigned) bitlength % SET_WORD_SIZE) != 0)
77         MASK_UNUSED_WORD_BITS(&c, (unsigned) bitlength % SET_WORD_SIZE);
78       if (c)
79         goto found;
80       else
81         bitno -= ((unsigned) bitno % SET_WORD_SIZE) + 1;
82       while (bitno >= first_bit)
83         {
84           c = *--p;
85           if (c)
86             goto found;
87           bitno -= SET_WORD_SIZE;
88         }
89       return -1;
90     found:
91       for (; bitno >= first_bit; bitno--)
92         {
93           if (GET_BIT_IN_WORD (c, (unsigned) bitno % SET_WORD_SIZE))
94             return bitno;
95         }
96       return -1;
97     }
98 }