qbitbits.c 1011 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10
#include "f2c.h"

#ifndef LONGBITS
#define LONGBITS 32
#endif

#ifndef LONG8BITS
#define LONG8BITS (2*LONGBITS)
#endif

Kaveh R. Ghazi committed
11 12
integer
qbit_bits (longint a, integer b, integer len)
13
{
Kaveh R. Ghazi committed
14
  /* Assume 2's complement arithmetic */
15

Kaveh R. Ghazi committed
16
  ulongint x, y;
17

Kaveh R. Ghazi committed
18 19 20 21 22 23
  x = (ulongint) a;
  y = (ulongint) - 1L;
  x >>= b;
  y <<= len;
  return (longint) (x & y);
}
24

Kaveh R. Ghazi committed
25 26
longint
qbit_cshift (longint a, integer b, integer len)
27
{
Kaveh R. Ghazi committed
28
  ulongint x, y, z;
29

Kaveh R. Ghazi committed
30 31 32 33 34 35 36 37 38 39 40 41 42
  x = (ulongint) a;
  if (len <= 0)
    {
      if (len == 0)
	return 0;
      goto full_len;
    }
  if (len >= LONG8BITS)
    {
    full_len:
      if (b >= 0)
	{
	  b %= LONG8BITS;
43
	  return (longint) (x << b | x >> (LONG8BITS - b));
44
	}
Kaveh R. Ghazi committed
45 46
      b = -b;
      b %= LONG8BITS;
47
      return (longint) (x << (LONG8BITS - b) | x >> b);
Kaveh R. Ghazi committed
48 49 50 51 52 53 54 55 56
    }
  y = z = (unsigned long) -1;
  y <<= len;
  z &= ~y;
  y &= x;
  x &= z;
  if (b >= 0)
    {
      b %= len;
57
      return (longint) (y | (z & (x << b | x >> (len - b))));
Kaveh R. Ghazi committed
58 59 60
    }
  b = -b;
  b %= len;
61
  return (longint) (y | (z & (x >> b | x << (len - b))));
Kaveh R. Ghazi committed
62
}