summaryrefslogtreecommitdiff
path: root/src/pkg/integer.go
blob: f6184c4cbd979d18062d4c8f65c483ad7b37ad3c (plain)
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
// Copyright 2009 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package Integer

const ValueLen = 1000;
type Word uint32
type Value *[ValueLen]Word
export type IntegerImpl struct {
  val Value
}
export type Integer *IntegerImpl

const N = 4;
const H = 1
const L = 28;
const M = 1 << L - 1;


// ----------------------------------------------------------------------------
// Support

// TODO What are we going to about asserts?
func ASSERT(p bool) {
  if !p {
    panic("ASSERT failed");
  }
}


func CHECK(p bool) {
  if !p {
    panic("CHECK failed");
  }
}


func UNIMPLEMENTED(s string) {
  panic("UNIMPLEMENTED: ", s);
}


// ----------------------------------------------------------------------------
//

// TODO "len" is a reserved word at the moment - I think that's wrong
func len_(x Value) int {
  l := int(x[0]);
  if l < 0 { return -l; }
  return l;
}


func set_len(x Value, len_ int) {
  x[0] = Word(len_);
}


func alloc(len_ int) Value {
  ASSERT(len_ >= 0);
  z := new([ValueLen] Word);
  set_len(z, len_);
  return z;
}


func sign(x Value) bool {
  return int(x[0]) < 0;
}


func zero(x Value) bool {
  return x[0] == 0;
}


func neg(x Value) {
  x[0] = Word(-int(x[0]));
}


// ----------------------------------------------------------------------------
// Unsigned ops

func make(x int) Value;


func Update(x Word) (z, c Word) {
  // z = x & M;
  // c = x >> L;
  return x & M, x >> L;
}


func uadd(x, y Value) Value {
  xl := len_(x);
  yl := len_(y);
  if xl < yl {
    return uadd(y, x);
  }
  ASSERT(xl >= yl);
  z := alloc(xl + 1);

  i := 0;
  c := Word(0);
  for i < yl { z[i + H], c = Update(x[i + H] + y[i + H] + c); i++; }
  for i < xl { z[i + H], c = Update(x[i + H] + c); i++; }
  if c != 0 { z[i + H] = c; i++; }
  set_len(z, i);

  return z;
}


func usub(x, y Value) Value {
  xl := len_(x);
  yl := len_(y);
  if xl < yl {
    return uadd(y, x);
  }
  ASSERT(xl >= yl);
  z := alloc(xl + 1);

  i := 0;
  c := Word(0);
  for i < yl { z[i + H], c = Update(x[i + H] - y[i + H] + c); i++; }
  for i < xl { z[i + H], c = Update(x[i + H] + c); i++; }
  ASSERT(c == 0);  // usub(x, y) must be called with x >= y
  for i > 0 && z[i - 1 + H] == 0 { i--; }
  set_len(z, i);

  return z;
}


// Computes x = x*a + c (in place) for "small" a's.
func umul_add(x Value, a, c Word) Value {
  CHECK(0 <= a && a < (1 << N));
  CHECK(0 <= c && c < (1 << N));
  if (zero(x) || a == 0) && c == 0 {
    return make(0);
  }
  xl := len_(x);
  
  z := alloc(xl + 1);
  i := 0;
  for i < xl { z[i + H], c = Update(x[i + H] * a + c); i++; }
  if c != 0 { z[i + H] = c; i++; }  
  set_len(z, i);

  return z;
}


// Computes x = x div d (in place) for "small" d's. Returns x mod d.
func umod(x Value, d Word) Word {
  CHECK(0 < d && d < (1 << N));
  xl := len_(x);
  c := Word(0);

  i := xl;
  for i > 0 {
    i--;
    c = c << L + x[i + H];

    q := c / d;
    x[i + H] = q;
    
    //x[i + H] = c / d;  // BUG
    
    c = c % d;
  }
  if xl > 0 && x[xl - 1 + H] == 0 {
    set_len(x, xl - 1);
  }

  return c;
}


// Returns z = (x * y) div B, c = (x * y) mod B.
func umul1(x, y Word) (z Word, c Word) {
  const L2 = (L + 1) >> 1;
  const B2 = 1 << L2;
  const M2 = B2 - 1;
  
  x0 := x & M2;
  x1 := x >> L2;

  y0 := y & M2;
  y1 := y >> L2;
  
  z10 := x0*y0;
  z21 := x1*y0 + x0*y1 + (z10 >> L2);

  cc := x1*y1 + (z21 >> L2);  
  zz := ((z21 & M2) << L2) | (z10 & M2);
  return zz, cc
}


