1/* Implement POWERSET runtime actions for CHILL.
2   Copyright (C) 1992,1993 Free Software Foundation, Inc.
3   Author: Wilfried Moser, et al
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING.  If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA.  */
21
22/* As a special exception, if you link this library with other files,
23   some of which are compiled with GCC, to produce an executable,
24   this library does not by itself cause the resulting executable
25   to be covered by the GNU General Public License.
26   This exception does not however invalidate any other reasons why
27   the executable file might be covered by the GNU General Public License.  */
28
29#define __CHILL_LIB__
30
31#include <stdio.h>
32#include "powerset.h"
33
34/*
35 * function __ffsetclrpowerset
36 *
37 * parameters:
38 *	ps		powerset
39 *	bitlength	length of powerset
40 *
41 * returns:
42 *	int		-1 .. nothing found
43 *			>=0 .. index of first true bit found
44 * exceptions:
45 *  none
46 */
47
48int
49__ffsetclrpowerset (ps, bitlength, first_bit)
50     SET_WORD      *ps;
51     unsigned long  bitlength;
52     int first_bit;
53{
54  register int bitno;
55
56  if (first_bit >= bitlength)
57    return -1;
58
59#ifndef USE_CHARS
60  if (bitlength <= SET_CHAR_SIZE)
61    {
62      for (bitno = first_bit; bitno < bitlength; bitno++)
63	if (GET_BIT_IN_CHAR (*((SET_CHAR *)ps), bitno))
64	  break;
65      return bitno == bitlength ? -1 : bitno;
66    }
67  else if (bitlength <= SET_SHORT_SIZE)
68    {
69      for (bitno = first_bit; bitno < bitlength; bitno++)
70	if (GET_BIT_IN_SHORT (*((SET_SHORT *)ps), bitno))
71	  break;
72      return bitno == bitlength ? -1 : bitno;
73    }
74  else
75#endif
76    {
77      unsigned int words_to_skip = (unsigned) first_bit / SET_WORD_SIZE;
78      unsigned long cnt = words_to_skip * SET_WORD_SIZE;
79      SET_WORD	*p = ps + words_to_skip;
80      SET_WORD	*endp = ps + BITS_TO_WORDS(bitlength);
81      SET_WORD	c;
82      first_bit = (unsigned) first_bit % (unsigned) SET_WORD_SIZE;
83
84      c = *p++;
85      if (c)
86	{
87	  for (bitno = first_bit; bitno < SET_WORD_SIZE; bitno++)
88	    if (GET_BIT_IN_WORD(c, bitno))
89	      goto found;
90	}
91      cnt += SET_WORD_SIZE;
92
93      while (p < endp)
94	{
95	  if ((c = *p++))
96	    {
97	      /* found a bit set .. calculate which */
98	      for (bitno = 0; bitno < SET_WORD_SIZE; bitno++)
99		if (GET_BIT_IN_WORD(c, bitno))
100		  goto found;
101	    }
102	  cnt += SET_WORD_SIZE;
103	}
104      return -1;
105    found:
106      bitno += cnt;
107      return bitno >= bitlength ? -1 : bitno;
108    }
109}
110