examples: add most docstrings

This commit is contained in:
Michael McLoughlin
2018-12-27 23:09:44 -08:00
parent 9f5277bf8e
commit 2ffc7d7fd5
18 changed files with 43 additions and 23 deletions

View File

@@ -8,6 +8,7 @@ import (
func main() { func main() {
TEXT("Add", "func(x, y uint64) uint64") TEXT("Add", "func(x, y uint64) uint64")
Doc("Add adds x and y.")
x := Load(Param("x"), GP64v()) x := Load(Param("x"), GP64v())
y := Load(Param("y"), GP64v()) y := Load(Param("y"), GP64v())
ADDQ(x, y) ADDQ(x, y)

View File

@@ -2,4 +2,5 @@
package add package add
// Add adds x and y.
func Add(x uint64, y uint64) uint64 func Add(x uint64, y uint64) uint64

View File

@@ -7,19 +7,22 @@ import (
) )
func main() { func main() {
TEXT("Real", "func(x complex128) float64") TEXT("Real", "func(z complex128) float64")
r := Load(Param("x").Real(), Xv()) Doc("Real returns the real part of z.")
r := Load(Param("z").Real(), Xv())
Store(r, ReturnIndex(0)) Store(r, ReturnIndex(0))
RET() RET()
TEXT("Imag", "func(x complex128) float64") TEXT("Imag", "func(z complex128) float64")
i := Load(Param("x").Imag(), Xv()) Doc("Imag returns the imaginary part of z.")
i := Load(Param("z").Imag(), Xv())
Store(i, ReturnIndex(0)) Store(i, ReturnIndex(0))
RET() RET()
TEXT("Norm", "func(x complex128) float64") TEXT("Norm", "func(z complex128) float64")
r = Load(Param("x").Real(), Xv()) Doc("Norm returns the complex norm of z.")
i = Load(Param("x").Imag(), Xv()) r = Load(Param("z").Real(), Xv())
i = Load(Param("z").Imag(), Xv())
MULSD(r, r) MULSD(r, r)
MULSD(i, i) MULSD(i, i)
ADDSD(i, r) ADDSD(i, r)

View File

@@ -2,22 +2,22 @@
#include "textflag.h" #include "textflag.h"
// func Real(x complex128) float64 // func Real(z complex128) float64
TEXT ·Real(SB), 0, $0-24 TEXT ·Real(SB), 0, $0-24
MOVSD x_real(FP), X0 MOVSD z_real(FP), X0
MOVSD X0, ret+16(FP) MOVSD X0, ret+16(FP)
RET RET
// func Imag(x complex128) float64 // func Imag(z complex128) float64
TEXT ·Imag(SB), 0, $0-24 TEXT ·Imag(SB), 0, $0-24
MOVSD x_imag+8(FP), X0 MOVSD z_imag+8(FP), X0
MOVSD X0, ret+16(FP) MOVSD X0, ret+16(FP)
RET RET
// func Norm(x complex128) float64 // func Norm(z complex128) float64
TEXT ·Norm(SB), 0, $0-24 TEXT ·Norm(SB), 0, $0-24
MOVSD x_real(FP), X0 MOVSD z_real(FP), X0
MOVSD x_imag+8(FP), X1 MOVSD z_imag+8(FP), X1
MULSD X0, X0 MULSD X0, X0
MULSD X1, X1 MULSD X1, X1
ADDSD X1, X0 ADDSD X1, X0

View File

@@ -9,8 +9,8 @@ import (
//go:generate go run asm.go -out complex.s -stubs stub.go //go:generate go run asm.go -out complex.s -stubs stub.go
func TestReal(t *testing.T) { func TestReal(t *testing.T) {
expect := func(x complex128) float64 { expect := func(z complex128) float64 {
return real(x) return real(z)
} }
if err := quick.CheckEqual(Real, expect, nil); err != nil { if err := quick.CheckEqual(Real, expect, nil); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -18,8 +18,8 @@ func TestReal(t *testing.T) {
} }
func TestImag(t *testing.T) { func TestImag(t *testing.T) {
expect := func(x complex128) float64 { expect := func(z complex128) float64 {
return imag(x) return imag(z)
} }
if err := quick.CheckEqual(Imag, expect, nil); err != nil { if err := quick.CheckEqual(Imag, expect, nil); err != nil {
t.Fatal(err) t.Fatal(err)
@@ -27,8 +27,8 @@ func TestImag(t *testing.T) {
} }
func TestNorm(t *testing.T) { func TestNorm(t *testing.T) {
expect := func(x complex128) float64 { expect := func(z complex128) float64 {
return math.Sqrt(real(x)*real(x) + imag(x)*imag(x)) return math.Sqrt(real(z)*real(z) + imag(z)*imag(z))
} }
if err := quick.CheckEqual(Norm, expect, nil); err != nil { if err := quick.CheckEqual(Norm, expect, nil); err != nil {
t.Fatal(err) t.Fatal(err)

View File

@@ -2,8 +2,11 @@
package complex package complex
func Real(x complex128) float64 // Real returns the real part of z.
func Real(z complex128) float64
func Imag(x complex128) float64 // Imag returns the imaginary part of z.
func Imag(z complex128) float64
func Norm(x complex128) float64 // Norm returns the complex norm of z.
func Norm(z complex128) float64

View File

@@ -21,6 +21,7 @@ func main() {
DATA(39, U8(0x77)) DATA(39, U8(0x77))
TEXT("DataAt", "func(i int) byte") TEXT("DataAt", "func(i int) byte")
Doc("DataAt returns byte i in the 'bytes' global data section.")
i := Load(Param("i"), GP64v()) i := Load(Param("i"), GP64v())
ptr := Mem{Base: GP64v()} ptr := Mem{Base: GP64v()}
LEAQ(bytes, ptr.Base) LEAQ(bytes, ptr.Base)

View File

@@ -2,4 +2,5 @@
package data package data
// DataAt returns byte i in the 'bytes' global data section.
func DataAt(i int) byte func DataAt(i int) byte

View File

@@ -15,6 +15,7 @@ const (
func main() { func main() {
TEXT("Hash64", "func(data []byte) uint64") TEXT("Hash64", "func(data []byte) uint64")
Doc("Hash64 computes the FNV-1a hash of data.")
ptr := Load(Param("data").Base(), GP64v()) ptr := Load(Param("data").Base(), GP64v())
n := Load(Param("data").Len(), GP64v()) n := Load(Param("data").Len(), GP64v())

View File

@@ -2,4 +2,5 @@
package fnv1a package fnv1a
// Hash64 computes the FNV-1a hash of data.
func Hash64(data []byte) uint64 func Hash64(data []byte) uint64

View File

@@ -9,6 +9,7 @@ import (
func main() { func main() {
TEXT("EncodeInt", "func(lat, lng float64) uint64") TEXT("EncodeInt", "func(lat, lng float64) uint64")
Doc("EncodeInt computes the 64-bit integer geohash of (lat, lng).")
lat := Load(Param("lat"), Xv()) lat := Load(Param("lat"), Xv())
lng := Load(Param("lng"), Xv()) lng := Load(Param("lng"), Xv())

View File

@@ -2,4 +2,5 @@
package geohash package geohash
// EncodeInt computes the 64-bit integer geohash of (lat, lng).
func EncodeInt(lat float64, lng float64) uint64 func EncodeInt(lat float64, lng float64) uint64

View File

@@ -10,6 +10,7 @@ import (
func main() { func main() {
TEXT("block", "func(h *[5]uint32, m []byte)") TEXT("block", "func(h *[5]uint32, m []byte)")
Doc("block SHA-1 hashes the 64-byte message m into the running state h.")
h := Mem{Base: Load(Param("h"), GP64v())} h := Mem{Base: Load(Param("h"), GP64v())}
m := Mem{Base: Load(Param("m").Base(), GP64v())} m := Mem{Base: Load(Param("m").Base(), GP64v())}

View File

@@ -2,4 +2,5 @@
package sha1 package sha1
// block SHA-1 hashes the 64-byte message m into the running state h.
func block(h *[5]uint32, m []byte) func block(h *[5]uint32, m []byte)

View File

@@ -39,6 +39,7 @@ func makelabels(name string, n int) []string {
func main() { func main() {
Package("github.com/mmcloughlin/avo/examples/stadtx") Package("github.com/mmcloughlin/avo/examples/stadtx")
TEXT("Hash", "func(state *State, key []byte) uint64") TEXT("Hash", "func(state *State, key []byte) uint64")
Doc("Hash computes the Stadtx hash.")
statePtr := Load(Param("state"), GP64v()) statePtr := Load(Param("state"), GP64v())
ptr := Load(Param("key").Base(), GP64v()) ptr := Load(Param("key").Base(), GP64v())

View File

@@ -2,4 +2,5 @@
package stadtx package stadtx
// Hash computes the Stadtx hash.
func Hash(state *State, key []byte) uint64 func Hash(state *State, key []byte) uint64

View File

@@ -9,6 +9,7 @@ import (
func main() { func main() {
TEXT("Sum", "func(xs []uint64) uint64") TEXT("Sum", "func(xs []uint64) uint64")
Doc("Sum returns the sum of the elements in xs.")
ptr := Load(Param("xs").Base(), GP64v()) ptr := Load(Param("xs").Base(), GP64v())
n := Load(Param("xs").Len(), GP64v()) n := Load(Param("xs").Len(), GP64v())
s := GP64v() s := GP64v()

View File

@@ -2,4 +2,5 @@
package sum package sum
// Sum returns the sum of the elements in xs.
func Sum(xs []uint64) uint64 func Sum(xs []uint64) uint64