func umul(x Value, y Value) Value {
  if zero(x) || zero(y) {
    return make(0);
  }
  xl := len_(x);
  yl := len_(y);
  if xl < yl {
    return umul(y, x);  // for speed
  }
  ASSERT(xl >= yl && yl > 0);
  
  // initialize z
  zl := xl + yl;
  z := alloc(zl);
  for i := 0; i < zl; i++ { z[i + H] = 0; }
  
  k := 0;
  for j := 0; j < yl; j++ {
    d := y[j + H];
    if d != 0 {
      k = j;
      c := Word(0);
      for i := 0; i < xl; i++ {
        // compute z[k + H] += x[i + H] * d + c;
        t := z[k + H] + c;
        var z1 Word;
        z1, c = umul1(x[i + H], d);
        t += z1;
        z[k + H] = t & M;
        c += t >> L;
        k++;
      }
      if c != 0 {
        z[k + H] = Word(c);
        k++;
      }
    }
  }
  set_len(z, k);

  return z;
}


func ucmp(x Value, y Value) int {
  xl := len_(x);
  yl := len_(y);
  
  if xl != yl || xl == 0 {
    return xl - yl;
  }
  
  i := xl - 1;
  for i > 0 && x[i + H] == y[i + H] { i--; }
  return int(x[i + H]) - int(y[i + H]);
}


func ulog(x Value) int {
  xl := len_(x);
  if xl == 0 { return 0; }
  
  n := (xl - 1) * L;
  for t := x[xl - 1 + H]; t != 0; t >>= 1 { n++ };
  
  return n;
}


func make(x int) Value {
  if x == 0 {
    z := alloc(0);
    set_len(z, 0);
    return z;
  }
  
  if x == -x {  // smallest int
    z := alloc(2);
    z[0 + H] = 0;
    z[1 + H] = 1;
    set_len(z, -2);
    return z;
  }
  
  z := alloc(1);
  if x < 0 {
    z[0 + H] = Word(-x);
    set_len(z, -1);
  } else {
    z[0 + H] = Word(x);
    set_len(z, 1);
  }
  
  return z;
}


func make_from_string(s string) Value {
  // skip sign, if any
  i := 0;
  if len(s) > 0 && (s[i] == '-' || s[i] == '+') {
    i = 1;
  }
  
  // read digits
  x := make(0);
  for i < len(s) && '0' <= s[i] && s[i] <= '9' {
    x = umul_add(x, 10, Word(s[i] - '0'));
    i++;
  }
  
  // read sign
  if len(s) > 0 && s[0] == '-' {
    neg(x);
  }
  
  return x;
}


// ----------------------------------------------------------------------------
// Creation


func (x Integer) Init(val Value) Integer {
  x.val = val;
  return x;
}


// ----------------------------------------------------------------------------
// Signed ops

func add(x Value, y Value) Value {
  var z Value;
  if sign(x) == sign(y) {
    // x + y == x + y
    // (-x) + (-y) == -(x + y)
    z = uadd(x, y);
  } else {
    // x + (-y) == x - y == -(y - x)
    // (-x) + y == y - x == -(x - y)
    if ucmp(x, y) >= 0 {
      z = usub(x, y);
    } else {
      z = usub(y, x);
      neg(z);
    }
  }
  if sign(x) {
    neg(z);
  }
  return z;
}


func sub(x Value, y Value) Value {
  var z Value;
  if sign(x) != sign(y) {
    // x - (-y) == x + y
    // (-x) - y == -(x + y)
    z = uadd(x, y);
  } else {
    // x - y == x - y == -(y - x)
    // (-x) - (-y) == y - x == -(x - y)
    if ucmp(x, y) >= 0 {
      z = usub(x, y);
    } else {
      z = usub(y, x);
      neg(z);
    }
  }
  if sign(x) {
    neg(z);
  }
  return z;
}


func mul(x Value, y Value) Value {
  // x * y == x * y
  // x * (-y) == -(x * y)
  // (-x) * y == -(x * y)
  // (-x) * (-y) == x * y
  z := umul(x, y);
  if sign(x) != sign(y) {
    neg(z);
  }
  return z;
}


func mul_range(a, b int) Value {
  if a > b { return make(1) };
  if a == b { return make(a) };
  if a + 1 == b { return mul(make(a), make(b)) };
  m := (a + b) >> 1;
  ASSERT(a <= m && m < b);
  return mul(mul_range(a, m), mul_range(m + 1, b));
}


func fact(n int) Value {
  return mul_range(2, n);
}


