Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

Testing latest pari + WASM + node.js... and it works?! Wow.

28478 views
License: GPL3
ubuntu2004
1
/* Copyright (C) 2000 The PARI group.
2
3
This file is part of the PARI/GP package.
4
5
PARI/GP is free software; you can redistribute it and/or modify it under the
6
terms of the GNU General Public License as published by the Free Software
7
Foundation; either version 2 of the License, or (at your option) any later
8
version. It is distributed in the hope that it will be useful, but WITHOUT
9
ANY WARRANTY WHATSOEVER.
10
11
Check the License for details. You should have received a copy of it, along
12
with the package; see the file 'COPYING'. If not, write to the Free Software
13
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */
14
15
#include "pari.h"
16
#include "paripriv.h"
17
/*********************************************************************/
18
/** **/
19
/** BINARY DECOMPOSITION **/
20
/** **/
21
/*********************************************************************/
22
23
INLINE GEN
24
inegate(GEN z) { return subsi(-1,z); }
25
26
GEN
27
binary_zv(GEN x)
28
{
29
GEN xp, z;
30
long i, k, lx;
31
if (!signe(x)) return cgetg(1,t_VECSMALL);
32
xp = int_LSW(x);
33
lx = lgefint(x);
34
k = expi(x)+2;
35
z = cgetg(k, t_VECSMALL);
36
k--;
37
for(i = 2; i < lx; i++)
38
{
39
ulong u = *xp;
40
long j;
41
for (j=0; j<BITS_IN_LONG && k; j++) z[k--] = (u>>j)&1UL;
42
if (!k) break;
43
xp = int_nextW(xp);
44
}
45
return z;
46
}
47
static GEN
48
F2v_to_ZV_inplace(GEN v)
49
{
50
long i, l = lg(v);
51
v[0] = evaltyp(t_VEC) | _evallg(l);
52
for (i = 1; i < l; i++) gel(v,i) = v[i]? gen_1: gen_0;
53
return v;
54
}
55
/* "vector" of l bits (possibly no code word) to nonnegative t_INT */
56
GEN
57
bits_to_int(GEN x, long l)
58
{
59
long i, j, lz;
60
GEN z, zp;
61
62
if (!l) return gen_0;
63
lz = nbits2lg(l);
64
z = cgetg(lz, t_INT);
65
z[1] = evalsigne(1) | evallgefint(lz);
66
zp = int_LSW(z); *zp = 0;
67
for(i=l,j=0; i; i--,j++)
68
{
69
if (j==BITS_IN_LONG) { j=0; zp = int_nextW(zp); *zp = 0; }
70
if (x[i]) *zp |= 1UL<<j;
71
}
72
return int_normalize(z, 0);
73
}
74
/* "vector" of l < BITS_IN_LONG bits (possibly no code word) to nonnegative
75
* ulong */
76
ulong
77
bits_to_u(GEN v, long l)
78
{
79
ulong u = 0;
80
long i;
81
for (i = 1; i <= l; i++) u = (u <<1) | v[i];
82
return u;
83
}
84
85
/* set BITS_IN_LONG bits starting at word *w plus *r bits,
86
* clearing subsequent bits in the last word touched */
87
INLINE void
88
int_set_ulong(ulong a, GEN *w, long *r)
89
{
90
if (*r) {
91
**w |= (a << *r);
92
*w = int_nextW(*w);
93
**w = (a >> (BITS_IN_LONG - *r));
94
} else {
95
**w = a;
96
*w = int_nextW(*w);
97
}
98
}
99
100
/* set k bits starting at word *w plus *r bits,
101
* clearing subsequent bits in the last word touched */
102
INLINE void
103
int_set_bits(ulong a, long k, GEN *w, long *r)
104
{
105
if (*r) {
106
**w |= a << *r;
107
a >>= BITS_IN_LONG - *r;
108
} else {
109
**w = a;
110
a = 0;
111
}
112
*r += k;
113
if (*r >= BITS_IN_LONG) {
114
*w = int_nextW(*w);
115
*r -= BITS_IN_LONG;
116
for (; *r >= BITS_IN_LONG; *r -= BITS_IN_LONG) {
117
**w = a;
118
a = 0;
119
*w = int_nextW(*w);
120
}
121
if (*r)
122
**w = a;
123
}
124
}
125
126
/* set k bits from z (t_INT) starting at word *w plus *r bits,
127
* clearing subsequent bits in the last word touched */
128
INLINE void
129
int_set_int(GEN z, long k, GEN *w, long *r)
130
{
131
long l = lgefint(z) - 2;
132
GEN y;
133
if (!l) {
134
int_set_bits(0, k, w, r);
135
return;
136
}
137
y = int_LSW(z);
138
for (; l > 1; l--) {
139
int_set_ulong((ulong) *y, w, r);
140
y = int_nextW(y);
141
k -= BITS_IN_LONG;
142
}
143
if (k)
144
int_set_bits((ulong) *y, k, w, r);
145
}
146
147
GEN
148
nv_fromdigits_2k(GEN x, long k)
149
{
150
long l = lg(x) - 1, r;
151
GEN w, z;
152
if (k == 1) return bits_to_int(x, l);
153
if (!l) return gen_0;
154
z = cgetipos(nbits2lg(k * l));
155
w = int_LSW(z);
156
r = 0;
157
for (; l; l--)
158
int_set_bits(uel(x, l), k, &w, &r);
159
return int_normalize(z, 0);
160
}
161
162
GEN
163
fromdigits_2k(GEN x, long k)
164
{
165
long l, m;
166
GEN w, y, z;
167
for (l = lg(x) - 1; l && !signe(gel(x, 1)); x++, l--);
168
if (!l) return gen_0;
169
m = expi(gel(x, 1)) + 1;
170
z = cgetipos(nbits2lg(k * (l - 1) + m));
171
w = int_LSW(z);
172
if (!(k & (BITS_IN_LONG - 1)))
173
{
174
long i, j, t = k >> TWOPOTBITS_IN_LONG;
175
for (; l; l--)
176
{
177
j = lgefint(gel(x, l)) - 2;
178
y = int_LSW(gel(x, l));
179
for (i = 0; i < j; i++, y = int_nextW(y), w = int_nextW(w)) *w = *y;
180
if (l > 1) for (; i < t; i++, w = int_nextW(w)) *w = 0;
181
}
182
}
183
else
184
{
185
long r = 0;
186
for (; l > 1; l--) int_set_int(gel(x, l), k, &w, &r);
187
int_set_int(gel(x,1), m, &w, &r);
188
}
189
return int_normalize(z, 0);
190
}
191
192
GEN
193
binaire(GEN x)
194
{
195
ulong m,u;
196
long i,lx,ex,ly,tx=typ(x);
197
GEN y,p1,p2;
198
199
switch(tx)
200
{
201
case t_INT:
202
return F2v_to_ZV_inplace( binary_zv(x) );
203
case t_REAL:
204
ex = expo(x);
205
if (!signe(x)) return zerovec(maxss(-ex,0));
206
207
lx=lg(x); y=cgetg(3,t_VEC);
208
if (ex > bit_prec(x)) pari_err_PREC("binary");
209
p1 = cgetg(maxss(ex,0)+2,t_VEC);
210
p2 = cgetg(bit_prec(x)-ex,t_VEC);
211
gel(y,1) = p1;
212
gel(y,2) = p2;
213
ly = -ex; ex++; m = HIGHBIT;
214
if (ex<=0)
215
{
216
gel(p1,1) = gen_0; for (i=1; i <= -ex; i++) gel(p2,i) = gen_0;
217
i=2;
218
}
219
else
220
{
221
ly=1;
222
for (i=2; i<lx && ly<=ex; i++)
223
{
224
m=HIGHBIT; u=x[i];
225
do
226
{ gel(p1,ly) = (m & u) ? gen_1 : gen_0; ly++; }
227
while ((m>>=1) && ly<=ex);
228
}
229
ly=1;
230
if (m) i--; else m=HIGHBIT;
231
}
232
for (; i<lx; i++)
233
{
234
u=x[i];
235
do { gel(p2,ly) = m & u ? gen_1 : gen_0; ly++; } while (m>>=1);
236
m=HIGHBIT;
237
}
238
break;
239
240
case t_VEC: case t_COL: case t_MAT:
241
y = cgetg_copy(x, &lx);
242
for (i=1; i<lx; i++) gel(y,i) = binaire(gel(x,i));
243
break;
244
default: pari_err_TYPE("binary",x);
245
return NULL; /* LCOV_EXCL_LINE */
246
}
247
return y;
248
}
249
250
/* extract k bits (as ulong) starting at word *w plus *r bits */
251
INLINE ulong
252
int_get_bits(long k, GEN *w, long *r)
253
{
254
ulong mask = (1UL << k) - 1;
255
ulong a = (((ulong) **w) >> *r) & mask;
256
*r += k;
257
if (*r >= BITS_IN_LONG) {
258
*r -= BITS_IN_LONG;
259
*w = int_nextW(*w);
260
if (*r)
261
a |= ((ulong)**w << (k - *r)) & mask;
262
}
263
return a;
264
}
265
266
/* extract BITS_IN_LONG bits starting at word *w plus *r bits */
267
INLINE ulong
268
int_get_ulong(GEN *w, long *r)
269
{
270
ulong a = ((ulong) **w) >> *r;
271
*w = int_nextW(*w);
272
if (*r)
273
a |= ((ulong)**w << (BITS_IN_LONG - *r));
274
return a;
275
}
276
277
/* extract k bits (as t_INT) starting at word *w plus *r bits */
278
INLINE GEN
279
int_get_int(long k, GEN *w, long *r)
280
{
281
GEN z = cgetipos(nbits2lg(k));
282
GEN y = int_LSW(z);
283
for (; k >= BITS_IN_LONG; k -= BITS_IN_LONG) {
284
*y = int_get_ulong(w, r);
285
y = int_nextW(y);
286
}
287
if (k)
288
*y = int_get_bits(k, w, r);
289
return int_normalize(z, 0);
290
}
291
292
/* assume k < BITS_IN_LONG */
293
GEN
294
binary_2k_nv(GEN x, long k)
295
{
296
long l, n, r;
297
GEN v, w;
298
if (k == 1) return binary_zv(x);
299
if (!signe(x)) return cgetg(1, t_VECSMALL);
300
n = expi(x) + 1;
301
l = (n + k - 1) / k;
302
v = cgetg(l + 1, t_VECSMALL);
303
w = int_LSW(x);
304
r = 0;
305
for (; l > 1; l--) {
306
uel(v, l) = int_get_bits(k, &w, &r);
307
n -= k;
308
}
309
uel(v, 1) = int_get_bits(n, &w, &r);
310
return v;
311
}
312
313
GEN
314
binary_2k(GEN x, long k)
315
{
316
long l, n;
317
GEN v, w, y, z;
318
if (k == 1) return binaire(x);
319
if (!signe(x)) return cgetg(1, t_VEC);
320
n = expi(x) + 1;
321
l = (n + k - 1) / k;
322
v = cgetg(l + 1, t_VEC);
323
w = int_LSW(x);
324
if (!(k & (BITS_IN_LONG - 1))) {
325
long m, t = k >> TWOPOTBITS_IN_LONG, u = lgefint(x) - 2;
326
for (; l; l--) {
327
m = minss(t, u);
328
z = cgetipos(m + 2);
329
y = int_LSW(z);
330
for (; m; m--) {
331
*y = *w;
332
y = int_nextW(y);
333
w = int_nextW(w);
334
}
335
gel(v, l) = int_normalize(z, 0);
336
u -= t;
337
}
338
} else {
339
long r = 0;
340
for (; l > 1; l--, n -= k)
341
gel(v, l) = int_get_int(k, &w, &r);
342
gel(v, 1) = int_get_int(n, &w, &r);
343
}
344
return v;
345
}
346
347
/* return 1 if bit n of x is set, 0 otherwise */
348
long
349
bittest(GEN x, long n)
350
{
351
if (typ(x) != t_INT) pari_err_TYPE("bittest",x);
352
if (!signe(x) || n < 0) return 0;
353
if (signe(x) < 0)
354
{
355
pari_sp ltop=avma;
356
long b = !int_bit(inegate(x),n);
357
set_avma(ltop);
358
return b;
359
}
360
return int_bit(x, n);
361
}
362
363
GEN
364
gbittest(GEN x, long n) { return map_proto_lGL(bittest,x,n); }
365
366
/***********************************************************************/
367
/** **/
368
/** BITMAP OPS **/
369
/** x & y (and), x | y (or), x ^ y (xor), ~x (neg), x & ~y (negimply) **/
370
/** **/
371
/***********************************************************************/
372
/* Truncate a nonnegative integer to a number of bits. */
373
static GEN
374
ibittrunc(GEN x, long bits)
375
{
376
long lowbits, known_zero_words, xl = lgefint(x) - 2;
377
long len_out = nbits2nlong(bits);
378
379
if (xl < len_out)
380
return x;
381
/* Check whether mask is trivial */
382
lowbits = bits & (BITS_IN_LONG-1);
383
if (!lowbits) {
384
if (xl == len_out)
385
return x;
386
} else if (len_out <= xl) {
387
GEN xi = int_W(x, len_out-1);
388
/* Non-trival mask is given by a formula, if x is not
389
normalized, this works even in the exceptional case */
390
*xi &= (1L << lowbits) - 1;
391
if (*xi && xl == len_out) return x;
392
}
393
/* Normalize */
394
known_zero_words = xl - len_out;
395
if (known_zero_words < 0) known_zero_words = 0;
396
return int_normalize(x, known_zero_words);
397
}
398
399
GEN
400
gbitneg(GEN x, long bits)
401
{
402
const ulong uzero = 0;
403
long lowbits, xl, len_out, i;
404
405
if (typ(x) != t_INT) pari_err_TYPE("bitwise negation",x);
406
if (bits < -1)
407
pari_err_DOMAIN("bitwise negation","exponent","<",gen_m1,stoi(bits));
408
if (bits == -1) return inegate(x);
409
if (bits == 0) return gen_0;
410
if (signe(x) < 0) { /* Consider as if mod big power of 2 */
411
pari_sp ltop = avma;
412
return gerepileuptoint(ltop, ibittrunc(inegate(x), bits));
413
}
414
xl = lgefint(x);
415
len_out = nbits2lg(bits);
416
lowbits = bits & (BITS_IN_LONG-1);
417
if (len_out > xl) /* Need to grow */
418
{
419
GEN out, outp, xp = int_MSW(x);
420
out = cgetipos(len_out);
421
outp = int_MSW(out);
422
if (!lowbits)
423
*outp = ~uzero;
424
else
425
*outp = (1L << lowbits) - 1;
426
for (i = 3; i < len_out - xl + 2; i++)
427
{
428
outp = int_precW(outp); *outp = ~uzero;
429
}
430
for ( ; i < len_out; i++)
431
{
432
outp = int_precW(outp); *outp = ~*xp;
433
xp = int_precW(xp);
434
}
435
return out;
436
}
437
x = icopy(x);
438
for (i = 2; i < xl; i++) x[i] = ~x[i];
439
return ibittrunc(int_normalize(x,0), bits);
440
}
441
442
/* bitwise 'and' of two positive integers (any integers, but we ignore sign).
443
* Inputs are not necessary normalized. */
444
GEN
445
ibitand(GEN x, GEN y)
446
{
447
long lx, ly, lout;
448
long *xp, *yp, *outp;
449
GEN out;
450
long i;
451
452
if (!signe(x) || !signe(y)) return gen_0;
453
lx=lgefint(x); ly=lgefint(y);
454
lout = minss(lx,ly); /* > 2 */
455
xp = int_LSW(x);
456
yp = int_LSW(y);
457
out = cgetipos(lout);
458
outp = int_LSW(out);
459
for (i=2; i<lout; i++)
460
{
461
*outp = (*xp) & (*yp);
462
outp = int_nextW(outp);
463
xp = int_nextW(xp);
464
yp = int_nextW(yp);
465
}
466
if ( !*int_MSW(out) ) out = int_normalize(out, 1);
467
return out;
468
}
469
470
/* bitwise 'or' of absolute values of two integers */
471
GEN
472
ibitor(GEN x, GEN y)
473
{
474
long lx, ly;
475
long *xp, *yp, *outp;
476
GEN out;
477
long i;
478
if (!signe(x)) return absi(y);
479
if (!signe(y)) return absi(x);
480
481
lx = lgefint(x); xp = int_LSW(x);
482
ly = lgefint(y); yp = int_LSW(y);
483
if (lx < ly) swapspec(xp,yp,lx,ly);
484
/* lx > 2 */
485
out = cgetipos(lx);
486
outp = int_LSW(out);
487
for (i=2;i<ly;i++)
488
{
489
*outp = (*xp) | (*yp);
490
outp = int_nextW(outp);
491
xp = int_nextW(xp);
492
yp = int_nextW(yp);
493
}
494
for ( ;i<lx;i++)
495
{
496
*outp = *xp;
497
outp = int_nextW(outp);
498
xp = int_nextW(xp);
499
}
500
/* If input is normalized, this is not needed */
501
if ( !*int_MSW(out) ) out = int_normalize(out, 1);
502
return out;
503
}
504
505
/* bitwise 'xor' of absolute values of two integers */
506
GEN
507
ibitxor(GEN x, GEN y)
508
{
509
long lx, ly;
510
long *xp, *yp, *outp;
511
GEN out;
512
long i;
513
if (!signe(x)) return absi(y);
514
if (!signe(y)) return absi(x);
515
516
lx = lgefint(x); xp = int_LSW(x);
517
ly = lgefint(y); yp = int_LSW(y);
518
if (lx < ly) swapspec(xp,yp,lx,ly);
519
/* lx > 2 */
520
out = cgetipos(lx);
521
outp = int_LSW(out);
522
for (i=2;i<ly;i++)
523
{
524
*outp = (*xp) ^ (*yp);
525
outp = int_nextW(outp);
526
xp = int_nextW(xp);
527
yp = int_nextW(yp);
528
}
529
for ( ;i<lx;i++)
530
{
531
*outp = *xp;
532
outp = int_nextW(outp);
533
xp = int_nextW(xp);
534
}
535
if ( !*int_MSW(out) ) out = int_normalize(out, 1);
536
return out;
537
}
538
539
/* bitwise 'negimply' of absolute values of two integers */
540
/* "negimply(x,y)" is ~(x => y) == ~(~x | y) == x & ~y */
541
GEN
542
ibitnegimply(GEN x, GEN y)
543
{
544
long lx, ly, lin;
545
long *xp, *yp, *outp;
546
GEN out;
547
long i;
548
if (!signe(x)) return gen_0;
549
if (!signe(y)) return absi(x);
550
551
lx = lgefint(x); xp = int_LSW(x);
552
ly = lgefint(y); yp = int_LSW(y);
553
lin = minss(lx,ly);
554
out = cgetipos(lx);
555
outp = int_LSW(out);
556
for (i=2; i<lin; i++)
557
{
558
*outp = (*xp) & ~(*yp);
559
outp = int_nextW(outp);
560
xp = int_nextW(xp);
561
yp = int_nextW(yp);
562
}
563
for ( ;i<lx;i++)
564
{
565
*outp = *xp;
566
outp = int_nextW(outp);
567
xp = int_nextW(xp);
568
}
569
if ( !*int_MSW(out) ) out = int_normalize(out, 1);
570
return out;
571
}
572
573
static int
574
signs(GEN x, GEN y) { return (((signe(x) >= 0) << 1) | (signe(y) >= 0)); }
575
static void
576
checkint2(const char *f,GEN x, GEN y)
577
{ if (typ(x)!=t_INT || typ(y)!=t_INT) pari_err_TYPE2(f,x,y); }
578
579
GEN
580
gbitor(GEN x, GEN y)
581
{
582
pari_sp ltop = avma;
583
GEN z;
584
585
checkint2("bitwise or",x,y);
586
switch (signs(x, y))
587
{
588
case 3: /*1,1*/
589
return ibitor(x,y);
590
case 2: /*1,-1*/
591
z = ibitnegimply(inegate(y),x);
592
break;
593
case 1: /*-1,1*/
594
z = ibitnegimply(inegate(x),y);
595
break;
596
default: /*-1,-1*/
597
z = ibitand(inegate(x),inegate(y));
598
break;
599
}
600
return gerepileuptoint(ltop, inegate(z));
601
}
602
603
GEN
604
gbitand(GEN x, GEN y)
605
{
606
pari_sp ltop = avma;
607
GEN z;
608
609
checkint2("bitwise and",x,y);
610
switch (signs(x, y))
611
{
612
case 3: /*1,1*/
613
return ibitand(x,y);
614
case 2: /*1,-1*/
615
z = ibitnegimply(x,inegate(y));
616
break;
617
case 1: /*-1,1*/
618
z = ibitnegimply(y,inegate(x));
619
break;
620
default: /*-1,-1*/
621
z = inegate(ibitor(inegate(x),inegate(y)));
622
break;
623
}
624
return gerepileuptoint(ltop, z);
625
}
626
627
GEN
628
gbitxor(GEN x, GEN y)
629
{
630
pari_sp ltop = avma;
631
GEN z;
632
633
checkint2("bitwise xor",x,y);
634
switch (signs(x, y))
635
{
636
case 3: /*1,1*/
637
return ibitxor(x,y);
638
case 2: /*1,-1*/
639
z = inegate(ibitxor(x,inegate(y)));
640
break;
641
case 1: /*-1,1*/
642
z = inegate(ibitxor(inegate(x),y));
643
break;
644
default: /*-1,-1*/
645
z = ibitxor(inegate(x),inegate(y));
646
break;
647
}
648
return gerepileuptoint(ltop,z);
649
}
650
651
/* x & ~y */
652
GEN
653
gbitnegimply(GEN x, GEN y)
654
{
655
pari_sp ltop = avma;
656
GEN z;
657
658
checkint2("bitwise negated imply",x,y);
659
switch (signs(x, y))
660
{
661
case 3: /*1,1*/
662
return ibitnegimply(x,y);
663
case 2: /*1,-1*/
664
z = ibitand(x,inegate(y));
665
break;
666
case 1: /*-1,1*/
667
z = inegate(ibitor(y,inegate(x)));
668
break;
669
default: /*-1,-1*/
670
z = ibitnegimply(inegate(y),inegate(x));
671
break;
672
}
673
return gerepileuptoint(ltop,z);
674
}
675
676
long
677
hammingl(ulong w)
678
{
679
#if 0
680
return __builtin_popcountl(w);
681
#endif
682
static long byte_weight[] = {
683
0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,
684
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
685
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
686
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
687
1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,
688
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
689
2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,
690
3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8
691
};
692
long sum = 0;
693
while (w) { sum += byte_weight[w & 255]; w >>= 8; }
694
return sum;
695
}
696
697
/* number of nonzero entries among x[a], ..., x[b] */
698
static long
699
hamming_slice(GEN x, long a, long b)
700
{
701
long i, nb = 0;
702
for (i = a; i <= b; i++)
703
if (!gequal0(gel(x,i))) nb++;
704
return nb;
705
}
706
static long
707
hamming_mat(GEN x)
708
{
709
long i, lx = lg(x), nb = 0;
710
for (i = 1; i < lx; i++) nb += hammingweight(gel(x,i));
711
return nb;
712
}
713
static long
714
hamming_vecsmall(GEN x)
715
{
716
long i, lx = lg(x), nb = 0;
717
for (i = 1; i < lx; i++)
718
if (x[i]) nb++;
719
return nb;
720
}
721
static long
722
hamming_int(GEN n)
723
{
724
long lx = lgefint(n), i, sum;
725
if (lx == 2) return 0;
726
sum = hammingl(n[2]);
727
for (i = 3; i < lx; i++) sum += hammingl(n[i]);
728
return sum;
729
}
730
731
long
732
hammingweight(GEN n)
733
{
734
switch(typ(n))
735
{
736
case t_INT: return hamming_int(n);
737
case t_VEC:
738
case t_COL: return hamming_slice(n, 1, lg(n)-1);
739
case t_POL: return hamming_slice(n, 2, lg(n)-1);
740
case t_VECSMALL: return hamming_vecsmall(n);
741
case t_MAT: return hamming_mat(n);
742
}
743
pari_err_TYPE("hammingweight", n);
744
return 0;/*LCOV_EXCL_LINE*/
745
}
746
747