aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/internal/gc/mparith1.go
blob: 14b8620ad490a3daca45039111179f52c46c1c35 (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
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
// 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 gc

import (
	"cmd/internal/obj"
	"fmt"
	"math"
)

/// uses arithmetic

func mpcmpfixflt(a *Mpint, b *Mpflt) int {
	var buf string
	var c Mpflt

	buf = fmt.Sprintf("%v", Bconv(a, 0))
	mpatoflt(&c, buf)
	return mpcmpfltflt(&c, b)
}

func mpcmpfltfix(a *Mpflt, b *Mpint) int {
	var buf string
	var c Mpflt

	buf = fmt.Sprintf("%v", Bconv(b, 0))
	mpatoflt(&c, buf)
	return mpcmpfltflt(a, &c)
}

func Mpcmpfixfix(a *Mpint, b *Mpint) int {
	var c Mpint

	mpmovefixfix(&c, a)
	mpsubfixfix(&c, b)
	return mptestfix(&c)
}

func mpcmpfixc(b *Mpint, c int64) int {
	var c1 Mpint

	Mpmovecfix(&c1, c)
	return Mpcmpfixfix(b, &c1)
}

func mpcmpfltflt(a *Mpflt, b *Mpflt) int {
	var c Mpflt

	mpmovefltflt(&c, a)
	mpsubfltflt(&c, b)
	return mptestflt(&c)
}

func mpcmpfltc(b *Mpflt, c float64) int {
	var a Mpflt

	Mpmovecflt(&a, c)
	return mpcmpfltflt(b, &a)
}

func mpsubfixfix(a *Mpint, b *Mpint) {
	mpnegfix(a)
	mpaddfixfix(a, b, 0)
	mpnegfix(a)
}

func mpsubfltflt(a *Mpflt, b *Mpflt) {
	mpnegflt(a)
	mpaddfltflt(a, b)
	mpnegflt(a)
}

func mpaddcfix(a *Mpint, c int64) {
	var b Mpint

	Mpmovecfix(&b, c)
	mpaddfixfix(a, &b, 0)
}

func mpaddcflt(a *Mpflt, c float64) {
	var b Mpflt

	Mpmovecflt(&b, c)
	mpaddfltflt(a, &b)
}

func mpmulcfix(a *Mpint, c int64) {
	var b Mpint

	Mpmovecfix(&b, c)
	mpmulfixfix(a, &b)
}

func mpmulcflt(a *Mpflt, c float64) {
	var b Mpflt

	Mpmovecflt(&b, c)
	mpmulfltflt(a, &b)
}

func mpdivfixfix(a *Mpint, b *Mpint) {
	var q Mpint
	var r Mpint

	mpdivmodfixfix(&q, &r, a, b)
	mpmovefixfix(a, &q)
}

func mpmodfixfix(a *Mpint, b *Mpint) {
	var q Mpint
	var r Mpint

	mpdivmodfixfix(&q, &r, a, b)
	mpmovefixfix(a, &r)
}

func mpcomfix(a *Mpint) {
	var b Mpint

	Mpmovecfix(&b, 1)
	mpnegfix(a)
	mpsubfixfix(a, &b)
}

func Mpmovefixflt(a *Mpflt, b *Mpint) {
	a.Val = *b
	a.Exp = 0
	mpnorm(a)
}

// convert (truncate) b to a.
// return -1 (but still convert) if b was non-integer.
func mpexactfltfix(a *Mpint, b *Mpflt) int {
	var f Mpflt

	*a = b.Val
	Mpshiftfix(a, int(b.Exp))
	if b.Exp < 0 {
		f.Val = *a
		f.Exp = 0
		mpnorm(&f)
		if mpcmpfltflt(b, &f) != 0 {
			return -1
		}
	}

	return 0
}

func mpmovefltfix(a *Mpint, b *Mpflt) int {
	var f Mpflt
	var i int

	if mpexactfltfix(a, b) == 0 {
		return 0
	}

	// try rounding down a little
	f = *b

	f.Val.A[0] = 0
	if mpexactfltfix(a, &f) == 0 {
		return 0
	}

	// try rounding up a little
	for i = 1; i < Mpprec; i++ {
		f.Val.A[i]++
		if f.Val.A[i] != Mpbase {
			break
		}
		f.Val.A[i] = 0
	}

	mpnorm(&f)
	if mpexactfltfix(a, &f) == 0 {
		return 0
	}

	return -1
}

func mpmovefixfix(a *Mpint, b *Mpint) {
	*a = *b
}

func mpmovefltflt(a *Mpflt, b *Mpflt) {
	*a = *b
}

var tab = []float64{1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7}

func mppow10flt(a *Mpflt, p int) {
	if p < 0 {
		panic("abort")
	}
	if p < len(tab) {
		Mpmovecflt(a, tab[p])
		return
	}

	mppow10flt(a, p>>1)
	mpmulfltflt(a, a)
	if p&1 != 0 {
		mpmulcflt(a, 10)
	}
}

func mphextofix(a *Mpint, s string) {
	var c int8
	var d int
	var bit int
	var hexdigitp int
	var end int

	for s != "" && s[0] == '0' {
		s = s[1:]
	}

	// overflow
	if 4*len(s) > Mpscale*Mpprec {
		a.Ovf = 1
		return
	}

	end = len(s) - 1
	for hexdigitp = end; hexdigitp >= 0; hexdigitp-- {
		c = int8(s[hexdigitp])
		if c >= '0' && c <= '9' {
			d = int(c) - '0'
		} else if c >= 'A' && c <= 'F' {
			d = int(c) - 'A' + 10
		} else {
			d = int(c) - 'a' + 10
		}

		bit = 4 * (end - hexdigitp)
		for d > 0 {
			if d&1 != 0 {
				a.A[bit/Mpscale] |= int(1) << uint(bit%Mpscale)
			}
			bit++
			d = d >> 1
		}
	}
}

//
// floating point input
// required syntax is [+-]d*[.]d*[e[+-]d*] or [+-]0xH*[e[+-]d*]
//
func mpatoflt(a *Mpflt, as string) {
	var b Mpflt
	var dp int
	var c int
	var f int
	var ef int
	var ex int
	var eb int
	var base int
	var s string
	var start string

	for as[0] == ' ' || as[0] == '\t' {
		as = as[1:]
	}

	/* determine base */
	s = as

	base = -1
	for base == -1 {
		if s == "" {
			base = 10
			break
		}
		c := s[0]
		s = s[1:]
		switch c {
		case '-',
			'+':
			break

		case '0':
			if s != "" && s[0] == 'x' {
				base = 16
			} else {
				base = 10
			}

		default:
			base = 10
		}
	}

	s = as
	dp = 0 /* digits after decimal point */
	f = 0  /* sign */
	ex = 0 /* exponent */
	eb = 0 /* binary point */

	Mpmovecflt(a, 0.0)
	if base == 16 {
		start = ""
		for {
			c, _ = intstarstringplusplus(s)
			if c == '-' {
				f = 1
				s = s[1:]
			} else if c == '+' {
				s = s[1:]
			} else if c == '0' && s[1] == 'x' {
				s = s[2:]
				start = s
			} else if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') {
				s = s[1:]
			} else {
				break
			}
		}

		if start == "" {
			Yyerror("malformed hex constant: %s", as)
			goto bad
		}

		mphextofix(&a.Val, start[:len(start)-len(s)])
		if a.Val.Ovf != 0 {
			Yyerror("constant too large: %s", as)
			goto bad
		}

		a.Exp = 0
		mpnorm(a)
	}

	for {
		c, s = intstarstringplusplus(s)
		switch c {
		default:
			Yyerror("malformed constant: %s (at %c)", as, c)
			goto bad

		case '-':
			f = 1
			fallthrough

		case ' ',
			'\t',
			'+':
			continue

		case '.':
			if base == 16 {
				Yyerror("decimal point in hex constant: %s", as)
				goto bad
			}

			dp = 1
			continue

		case '1',
			'2',
			'3',
			'4',
			'5',
			'6',
			'7',
			'8',
			'9',
			'0':
			mpmulcflt(a, 10)
			mpaddcflt(a, float64(c)-'0')
			if dp != 0 {
				dp++
			}
			continue

		case 'P',
			'p':
			eb = 1
			fallthrough

		case 'E',
			'e':
			ex = 0
			ef = 0
			for {
				c, s = intstarstringplusplus(s)
				if c == '+' || c == ' ' || c == '\t' {
					continue
				}
				if c == '-' {
					ef = 1
					continue
				}

				if c >= '0' && c <= '9' {
					ex = ex*10 + (c - '0')
					if ex > 1e8 {
						Yyerror("constant exponent out of range: %s", as)
						errorexit()
					}

					continue
				}

				break
			}

			if ef != 0 {
				ex = -ex
			}
			fallthrough

		case 0:
			break
		}

		break
	}

	if eb != 0 {
		if dp != 0 {
			Yyerror("decimal point and binary point in constant: %s", as)
			goto bad
		}

		mpsetexp(a, int(a.Exp)+ex)
		goto out
	}

	if dp != 0 {
		dp--
	}
	if mpcmpfltc(a, 0.0) != 0 {
		if ex >= dp {
			mppow10flt(&b, ex-dp)
			mpmulfltflt(a, &b)
		} else {
			// 4 approximates least_upper_bound(log2(10)).
			if dp-ex >= 1<<(32-3) || int(int16(4*(dp-ex))) != 4*(dp-ex) {
				Mpmovecflt(a, 0.0)
			} else {
				mppow10flt(&b, dp-ex)
				mpdivfltflt(a, &b)
			}
		}
	}

out:
	if f != 0 {
		mpnegflt(a)
	}
	return

bad:
	Mpmovecflt(a, 0.0)
}