// Returns a copy of x with space for one extra digit.
func copy(x Value) Value {
  xl := len_(x);
  
  z := alloc(xl + 1);  // add space for one extra digit
  for i := 0; i < xl; i++ { z[i + H] = x[i + H]; }
  set_len(z, int(x[0]));  // don't loose sign!
  
  return z;
}


func tostring(x Value) string {
  // allocate string
  // approx. length: 1 char for 3 bits
  n := ulog(x)/3 + 3;  // +1 (round up) +1 (sign) +1 (0 termination)
  //s := new([]byte, n);
  s := new([100000]byte);

  // convert
  z := copy(x);
  i := 0;
  for i == 0 || !zero(z) {
    s[i] = byte(umod(z, 10) + '0');
    i++;
  };
  if sign(x) {
    s[i] = '-';
    i++;
  }
  length := i;
  ASSERT(0 < i && i < n);
  
  // reverse in place
  i--;
  for j := 0; j < i; j++ {
    t := s[j];
    s[j] = s[i];
    s[i] = t;
    i--;
  }

  return string(s)[0:length];
}


// ----------------------------------------------------------------------------
// Creation

export func FromInt(v int) Integer {
  return new(IntegerImpl).Init(make(v));
}


export func FromString(s string) Integer {
  return new(IntegerImpl).Init(make_from_string(s));
}


// ----------------------------------------------------------------------------
// Arithmetic ops

func (x Integer) neg () Integer {
  if (zero(x.val)) {
    return new(IntegerImpl).Init(make(0));
  }
  
  z := copy(x.val);
  neg(z);
  return new(IntegerImpl).Init(z);
}


func (x Integer) add (y Integer) Integer {
  return new(IntegerImpl).Init(add(x.val, y.val));
}


func (x Integer) sub (y Integer) Integer {
  return new(IntegerImpl).Init(sub(x.val, y.val));
}


func (x Integer) mul (y Integer) Integer {
  return new(IntegerImpl).Init(mul(x.val, y.val));
}


func (x Integer) quo (y Integer) Integer {
  UNIMPLEMENTED("quo");
  return nil;
}


func (x Integer) rem (y Integer) Integer {
  UNIMPLEMENTED("rem");
  return nil;
}


func (x Integer) div (y Integer) Integer {
  UNIMPLEMENTED("div");
  return nil;
}


func (x Integer) mod (y Integer) Integer {
  UNIMPLEMENTED("mod");
  return nil;
}


// ----------------------------------------------------------------------------
// Arithmetic shifts

func (x Integer) shl (s uint) Integer {
  UNIMPLEMENTED("shl");
  return nil;
}


func (x Integer) shr (s uint) Integer {
  UNIMPLEMENTED("shr");
  return nil;
}


// ----------------------------------------------------------------------------
// Logical ops

func (x Integer) inv () Integer {
  UNIMPLEMENTED("inv");
  return nil;
}


func (x Integer) and (y Integer) Integer {
  UNIMPLEMENTED("and");
  return nil;
}


func (x Integer) or (y Integer) Integer {
  UNIMPLEMENTED("or");
  return nil;
}


func (x Integer) xor (y Integer) Integer {
  UNIMPLEMENTED("xor");
  return nil;
}


// ----------------------------------------------------------------------------
// Comparisons

func (x Integer) cmp (y Integer) int {
  // do better then this
  d := x.sub(y);
  switch {
    case sign(d.val): return -1;
    case zero(d.val): return  0;
    default         : return +1;
  }
  panic("UNREACHABLE");
}


func (x Integer) eql (y Integer) bool {
  return x.cmp(y) == 0;
}


func (x Integer) neq (y Integer) bool {
  return x.cmp(y) != 0;
}


func (x Integer) lss (y Integer) bool {
  return x.cmp(y) < 0;
}


func (x Integer) leq (y Integer) bool {
  return x.cmp(y) <= 0;
}


func (x Integer) gtr (y Integer) bool {
  return x.cmp(y) > 0;
}


func (x Integer) geq (y Integer) bool {
  return x.cmp(y) >= 0;
}


// ----------------------------------------------------------------------------
// Specials

export func Fact(n int) Integer {
  return new(IntegerImpl).Init(fact(n));
}


func (x Integer) ToString() string {
  return tostring(x.val);
}


func (x Integer) ToInt() int {
  v := x.val;
  if len_(v) <= 1 {
    if zero(v) {
      return 0;
    }
    i := int(v[0 + H]);
    if sign(v) {
      i = -i;  // incorrect for smallest int
    }
    return i;
  }
  panic("integer too large");
}