forked from gap-system/gap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobjfgelm.c
3373 lines (2919 loc) · 119 KB
/
objfgelm.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
**
*W objfgelm.c GAP source Frank Celler
**
*
*Y Copyright (C) 1996, Lehrstuhl D für Mathematik, RWTH Aachen, Germany
*Y (C) 1998 School Math and Comp. Sci., University of St Andrews, Scotland
*Y Copyright (C) 2002 The GAP Group
**
** This file contains the C functions for free group elements. There are
** basically three different (internal) types: 8 bits, 16 bits, and 32 bits
** for the generator/exponent pairs. The number of bits used for the
** generator numbers is determined by the number of free group generators in
** the family. Any object of this type looks like:
**
** +------+-----+-------+-------+-----+-----------+
** | TYPE | len | g1/e1 | g2/e2 | ... | glen/elen |
** +------+-----+-------+-------+-----+-----------+
**
** where <len> is a GAP integer object and <gi>/<ei> occupies 8, 16, or 32
** bits depending on the type. A TYPE of such an objects looks like:
**
** +--------+-------+--------+----------+-------+------+------+
** | FAMILY | FLAGS | SHARED | PURETYPE | EBITS | RANK | BITS |
** +--------+-------+--------+----------+-------+------+------+
**
** <PURETYPE> is the type of a result, <EBITS> is the number of bits used
** for the exponent, <RANK> is number of free group generators. But instead
** of accessing these entries directly you should use the following macros.
**
**
** The file "objects.h" defines the following macros:
**
** NEW_WORD( <result>, <type>, <npairs> )
** creates a new objects of type <type> with room for <npairs>
** generator/exponent pairs.
**
** RESIZE_WORD( <word>, <npairs> )
** resizes the <word> such that it will hold <npairs> generator/exponent
** pairs.
**
**
** BITS_WORD( <word> )
** returns the number of bits as C integers
**
** DATA_WORD( <word> )
** returns a pointer to the beginning of the data area
**
** EBITS_WORD( <word> )
** returns the ebits as C integer
**
** NPAIRS_WORD( <word> )
** returns the number of pairs as C integer
**
** RANK_WORD( <word> )
** returns the rank as C integer
**
** PURETYPE_WORD( <word> )
** returns the result type
**
**
** BITS_WORDTYPE( <type> )
** returns the number of bits as C integers
**
** EBITS_WORDTYPE( <type> )
** returns the ebits as C integer
**
** RANK_WORDTYPE( <type> )
** returns the rank as C integer
**
** PURETYPE_WORDTYPE( <type> )
** returns the result type
*/
#include <assert.h> /* assert */
#include <src/system.h> /* Ints, UInts */
#include <src/gasman.h> /* garbage collector */
#include <src/objects.h> /* objects */
#include <src/scanner.h> /* scanner */
#include <src/gap.h> /* error handling, initialisation */
#include <src/gvars.h> /* global variables */
#include <src/calls.h> /* generic call mechanism */
#include <src/opers.h> /* generic operations */
#include <src/ariths.h> /* arithmetic macros */
#include <src/records.h> /* generic records */
#include <src/precord.h> /* plain records */
#include <src/lists.h> /* generic lists */
#include <src/plist.h> /* plain lists */
#include <src/stringobj.h> /* strings */
#include <src/bool.h> /* booleans */
#include <src/objfgelm.h> /* objects of free groups */
/****************************************************************************
**
*F * * * * * * * * * * * * * * * * 8 bits words * * * * * * * * * * * * * * *
*/
/****************************************************************************
**
*F Func8Bits_Equal( <self>, <l>, <r> )
*/
Obj Func8Bits_Equal (
Obj self,
Obj l,
Obj r )
{
Int nl; /* number of pairs to consider in <l> */
Int nr; /* number of pairs in <r> */
UInt1 * pl; /* data area in <l> */
UInt1 * pr; /* data area in <r> */
/* if <l> or <r> is the identity it is easy */
nl = NPAIRS_WORD(l);
nr = NPAIRS_WORD(r);
if ( nl != nr ) {
return False;
}
/* compare the generator/exponent pairs */
pl = (UInt1*)DATA_WORD(l);
pr = (UInt1*)DATA_WORD(r);
for ( ; 0 < nl; nl--, pl++, pr++ ) {
if ( *pl != *pr ) {
return False;
}
}
return True;
}
/****************************************************************************
**
*F Func8Bits_ExponentSums3( <self>, <obj>, <start>, <end> )
*/
Obj Func8Bits_ExponentSums3 (
Obj self,
Obj obj,
Obj vstart,
Obj vend )
{
Int start; /* the lowest generator number */
Int end; /* the highest generator number */
Obj sums; /* result, the exponent sums */
Int ebits; /* number of bits in the exponent */
UInt expm; /* signed exponent mask */
UInt exps; /* sign exponent mask */
Int num; /* number of gen/exp pairs in <data> */
Int i; /* loop variable for gen/exp pairs */
Int pos; /* current generator number */
Int exp; /* current exponent */
UInt1 * ptr; /* pointer into the data area of <obj> */
/* <start> must be positive */
while ( !IS_POS_INTOBJ(vstart) )
vstart = ErrorReturnObj( "<start> must be a positive integer", 0L, 0L,
"you can replace <start> via 'return <start>;'" );
start = INT_INTOBJ(vstart);
/* <end> must be positive */
while ( !IS_POS_INTOBJ(vend) )
vend = ErrorReturnObj( "<end> must be a positive integer", 0L, 0L,
"you can replace <end> via 'return <end>;'" );
end = INT_INTOBJ(vend);
/* <end> must be at least <start> */
if ( end < start ) {
sums = NEW_PLIST( T_PLIST_CYC, 0 );
SET_LEN_PLIST( sums, 0 );
return sums;
}
/* get the number of bits for exponents */
ebits = EBITS_WORD(obj);
/* get the exponent masks */
exps = 1UL << (ebits-1);
expm = exps - 1;
/* get the number of gen/exp pairs */
num = NPAIRS_WORD(obj);
/* create the zero vector */
sums = NEW_PLIST( T_PLIST_CYC, end-start+1 );
SET_LEN_PLIST( sums, end-start+1 );
for ( i = start; i <= end; i++ )
SET_ELM_PLIST( sums, i-start+1, 0 );
/* and unpack <obj> into <sums> */
ptr = (UInt1*)DATA_WORD(obj);
for ( i = 1; i <= num; i++, ptr++ ) {
pos = ((*ptr) >> ebits)+1;
if ( start <= pos && pos <= end ) {
if ( (*ptr) & exps )
exp = ((*ptr)&expm)-exps;
else
exp = (*ptr)&expm;
/* this will not cause a garbage collection */
exp = exp + (Int) ELM_PLIST( sums, pos-start+1 );
SET_ELM_PLIST( sums, pos-start+1, (Obj) exp );
assert( ptr == (UInt1*)DATA_WORD(obj) + (i-1) );
}
}
/* convert integers into values */
for ( i = start; i <= end; i++ ) {
exp = (Int) ELM_PLIST( sums, i-start+1 );
SET_ELM_PLIST( sums, i-start+1, INTOBJ_INT(exp) );
}
/* return the exponent sums */
return sums;
}
/****************************************************************************
**
*F Func8Bits_ExponentSums1( <self>, <obj> )
*/
Obj Func8Bits_ExponentSums1 (
Obj self,
Obj obj )
{
return Func8Bits_ExponentSums3( self, obj,
INTOBJ_INT(1), INTOBJ_INT(RANK_WORD(obj)) );
}
/****************************************************************************
**
*F Func8Bits_ExponentSyllable( <self>, <w>, <i> )
*/
Obj Func8Bits_ExponentSyllable (
Obj self,
Obj w,
Obj vi )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* signed exponent mask */
UInt exps; /* sign exponent mask */
Int num; /* number of gen/exp pairs in <data> */
Int i; /* integer corresponding to <vi> */
UInt1 p; /* <i>th syllable */
/* check <i> */
num = NPAIRS_WORD(w);
while ( !IS_INTOBJ(vi) || INT_INTOBJ(vi) <= 0 || num < INT_INTOBJ(vi) )
vi = ErrorReturnObj( "<i> must be an integer between 1 and %d", num, 0L,
"you can replace <i> via 'return <i>;'" );
i = INT_INTOBJ(vi);
/* get the number of bits for exponents */
ebits = EBITS_WORD(w);
/* get the exponent masks */
exps = 1UL << (ebits-1);
expm = exps - 1;
/* return the <i> th exponent */
p = ((UInt1*)DATA_WORD(w))[i-1];
if ( p & exps )
return INTOBJ_INT((p&expm)-exps);
else
return INTOBJ_INT(p&expm);
}
/****************************************************************************
**
*F Func8Bits_ExtRepOfObj( <self>, <obj> )
*/
Obj Func8Bits_ExtRepOfObj (
Obj self,
Obj obj )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* signed exponent mask */
UInt exps; /* sign exponent mask */
Int num; /* number of gen/exp pairs in <data> */
Int i; /* loop variable for gen/exp pairs */
Obj type; /* type of <obj> */
UInt1 * ptr; /* pointer into the data area of <obj> */
Obj lst; /* result */
/* get the type of <obj> */
type = TYPE_DATOBJ(obj);
/* get the number of bits for exponents */
ebits = EBITS_WORDTYPE(type);
/* get the exponent masks */
exps = 1UL << (ebits-1);
expm = exps - 1;
/* get the number of gen/exp pairs */
num = NPAIRS_WORD(obj);
/* construct a list with 2*<num> entries */
lst = NEW_PLIST( T_PLIST, 2*num );
SET_LEN_PLIST( lst, 2*num );
/* and unpack <obj> into <lst> */
ptr = (UInt1*)DATA_WORD(obj);
/* this will not cause a garbage collection */
for ( i = 1; i <= num; i++, ptr++ ) {
SET_ELM_PLIST( lst, 2*i-1, INTOBJ_INT(((*ptr) >> ebits)+1) );
if ( (*ptr) & exps )
SET_ELM_PLIST( lst, 2*i, INTOBJ_INT(((*ptr)&expm)-exps) );
else
SET_ELM_PLIST( lst, 2*i, INTOBJ_INT((*ptr)&expm) );
assert( ptr == (UInt1*)DATA_WORD(obj) + (i-1) );
}
CHANGED_BAG(lst);
/* return the gen/exp list */
return lst;
}
/****************************************************************************
**
*F Func8Bits_GeneratorSyllable( <self>, <w>, <i> )
*/
Obj Func8Bits_GeneratorSyllable (
Obj self,
Obj w,
Obj vi )
{
Int ebits; /* number of bits in the exponent */
Int num; /* number of gen/exp pairs in <data> */
Int i; /* integer corresponding to <vi> */
UInt1 p; /* <i>th syllable */
/* check <i> */
num = NPAIRS_WORD(w);
while ( !IS_INTOBJ(vi) || INT_INTOBJ(vi) <= 0 || num < INT_INTOBJ(vi) )
vi = ErrorReturnObj( "<i> must be an integer between 1 and %d", num, 0L,
"you can replace <i> via 'return <i>;'" );
i = INT_INTOBJ(vi);
/* get the number of bits for exponents */
ebits = EBITS_WORD(w);
/* return the <i> th generator */
p = ((UInt1*)DATA_WORD(w))[i-1];
return INTOBJ_INT((p >> ebits)+1);
}
/****************************************************************************
**
*F Func8Bits_HeadByNumber( <self>, <l>, <gen> )
*/
Obj Func8Bits_HeadByNumber (
Obj self,
Obj l,
Obj r )
{
Int ebits; /* number of bits in the exponent */
UInt genm; /* generator mask */
Int sl; /* start position in <obj> */
Int nl; /* number of pairs to consider in <l> */
Int gr; /* value of <r> */
UInt1 * pl; /* data area in <l> */
Obj obj; /* the result */
UInt1 * po; /* data area in <obj> */
/* get the generator number to stop */
gr = INT_INTOBJ(r) - 1;
/* get the number of bits for exponents */
ebits = EBITS_WORD(l);
/* get the generator mask */
genm = ((1UL << (8-ebits)) - 1) << ebits;
/* if <l> is the identity return */
nl = NPAIRS_WORD(l);
if ( 0 == nl ) return l;
/* look closely at the generators */
sl = 0;
pl = (UInt1*)DATA_WORD(l);
while ( sl < nl && ((*pl & genm) >> ebits) < gr ) {
sl++; pl++;
}
if ( sl == nl )
return l;
/* create a new word */
NEW_WORD( obj, PURETYPE_WORD(l), sl );
/* copy the <l> part into the word */
po = (UInt1*)DATA_WORD(obj);
pl = (UInt1*)DATA_WORD(l);
while ( 0 < sl-- )
*po++ = *pl++;
return obj;
}
/****************************************************************************
**
*F Func8Bits_Less( <self>, <l>, <r> )
**
** This function implements a length-plus-lexicographic ordering on
** associative words. This ordering is translation invariant, therefore,
** we can skip common prefixes. This is done in the first loop of the
** function. When a difference in the two words is encountered, it is
** decided which is the lexicographically smaller. There are several case
** that can occur:
**
** The syllables where the difference occurs have different generators. In
** this case it is sufficient to compare the two generators.
** Example: x^3 < y^3.
**
** The syllables have the same generator but one exponent is the negative of
** the other. In this case the word with the negative exponent is smaller.
** Example: x^-1 < x.
**
** Now it suffices to compare the unsigned exponents. For the syllable
** with the smaller exponent we have to take the next generator in that
** word into account. This means that we are discarding the smaller
** syllable as a common prefix. Note that if this happens at the end of
** one of the two words, then this word (ignoring the common prefix) is the
** empty word and we can immediately decide which word is the smaller.
** Examples: y^3 x < y^2 z, y^3 x > y^2 x z, x^2 < x y^-1, x^2 < x^3.
**
*/
Obj Func8Bits_Less (
Obj self,
Obj l,
Obj r )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* signed exponent mask */
UInt exps; /* sign exponent mask */
UInt genm; /* generator mask */
Int exl; /* left exponent */
Int exr; /* right exponent */
Int nl; /* number of pairs to consider in <l> */
Int nr; /* number of pairs in <r> */
UInt1 * pl; /* data area in <l> */
UInt1 * pr; /* data area in <r> */
Obj lexico; /* lexicographic order of <l> and <r> */
Obj ll; /* length of <l> */
Obj lr; /* length of <r> */
/* if <l> or <r> is the identity it is easy */
nl = NPAIRS_WORD(l);
nr = NPAIRS_WORD(r);
if ( nl == 0 || nr == 0 ) {
return ( nr != 0 ) ? True : False;
}
/* get the number of bits for exponents */
ebits = EBITS_WORD(l);
/* get the exponent masks */
exps = 1UL << (ebits-1);
expm = exps - 1;
/* Skip the common prefix and determine if the first word is smaller */
/* with respect to the lexicographic ordering. */
pl = (UInt1*)DATA_WORD(l);
pr = (UInt1*)DATA_WORD(r);
for ( lexico = False; 0 < nl && 0 < nr; nl--, nr--, pl++, pr++ )
if ( *pl != *pr ) {
/* got a difference */
/* get the generator mask */
genm = ((1UL << (8-ebits)) - 1) << ebits;
/* compare the generators */
if ( (*pl & genm) != (*pr & genm) ) {
lexico = ( (*pl & genm) < (*pr & genm) ) ? True : False;
break;
}
/* get the unsigned exponents */
exl = (*pl & exps) ? (exps - (*pl & expm)) : (*pl & expm);
exr = (*pr & exps) ? (exps - (*pr & expm)) : (*pr & expm);
/* compare the sign of the exponents */
if( exl == exr && (*pl & exps) != (*pr & exps) ) {
lexico = (*pl & exps) ? True : False;
break;
}
/* compare the exponents, and check the next generator. This */
/* amounts to stripping off the common prefix x^|expl-expr|. */
if( exl > exr ) {
if( nr > 0 ) {
lexico = (*pl & genm) < (*(pr+1) & genm) ? True : False;
break;
}
else
/* <r> is now essentially the empty word. */
return False;
}
if( nl > 0 ) { /* exl < exr */
lexico = (*(pl+1) & genm) < (*pr & genm) ? True : False;
break;
}
/* <l> is now essentially the empty word. */
return True;
}
/* compute the lengths of the rest */
for ( ll = INTOBJ_INT(0); 0 < nl; nl--, pl++ ) {
exl = (*pl & exps) ? (exps - (*pl & expm)) : (*pl & expm);
C_SUM_FIA(ll,ll,INTOBJ_INT(exl));
}
for ( lr = INTOBJ_INT(0); 0 < nr; nr--, pr++ ) {
exr = (*pr & exps) ? (exps - (*pr & expm)) : (*pr & expm);
C_SUM_FIA(lr,lr,INTOBJ_INT(exr));
}
if( EQ( ll, lr ) ) return lexico;
return LT( ll, lr ) ? True : False;
}
/****************************************************************************
**
*F Func8Bits_AssocWord( <self>, <type>, <data> )
*/
Obj Func8Bits_AssocWord (
Obj self,
Obj type,
Obj data )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* unsigned exponent mask */
Int num; /* number of gen/exp pairs in <data> */
Int i; /* loop variable for gen/exp pairs */
Obj vexp; /* value of current exponent */
Int nexp; /* current exponent */
Obj vgen; /* value of current generator */
Int ngen; /* current generator */
Obj obj; /* result */
UInt1 * ptr; /* pointer into the data area of <obj> */
/* get the number of bits for exponents */
ebits = EBITS_WORDTYPE(type);
/* get the exponent mask */
expm = (1UL << ebits) - 1;
/* construct a new object */
num = LEN_LIST(data)/2;
NEW_WORD( obj, type, num );
/* use UInt1 pointer for eight bits */
ptr = (UInt1*)DATA_WORD(obj);
for ( i = 1; i <= num; i++, ptr++ ) {
/* this will not cause a garbage collection */
vgen = ELMW_LIST( data, 2*i-1 );
ngen = INT_INTOBJ(vgen);
vexp = ELMW_LIST( data, 2*i );
while ( ! IS_INTOBJ(vexp) || vexp == INTOBJ_INT(0) ) {
vexp = ErrorReturnObj( "<exponent> must be a non-zero integer",
0L, 0L,
"you can replace <exponent> via 'return <exponent>;'" );
}
nexp = INT_INTOBJ(vexp) & expm;
*ptr = ((ngen-1) << ebits) | nexp;
assert( ptr == (UInt1*)DATA_WORD(obj) + (i-1) );
}
CHANGED_BAG(obj);
return obj;
}
/****************************************************************************
**
*F Func8Bits_ObjByVector( <self>, <type>, <data> )
*/
Obj Func8Bits_ObjByVector (
Obj self,
Obj type,
Obj data )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* unsigned exponent mask */
Int num; /* number of gen/exp pairs in <data> */
Int i; /* loop variable for gen/exp pairs */
Int j; /* loop variable for exponent vector */
Int nexp; /* current exponent */
Obj vexp; /* value of current exponent */
Obj obj; /* result */
UInt1 * ptr; /* pointer into the data area of <obj> */
/* get the number of bits for exponents */
ebits = EBITS_WORDTYPE(type);
/* get the exponent mask */
expm = (1UL << ebits) - 1;
/* count the number of non-zero entries */
for ( i = LEN_LIST(data), num = 0, j = 1; 0 < i; i-- ) {
vexp = ELMW_LIST(data,i);
while ( ! IS_INTOBJ(vexp) ) {
vexp = ErrorReturnObj(
"%d element must be a small integer (not a %s)",
(Int) i, (Int) TNAM_OBJ(vexp),
"you can replace the element by <val> via 'return <val>;'" );
}
if ( vexp != INTOBJ_INT(0) ) {
j = i;
num++;
}
}
/* construct a new object */
NEW_WORD( obj, type, num );
/* use UInt1 pointer for eight bits */
ptr = (UInt1*)DATA_WORD(obj);
for ( i = 1; i <= num; i++, ptr++, j++ ) {
/* this will not cause a garbage collection */
while ( ELMW_LIST(data,j) == INTOBJ_INT(0) )
j++;
vexp = ELMW_LIST( data, j );
nexp = INT_INTOBJ(vexp) & expm;
*ptr = ((j-1) << ebits) | nexp;
assert( ptr == (UInt1*)DATA_WORD(obj) + (i-1) );
}
CHANGED_BAG(obj);
return obj;
}
/****************************************************************************
**
*F Func8Bits_Power( <self>, <l>, <r> )
*/
Obj Func8Bits_Power (
Obj self,
Obj l,
Obj r )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* signed exponent mask */
UInt exps; /* sign exponent mask */
UInt genm; /* generator mask */
Int invm; /* mask used to invert exponent */
Obj obj; /* the result */
Int nl; /* number of pairs to consider in <l> */
Int sr; /* start position in <r> */
Int sl; /* start position in <obj> */
UInt1 * pl; /* data area in <l> */
UInt1 * pr; /* data area in <obj> */
UInt1 * pe; /* end marker */
Int ex = 0; /* meeting exponent */
Int pow; /* power to take */
Int apw; /* absolute value of <pow> */
/* get the number of bits for exponents */
ebits = EBITS_WORD(l);
/* get the exponent masks */
exps = 1UL << (ebits-1);
expm = exps - 1;
invm = (1UL<<ebits)-1;
/* get the generator mask */
genm = ((1UL << (8-ebits)) - 1) << ebits;
/* if <l> is the identity return <l> */
nl = NPAIRS_WORD(l);
if ( 0 == nl )
return l;
/* if <pow> is zero return the identity */
pow = INT_INTOBJ(r);
if ( pow == 0 ) {
NEW_WORD( obj, PURETYPE_WORD(l), 0 );
return obj;
}
/* if <pow> is one return <l> */
if ( pow == 1 )
return l;
/* if <pow> is minus one invert <l> */
if ( pow == -1 ) {
NEW_WORD( obj, PURETYPE_WORD(l), nl );
pl = (UInt1*)DATA_WORD(l);
pr = (UInt1*)DATA_WORD(obj) + (nl-1);
sl = nl;
/* exponents are symmtric, so we cannot get an overflow */
while ( 0 < sl-- ) {
*pr-- = ( *pl++ ^ invm ) + 1;
}
return obj;
}
/* split word into w * h * w^-1 */
pl = (UInt1*)DATA_WORD(l);
pr = pl + (nl-1);
sl = 0;
sr = nl-1;
while ( (*pl & genm) == (*pr & genm) ) {
if ( (*pl&exps) == (*pr&exps) )
break;
if ( (*pl&expm) + (*pr&expm) != exps )
break;
pl++; sl++;
pr--; sr--;
}
/* special case: w * gi^n * w^-1 */
if ( sl == sr ) {
ex = (*pl&expm);
if ( *pl & exps ) ex -= exps;
ex = ex * pow;
/* check that n*pow fits into the exponent */
if ( ( 0 < ex && expm < ex ) || ( ex < 0 && expm < -ex ) ) {
return TRY_NEXT_METHOD;
}
/* copy <l> into <obj> */
NEW_WORD( obj, PURETYPE_WORD(l), nl );
pl = (UInt1*)DATA_WORD(l);
pr = (UInt1*)DATA_WORD(obj);
sl = nl;
while ( 0 < sl-- ) {
*pr++ = *pl++;
}
/* and fix the exponent at position <sr> */
pr = (UInt1*)DATA_WORD(obj);
pr[sr] = (pr[sr] & genm) | (ex & ((1UL<<ebits)-1));
return obj;
}
/* special case: w * gj^x * t * gj^y * w^-1, x != -y */
if ( (*pl & genm) == (*pr & genm) ) {
ex = (*pl&expm) + (*pr&expm);
if ( *pl & exps ) ex -= exps;
if ( *pr & exps ) ex -= exps;
/* check that <ex> fits into the exponent */
if ( ( 0 < ex && expm < ex ) || ( ex < 0 && expm < -ex ) ) {
return TRY_NEXT_METHOD;
}
if ( 0 < pow )
ex = ex & ((1UL<<ebits)-1);
else
ex = (-ex) & ((1UL<<ebits)-1);
/* create a new word */
apw = ( pow < 0 ) ? -pow : pow;
NEW_WORD( obj, PURETYPE_WORD(l), 2*(sl+1)+apw*(sr-sl-1)+(apw-1) );
/* copy the beginning w * gj^x into <obj> */
pl = (UInt1*)DATA_WORD(l);
pr = (UInt1*)DATA_WORD(obj);
pe = pl+sl;
while ( pl <= pe ) {
*pr++ = *pl++;
}
/* copy t * gj^<ex> <pow> times into <obj> */
if ( 0 < pow ) {
for ( ; 0 < apw; apw-- ) {
pl = (UInt1*)DATA_WORD(l) + (sl+1);
pe = pl + (sr-sl-1);
while ( pl <= pe ) {
*pr++ = *pl++;
}
pr[-1] = (pr[-1] & genm) | ex;
}
/* copy tail gj^y * w^-1 into <obj> */
pr[-1] = pl[-1];
pe = (UInt1*)DATA_WORD(l) + nl;
while ( pl < pe ) {
*pr++ = *pl++;
}
}
/* copy and invert t * gj^<ex> <pow> times into <obj> */
else {
pr[-1] = ( pl[sr-sl-1] ^ invm ) + 1;
for ( ; 0 < apw; apw-- ) {
pl = (UInt1*)DATA_WORD(l) + (sr-1);
pe = pl + (sl-sr+1);
while ( pe <= pl ) {
*pr++ = ( *pl-- ^ invm ) + 1;
}
pr[-1] = (pr[-1] & genm) | ex;
}
/* copy tail gj^x * w^-1 into <obj> */
pr[-1] = ( pl[1] ^ invm ) + 1;
pl = (UInt1*)DATA_WORD(l) + (sr+1);
pe = (UInt1*)DATA_WORD(l) + nl;
while ( pl < pe ) {
*pr ++ = *pl++;
}
}
return obj;
}
/* general case: w * t * w^-1 */
else {
/* create a new word */
apw = ( pow < 0 ) ? -pow : pow;
NEW_WORD( obj, PURETYPE_WORD(l), 2*sl+apw*(sr-sl+1) );
/* copy the beginning w * gj^x into <obj> */
pl = (UInt1*)DATA_WORD(l);
pr = (UInt1*)DATA_WORD(obj);
pe = pl+sl;
while ( pl < pe ) {
*pr++ = *pl++;
}
/* copy t <pow> times into <obj> */
if ( 0 < pow ) {
for ( ; 0 < apw; apw-- ) {
pl = (UInt1*)DATA_WORD(l) + sl;
pe = pl + (sr-sl);
while ( pl <= pe ) {
*pr++ = *pl++;
}
}
/* copy tail w^-1 into <obj> */
pe = (UInt1*)DATA_WORD(l) + nl;
while ( pl < pe ) {
*pr++ = *pl++;
}
}
/* copy and invert t <pow> times into <obj> */
else {
for ( ; 0 < apw; apw-- ) {
pl = (UInt1*)DATA_WORD(l) + sr;
pe = pl + (sl-sr);
while ( pe <= pl ) {
*pr++ = ( *pl-- ^ invm ) + 1;
}
}
/* copy tail w^-1 into <obj> */
pl = (UInt1*)DATA_WORD(l) + (sr+1);
pe = (UInt1*)DATA_WORD(l) + nl;
while ( pl < pe ) {
*pr ++ = *pl++;
}
}
return obj;
}
}
/****************************************************************************
**
*F Func8Bits_Product( <self>, <l>, <r> )
*/
Obj Func8Bits_Product (
Obj self,
Obj l,
Obj r )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* signed exponent mask */
UInt exps; /* sign exponent mask */
UInt genm; /* generator mask */
Int nl; /* number of pairs to consider in <l> */
Int nr; /* number of pairs in <r> */
Int sr; /* start position in <r> */
UInt1 * pl; /* data area in <l> */
UInt1 * pr; /* data area in <r> */
Obj obj; /* the result */
UInt1 * po; /* data area in <obj> */
Int ex = 0; /* meeting exponent */
Int over; /* overlap */
/* get the number of bits for exponents */
ebits = EBITS_WORD(l);
/* get the exponent masks */
exps = 1UL << (ebits-1);
expm = exps - 1;
/* get the generator mask */
genm = ((1UL << (8-ebits)) - 1) << ebits;
/* if <l> or <r> is the identity return the other */
nl = NPAIRS_WORD(l);
if ( 0 == nl ) return r;
nr = NPAIRS_WORD(r);
if ( 0 == nr ) return l;
/* look closely at the meeting point */
sr = 0;
pl = (UInt1*)DATA_WORD(l)+(nl-1);
pr = (UInt1*)DATA_WORD(r);
while ( 0 < nl && sr < nr && (*pl & genm) == (*pr & genm) ) {
if ( (*pl&exps) == (*pr&exps) )
break;
if ( (*pl&expm) + (*pr&expm) != exps )
break;
pr++; sr++;
pl--; nl--;
}
/* create a new word */
over = ( 0 < nl && sr < nr && (*pl & genm) == (*pr & genm) ) ? 1 : 0;
if ( over ) {
ex = ( *pl & expm ) + ( *pr & expm );
if ( *pl & exps ) ex -= exps;
if ( *pr & exps ) ex -= exps;
if ( ( 0 < ex && expm < ex ) || ( ex < 0 && expm < -ex ) ) {
return TRY_NEXT_METHOD;
}
}
NEW_WORD( obj, PURETYPE_WORD(l), nl+(nr-sr)-over );
/* copy the <l> part into the word */
po = (UInt1*)DATA_WORD(obj);
pl = (UInt1*)DATA_WORD(l);
while ( 0 < nl-- )
*po++ = *pl++;
/* handle the overlap */
if ( over ) {
po[-1] = (po[-1] & genm) | (ex & ((1UL<<ebits)-1));
sr++;
}
/* copy the <r> part into the word */
pr = ((UInt1*)DATA_WORD(r)) + sr;
while ( sr++ < nr )
*po++ = *pr++;
return obj;
}
/****************************************************************************
**
*F Func8Bits_Quotient( <self>, <l>, <r> )
*/
Obj Func8Bits_Quotient (
Obj self,
Obj l,
Obj r )
{
Int ebits; /* number of bits in the exponent */
UInt expm; /* signed exponent mask */
UInt sepm; /* unsigned exponent mask */
UInt exps; /* sign exponent mask */
UInt genm; /* generator mask */
Int nl; /* number of pairs to consider in <l> */
Int nr; /* number of pairs in <r> */
UInt1 * pl; /* data area in <l> */
UInt1 * pr; /* data area in <r> */
Obj obj; /* the result */
UInt1 * po; /* data area in <obj> */
Int ex = 0; /* meeting exponent */
Int over; /* overlap */
/* get the number of bits for exponents */
ebits = EBITS_WORD(l);
/* get the exponent masks */
exps = 1UL << (ebits-1);
expm = exps - 1;
sepm = (1UL << ebits) - 1;
/* get the generator mask */
genm = ((1UL << (8-ebits)) - 1) << ebits;
/* if <r> is the identity return <l> */
nl = NPAIRS_WORD(l);
nr = NPAIRS_WORD(r);
if ( 0 == nr ) return l;