examples/complex: and bugfixes
This commit is contained in:
32
examples/complex/asm.go
Normal file
32
examples/complex/asm.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
. "github.com/mmcloughlin/avo/build"
|
||||
)
|
||||
|
||||
func main() {
|
||||
TEXT("Real", "func(x complex128) float64")
|
||||
r := Load(Param("x").Real(), Xv())
|
||||
Store(r, ReturnIndex(0))
|
||||
RET()
|
||||
|
||||
TEXT("Imag", "func(x complex128) float64")
|
||||
i := Load(Param("x").Imag(), Xv())
|
||||
Store(i, ReturnIndex(0))
|
||||
RET()
|
||||
|
||||
TEXT("Norm", "func(x complex128) float64")
|
||||
r = Load(Param("x").Real(), Xv())
|
||||
i = Load(Param("x").Imag(), Xv())
|
||||
MULSD(r, r)
|
||||
MULSD(i, i)
|
||||
ADDSD(i, r)
|
||||
n := Xv()
|
||||
SQRTSD(r, n)
|
||||
Store(n, ReturnIndex(0))
|
||||
RET()
|
||||
|
||||
Generate()
|
||||
}
|
||||
23
examples/complex/complex.s
Normal file
23
examples/complex/complex.s
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
#include "textflag.h"
|
||||
|
||||
// func Real(x complex128) float64
|
||||
TEXT ·Real(SB),0,$0-24
|
||||
MOVSD x_real(FP), X0
|
||||
MOVSD X0, ret+16(FP)
|
||||
RET
|
||||
// func Imag(x complex128) float64
|
||||
TEXT ·Imag(SB),0,$0-24
|
||||
MOVSD x_imag+8(FP), X0
|
||||
MOVSD X0, ret+16(FP)
|
||||
RET
|
||||
// func Norm(x complex128) float64
|
||||
TEXT ·Norm(SB),0,$0-24
|
||||
MOVSD x_real(FP), X0
|
||||
MOVSD x_imag+8(FP), X1
|
||||
MULSD X0, X0
|
||||
MULSD X1, X1
|
||||
ADDSD X1, X0
|
||||
SQRTSD X0, X2
|
||||
MOVSD X2, ret+16(FP)
|
||||
RET
|
||||
36
examples/complex/complex_test.go
Normal file
36
examples/complex/complex_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package complex
|
||||
|
||||
import (
|
||||
"math"
|
||||
"testing"
|
||||
"testing/quick"
|
||||
)
|
||||
|
||||
//go:generate go run asm.go -out complex.s -stubs stub.go
|
||||
|
||||
func TestReal(t *testing.T) {
|
||||
expect := func(x complex128) float64 {
|
||||
return real(x)
|
||||
}
|
||||
if err := quick.CheckEqual(Real, expect, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestImag(t *testing.T) {
|
||||
expect := func(x complex128) float64 {
|
||||
return imag(x)
|
||||
}
|
||||
if err := quick.CheckEqual(Imag, expect, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNorm(t *testing.T) {
|
||||
expect := func(x complex128) float64 {
|
||||
return math.Sqrt(real(x)*real(x) + imag(x)*imag(x))
|
||||
}
|
||||
if err := quick.CheckEqual(Norm, expect, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
5
examples/complex/stub.go
Normal file
5
examples/complex/stub.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package complex
|
||||
|
||||
func Real(x complex128) float64
|
||||
func Imag(x complex128) float64
|
||||
func Norm(x complex128) float64
|
||||
Reference in New Issue
Block a user