doc: README for complex example

Updates #14
This commit is contained in:
Michael McLoughlin
2019-01-01 15:56:17 -08:00
parent 1b0ce66552
commit 8465c21c02
2 changed files with 37 additions and 0 deletions

View File

@@ -0,0 +1,36 @@
# complex
Demonstrates how to access complex types in `avo`.
The `Real()` and `Imag()` parameter methods may be used to load the sub-components of complex arguments. The following function uses these to implement the [complex norm](http://mathworld.wolfram.com/ComplexModulus.html).
[embedmd]:# (asm.go go /.*TEXT.*Norm/ /RET.*/)
```go
TEXT("Norm", "func(z complex128) float64")
Doc("Norm returns the complex norm of z.")
r = Load(Param("z").Real(), Xv())
i = Load(Param("z").Imag(), Xv())
MULSD(r, r)
MULSD(i, i)
ADDSD(i, r)
n := Xv()
SQRTSD(r, n)
Store(n, ReturnIndex(0))
RET()
```
Generated assembly:
[embedmd]:# (complex.s s /.*func Norm/ /RET/)
```s
// func Norm(z complex128) float64
TEXT ·Norm(SB), $0-24
MOVSD z_real(FP), X0
MOVSD z_imag+8(FP), X1
MULSD X0, X0
MULSD X1, X1
ADDSD X1, X0
SQRTSD X0, X2
MOVSD X2, ret+16(FP)
RET
```