//
// fixed point input
// required syntax is [+-][0[x]]d*
//
func mpatofix(a *Mpint, as string) {
	var c int
	var f int
	var s string
	var s0 string

	s = as
	f = 0
	Mpmovecfix(a, 0)

	c, s = intstarstringplusplus(s)
	switch c {
	case '-':
		f = 1
		fallthrough

	case '+':
		c, s = intstarstringplusplus(s)
		if c != '0' {
			break
		}
		fallthrough

	case '0':
		goto oct
	}

	for c != 0 {
		if c >= '0' && c <= '9' {
			mpmulcfix(a, 10)
			mpaddcfix(a, int64(c)-'0')
			c, s = intstarstringplusplus(s)
			continue
		}

		Yyerror("malformed decimal constant: %s", as)
		goto bad
	}

	goto out

oct:
	c, s = intstarstringplusplus(s)
	if c == 'x' || c == 'X' {
		goto hex
	}
	for c != 0 {
		if c >= '0' && c <= '7' {
			mpmulcfix(a, 8)
			mpaddcfix(a, int64(c)-'0')
			c, s = intstarstringplusplus(s)
			continue
		}

		Yyerror("malformed octal constant: %s", as)
		goto bad
	}

	goto out

hex:
	s0 = s
	c, _ = intstarstringplusplus(s)
	for c != 0 {
		if (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') {
			s = s[1:]
			c, _ = intstarstringplusplus(s)
			continue
		}

		Yyerror("malformed hex constant: %s", as)
		goto bad
	}

	mphextofix(a, s0)
	if a.Ovf != 0 {
		Yyerror("constant too large: %s", as)
		goto bad
	}

out:
	if f != 0 {
		mpnegfix(a)
	}
	return

bad:
	Mpmovecfix(a, 0)
}

