2015-02-13 14:40:36 -05:00
|
|
|
// 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 (
|
|
|
|
|
"fmt"
|
|
|
|
|
"math"
|
|
|
|
|
)
|
|
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
func newMpflt() *Mpflt {
|
|
|
|
|
var a Mpflt
|
|
|
|
|
a.Val.SetPrec(Mpscale * Mpprec)
|
|
|
|
|
return &a
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// implements float arihmetic
|
|
|
|
|
|
|
|
|
|
func mpaddfltflt(a *Mpflt, b *Mpflt) {
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf("\n%v + %v", Fconv(a, 0), Fconv(b, 0))
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
a.Val.Add(&a.Val, &b.Val)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf(" = %v\n\n", Fconv(a, 0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mpmulfltflt(a *Mpflt, b *Mpflt) {
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf("%v\n * %v\n", Fconv(a, 0), Fconv(b, 0))
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
a.Val.Mul(&a.Val, &b.Val)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf(" = %v\n\n", Fconv(a, 0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mpdivfltflt(a *Mpflt, b *Mpflt) {
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf("%v\n / %v\n", Fconv(a, 0), Fconv(b, 0))
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
a.Val.Quo(&a.Val, &b.Val)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf(" = %v\n\n", Fconv(a, 0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mpgetfltN(a *Mpflt, prec int, bias int) float64 {
|
2015-03-20 16:59:08 -07:00
|
|
|
var x float64
|
|
|
|
|
switch prec {
|
|
|
|
|
case 53:
|
|
|
|
|
x, _ = a.Val.Float64()
|
|
|
|
|
case 24:
|
|
|
|
|
// We should be using a.Val.Float32() here but that seems incorrect
|
|
|
|
|
// for certain denormal values (all.bash fails). The current code
|
|
|
|
|
// appears to work for all existing test cases, though there ought
|
|
|
|
|
// to be issues with denormal numbers that are incorrectly rounded.
|
|
|
|
|
// TODO(gri) replace with a.Val.Float32() once correctly working
|
|
|
|
|
// See also: https://github.com/golang/go/issues/10321
|
|
|
|
|
var t Mpflt
|
|
|
|
|
t.Val.SetPrec(24).Set(&a.Val)
|
|
|
|
|
x, _ = t.Val.Float64()
|
|
|
|
|
default:
|
|
|
|
|
panic("unreachable")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check for overflow
|
|
|
|
|
if math.IsInf(x, 0) && nsavederrors+nerrors == 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
Yyerror("mpgetflt ovf")
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
return x
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mpgetflt(a *Mpflt) float64 {
|
|
|
|
|
return mpgetfltN(a, 53, -1023)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mpgetflt32(a *Mpflt) float64 {
|
|
|
|
|
return mpgetfltN(a, 24, -127)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Mpmovecflt(a *Mpflt, c float64) {
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf("\nconst %g", c)
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
a.Val.SetFloat64(c)
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2015-03-20 16:59:08 -07:00
|
|
|
if Mpdebug != 0 {
|
2015-02-13 14:40:36 -05:00
|
|
|
fmt.Printf(" = %v\n", Fconv(a, 0))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func mpnegflt(a *Mpflt) {
|
2015-03-20 16:59:08 -07:00
|
|
|
a.Val.Neg(&a.Val)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|