File: gmp_num_log.c

package info (click to toggle)
gcl 2.6.14-21
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 60,864 kB
  • sloc: ansic: 177,407; lisp: 151,509; asm: 128,169; sh: 22,510; cpp: 11,923; tcl: 3,181; perl: 2,930; makefile: 2,360; sed: 334; yacc: 226; lex: 95; awk: 30; fortran: 24; csh: 23
file content (117 lines) | stat: -rw-r--r-- 2,830 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
	x : fixnum or bignum (may be not normalized)
	y : integer
   returns
	fixnum or bignum ( not normalized )
*/

object big_log_op();
object normalize_big(object);

static fixnum
fixnum_log_op2(fixnum op,fixnum x,fixnum y) {
  
  return fixnum_boole(op,x,y);

}

static object
integer_log_op2(fixnum op,object x,enum type tx,object y,enum type ty) {

  object u=big_fixnum1;
  object ux=tx==t_bignum ? x : (mpz_set_si(MP(big_fixnum2),fix(x)), big_fixnum2);
  object uy=ty==t_bignum ? y : (mpz_set_si(MP(big_fixnum3),fix(y)), big_fixnum3);
  
  switch(op) {
  case BOOLCLR:	 mpz_set_si(MP(u),0);break;
  case BOOLSET:	 mpz_set_si(MP(u),-1);break;
  case BOOL1:	 mpz_set(MP(u),MP(ux));break;
  case BOOL2:	 mpz_set(MP(u),MP(uy));break;
  case BOOLC1:	 mpz_com(MP(u),MP(ux));break;
  case BOOLC2:	 mpz_com(MP(u),MP(uy));break;
  case BOOLAND:	 mpz_and(MP(u),MP(ux),MP(uy));break;
  case BOOLIOR:	 mpz_ior(MP(u),MP(ux),MP(uy));break;
  case BOOLXOR:	 mpz_xor(MP(u),MP(ux),MP(uy));break;
  case BOOLEQV:	 mpz_xor(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break;
  case BOOLNAND: mpz_and(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break;
  case BOOLNOR:	 mpz_ior(MP(u),MP(ux),MP(uy));mpz_com(MP(u),MP(u));break;
  case BOOLANDC1:mpz_com(MP(u),MP(ux));mpz_and(MP(u),MP(u),MP(uy));break;
  case BOOLANDC2:mpz_com(MP(u),MP(uy));mpz_and(MP(u),MP(ux),MP(u));break;
  case BOOLORC1: mpz_com(MP(u),MP(ux));mpz_ior(MP(u),MP(u),MP(uy));break;
  case BOOLORC2: mpz_com(MP(u),MP(uy));mpz_ior(MP(u),MP(ux),MP(u));break;
  default:break;/*FIXME error*/
  }
    
  return u;

}

object
log_op2(fixnum op,object x,object y) {

  enum type tx=type_of(x),ty=type_of(y);

  if (tx==t_fixnum && ty==t_fixnum)
    return make_fixnum(fixnum_log_op2(op,fix(x),fix(y)));
  else
    return maybe_replace_big(integer_log_op2(op,x,tx,y,ty));
}

static object
log_op(fixnum op) {

  fixnum i,n=vs_top-vs_base,fx=0;
  enum type tx,ty;
  object x,y;
  
  if ((tx=type_of(x=vs_base[0]))==t_fixnum) {fx=fix(x);x=OBJNULL;}
  for (i=1;i<n;i++) {
    ty=type_of(y=vs_base[i]);
    if (tx==t_fixnum&&ty==t_fixnum)
      fx=fixnum_log_op2(op,fx,fix(y));
    else {
      x=normalize_big(integer_log_op2(op,x==OBJNULL ? make_fixnum(fx) : x,tx,y,ty));
      if ((tx=type_of(x))==t_fixnum) {fx=fix(x);x=OBJNULL;}
    }
  }

  return x==OBJNULL ? make_fixnum(fx) : maybe_replace_big(x);

}




static int
big_bitp(object x, ufixnum p)
{
  return mpz_tstbit(MP(x),p);
}

static int
mpz_bitcount(__mpz_struct *x)
{
  if (mpz_sgn(x) >= 0) {
    return mpz_popcount(x);
  } else {
    object u = new_bignum();
    mpz_com(MP(u),x);
    return mpz_popcount(MP(u));
  }
}


static int
mpz_bitlength(__mpz_struct *x)
{
  if (mpz_sgn(x) >= 0) {
    return mpz_sizeinbase(x,2);
  } else {
    object u = new_bignum();
    mpz_com(MP(u),x);
    return mpz_sizeinbase(MP(u),2);
  }
}