Skip to content

Commit

Permalink
cmd/addchain: search options to modify costs (#120)
Browse files Browse the repository at this point in the history
Doubling (squaring) is typically cheaper than addition (multiplication). This
PR adds options to the search subcommand to score addition chains based on a
weighted sum.

Closes #55
Updates #117
  • Loading branch information
mmcloughlin committed Oct 9, 2021
1 parent b4b73a6 commit 881e229
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ addchain: expr: "2^255 - 19 - 2"
addchain: hex: 7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeb
addchain: dec: 57896044618658097711785492504343953926634992332820282019728792003956564819947
addchain: best: opt(runs(continued_fractions(dichotomic)))
addchain: cost: 266
_10 = 2*1
_11 = 1 + _10
_1100 = _11 << 2
Expand Down
15 changes: 12 additions & 3 deletions cmd/addchain/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
"math"
"runtime"

"github.com/google/subcommands"
Expand All @@ -20,14 +21,16 @@ import (
type search struct {
cli.Command

add float64
double float64
concurrency int
verbose bool
}

func (*search) Name() string { return "search" }
func (*search) Synopsis() string { return "search for an addition chain." }
func (*search) Usage() string {
return `Usage: search [-v] [-p <N>] <expr>
return `Usage: search [-v] [-p <N>] [-add <cost>] [-double <cost>] <expr>
Search for an addition chain for <expr>.
Expand All @@ -37,6 +40,8 @@ Search for an addition chain for <expr>.
func (cmd *search) SetFlags(f *flag.FlagSet) {
f.IntVar(&cmd.concurrency, "p", runtime.NumCPU(), "run `N` algorithms in parallel")
f.BoolVar(&cmd.verbose, "v", false, "verbose output")
f.Float64Var(&cmd.add, "add", 1, "add `cost`")
f.Float64Var(&cmd.double, "double", 1, "double `cost`")
}

func (cmd *search) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) (status subcommands.ExitStatus) {
Expand Down Expand Up @@ -76,15 +81,18 @@ func (cmd *search) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})

// Report results.
best := 0
mincost := math.Inf(+1)
for i, r := range rs {
cmd.Debugf("algorithm: %s", r.Algorithm)
if r.Err != nil {
return cmd.Fail("algorithm error: %v", r.Err)
}
doubles, adds := r.Program.Count()
cmd.Debugf("total: %d\tdoubles: \t%d adds: %d", doubles+adds, doubles, adds)
if len(r.Program) < len(rs[best].Program) {
cost := cmd.double*float64(doubles) + cmd.add*float64(adds)
cmd.Debugf("cost: %v\tdoubles: \t%d adds: %d", cost, doubles, adds)
if cost < mincost {
best = i
mincost = cost
}
}

Expand All @@ -94,6 +102,7 @@ func (cmd *search) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{})
cmd.Debugf("[%3d] %3d+%3d\t%x", n+1, op.I, op.J, b.Chain[n+1])
}
cmd.Log.Printf("best: %s", b.Algorithm)
cmd.Log.Printf("cost: %v", mincost)

// Produce a program for it.
p, err := acc.Decompile(b.Program)
Expand Down
1 change: 1 addition & 0 deletions internal/examples/cli/output
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ addchain: expr: "2^255 - 19 - 2"
addchain: hex: 7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffeb
addchain: dec: 57896044618658097711785492504343953926634992332820282019728792003956564819947
addchain: best: opt(runs(continued_fractions(dichotomic)))
addchain: cost: 266
_10 = 2*1
_11 = 1 + _10
_1100 = _11 << 2
Expand Down

0 comments on commit 881e229

Please sign in to comment.