func Bconv(xval *Mpint, flag int) string {
	var buf [500]byte
	var p int
	var fp string

	var q Mpint
	var r Mpint
	var ten Mpint
	var sixteen Mpint
	var f int
	var digit int

	mpmovefixfix(&q, xval)
	f = 0
	if mptestfix(&q) < 0 {
		f = 1
		mpnegfix(&q)
	}

	p = len(buf)
	if flag&obj.FmtSharp != 0 /*untyped*/ {
		// Hexadecimal
		Mpmovecfix(&sixteen, 16)

		for {
			mpdivmodfixfix(&q, &r, &q, &sixteen)
			digit = int(Mpgetfix(&r))
			if digit < 10 {
				p--
				buf[p] = byte(digit + '0')
			} else {
				p--
				buf[p] = byte(digit - 10 + 'A')
			}
			if mptestfix(&q) <= 0 {
				break
			}
		}

		p--
		buf[p] = 'x'
		p--
		buf[p] = '0'
	} else {
		// Decimal
		Mpmovecfix(&ten, 10)

		for {
			mpdivmodfixfix(&q, &r, &q, &ten)
			p--
			buf[p] = byte(Mpgetfix(&r) + '0')
			if mptestfix(&q) <= 0 {
				break
			}
		}
	}

	if f != 0 {
		p--
		buf[p] = '-'
	}
	fp += string(buf[p:])
	return fp
}

func Fconv(fvp *Mpflt, flag int) string {
	var buf string
	var fp string

	var fv Mpflt
	var d float64
	var dexp float64
	var exp int

	if flag&obj.FmtSharp != 0 /*untyped*/ {
		// alternate form - decimal for error messages.
		// for well in range, convert to double and use print's %g
		exp = int(fvp.Exp) + sigfig(fvp)*Mpscale

		if -900 < exp && exp < 900 {
			d = mpgetflt(fvp)
			if d >= 0 && (flag&obj.FmtSign != 0 /*untyped*/) {
				fp += fmt.Sprintf("+")
			}
			fp += fmt.Sprintf("%.6g", d)
			return fp
		}

		// very out of range. compute decimal approximation by hand.
		// decimal exponent
		dexp = float64(fvp.Exp) * 0.301029995663981195 // log_10(2)
		exp = int(dexp)

		// decimal mantissa
		fv = *fvp

		fv.Val.Neg = 0
		fv.Exp = 0
		d = mpgetflt(&fv)
		d *= math.Pow(10, dexp-float64(exp))
		for d >= 9.99995 {
			d /= 10
			exp++
		}

		if fvp.Val.Neg != 0 {
			fp += fmt.Sprintf("-")
		} else if flag&obj.FmtSign != 0 /*untyped*/ {
			fp += fmt.Sprintf("+")
		}
		fp += fmt.Sprintf("%.5fe+%d", d, exp)
		return fp
	}

	if sigfig(fvp) == 0 {
		buf = fmt.Sprintf("0p+0")
		goto out
	}

	fv = *fvp

	for fv.Val.A[0] == 0 {
		Mpshiftfix(&fv.Val, -Mpscale)
		fv.Exp += Mpscale
	}

	for fv.Val.A[0]&1 == 0 {
		Mpshiftfix(&fv.Val, -1)
		fv.Exp += 1
	}

	if fv.Exp >= 0 {
		buf = fmt.Sprintf("%vp+%d", Bconv(&fv.Val, obj.FmtSharp), fv.Exp)
		goto out
	}

	buf = fmt.Sprintf("%vp-%d", Bconv(&fv.Val, obj.FmtSharp), -fv.Exp)

out:
	fp += buf
	return fp
}