| 1 | # complex |
| 2 | |
| 3 | Demonstrates how to access complex types in `avo`. |
| 4 | |
| 5 | 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). |
| 6 | |
| 7 | [embedmd]:# (asm.go go /.*TEXT.*Norm/ /RET.*/) |
| 8 | ```go |
| 9 | TEXT("Norm", NOSPLIT, "func(z complex128) float64") |
| 10 | Doc("Norm returns the complex norm of z.") |
| 11 | r = Load(Param("z").Real(), XMM()) |
| 12 | i = Load(Param("z").Imag(), XMM()) |
| 13 | MULSD(r, r) |
| 14 | MULSD(i, i) |
| 15 | ADDSD(i, r) |
| 16 | n := XMM() |
| 17 | SQRTSD(r, n) |
| 18 | Store(n, ReturnIndex(0)) |
| 19 | RET() |
| 20 | ``` |
| 21 | |
| 22 | Generated assembly: |
| 23 | |
| 24 | [embedmd]:# (complex.s s /.*func Norm/ /RET/) |
| 25 | ```s |
| 26 | // func Norm(z complex128) float64 |
| 27 | // Requires: SSE2 |
| 28 | TEXT ·Norm(SB), NOSPLIT, $0-24 |
| 29 | MOVSD z_real+0(FP), X0 |
| 30 | MOVSD z_imag+8(FP), X1 |
| 31 | MULSD X0, X0 |
| 32 | MULSD X1, X1 |
| 33 | ADDSD X1, X0 |
| 34 | SQRTSD X0, X2 |
| 35 | MOVSD X2, ret+16(FP) |
| 36 | RET |
| 37 | ``` |