gofmt-ify math, expvar, exp/spacewar

R=rsc
http://go/go-review/1018061
This commit is contained in:
Robert Griesemer 2009-11-05 09:08:08 -08:00
parent e57acdca4d
commit 3630bfbe9f
6 changed files with 103 additions and 106 deletions

View File

@ -64,11 +64,11 @@ import (
type Word uint32 type Word uint32
const mask = 0777777; const mask = 0777777
const sign = 0400000; const sign = 0400000
const ( const (
_ = iota; // 00 _ = iota; // 00
opAND; opAND;
opIOR; opIOR;
opXOR; opXOR;
@ -109,17 +109,17 @@ const (
// The machine calls the Trap method to implement the // The machine calls the Trap method to implement the
// PDP-1 IOT instruction. // PDP-1 IOT instruction.
type Trapper interface { type Trapper interface {
Trap(y Word) Trap(y Word);
} }
// An M represents the machine state of a PDP-1. // An M represents the machine state of a PDP-1.
// Clients can set Display to install an output device. // Clients can set Display to install an output device.
type M struct { type M struct {
AC, IO, PC, OV Word; AC, IO, PC, OV Word;
Mem [010000]Word; Mem [010000]Word;
Flag [7]bool; Flag [7]bool;
Sense [7]bool; Sense [7]bool;
Halt bool; Halt bool;
} }
@ -142,8 +142,8 @@ func norm(i Word) Word {
} }
type UnknownInstrError struct { type UnknownInstrError struct {
Inst Word; Inst Word;
PC Word; PC Word;
} }
func (e UnknownInstrError) String() string { func (e UnknownInstrError) String() string {
@ -168,9 +168,9 @@ func (m *M) run(inst Word, t Trapper) os.Error {
if op < opSKP && op != opCALJDA { if op < opSKP && op != opCALJDA {
for n := 0; ib != 0; n++ { for n := 0; ib != 0; n++ {
if n > 07777 { if n > 07777 {
return LoopError(m.PC-1); return LoopError(m.PC - 1);
} }
ib = (m.Mem[y]>>12) & 1; ib = (m.Mem[y] >> 12)&1;
y = m.Mem[y] & 07777; y = m.Mem[y] & 07777;
} }
} }
@ -190,8 +190,8 @@ func (m *M) run(inst Word, t Trapper) os.Error {
a = 64; a = 64;
} }
m.Mem[a] = m.AC; m.Mem[a] = m.AC;
m.AC = (m.OV<<17) + m.PC; m.AC = (m.OV << 17) + m.PC;
m.PC = a + 1; m.PC = a+1;
case opLAC: case opLAC:
m.AC = m.Mem[y]; m.AC = m.Mem[y];
case opLIO: case opLIO:
@ -199,29 +199,29 @@ func (m *M) run(inst Word, t Trapper) os.Error {
case opDAC: case opDAC:
m.Mem[y] = m.AC; m.Mem[y] = m.AC;
case opDAP: case opDAP:
m.Mem[y] = m.Mem[y]&0770000 | m.AC&07777; m.Mem[y] = m.Mem[y] & 0770000 | m.AC & 07777;
case opDIO: case opDIO:
m.Mem[y] = m.IO; m.Mem[y] = m.IO;
case opDZM: case opDZM:
m.Mem[y] = 0; m.Mem[y] = 0;
case opADD: case opADD:
m.AC += m.Mem[y]; m.AC += m.Mem[y];
m.OV = m.AC>>18; m.OV = m.AC >> 18;
m.AC = norm(m.AC); m.AC = norm(m.AC);
case opSUB: case opSUB:
diffSigns := (m.AC ^ m.Mem[y])>>17 == 1; diffSigns := (m.AC ^ m.Mem[y])>>17 == 1;
m.AC += m.Mem[y]^mask; m.AC += m.Mem[y] ^ mask;
m.AC = norm(m.AC); m.AC = norm(m.AC);
if diffSigns && m.Mem[y]>>17 == m.AC>>17 { if diffSigns && m.Mem[y] >> 17 == m.AC >> 17 {
m.OV = 1; m.OV = 1;
} }
case opIDX: case opIDX:
m.AC = norm(m.Mem[y]+1); m.AC = norm(m.Mem[y] + 1);
m.Mem[y] = m.AC; m.Mem[y] = m.AC;
case opISP: case opISP:
m.AC = norm(m.Mem[y]+1); m.AC = norm(m.Mem[y] + 1);
m.Mem[y] = m.AC; m.Mem[y] = m.AC;
if m.AC&sign == 0 { if m.AC & sign == 0 {
m.PC++; m.PC++;
} }
case opSAD: case opSAD:
@ -233,17 +233,17 @@ func (m *M) run(inst Word, t Trapper) os.Error {
m.PC++; m.PC++;
} }
case opMUS: case opMUS:
if m.IO&1 == 1 { if m.IO & 1 == 1 {
m.AC += m.Mem[y]; m.AC += m.Mem[y];
m.AC = norm(m.AC) m.AC = norm(m.AC);
} }
m.IO = (m.IO>>1 | m.AC<<17) & mask; m.IO = (m.IO >> 1 | m.AC << 17)&mask;
m.AC >>= 1; m.AC >>= 1;
case opDIS: case opDIS:
m.AC, m.IO = (m.AC<<1 | m.IO>>17) & mask, m.AC, m.IO = (m.AC << 1 | m.IO >> 17)&mask,
((m.IO<<1 | m.AC>>17) & mask) ^ 1; ((m.IO << 1 | m.AC >> 17)&mask)^1;
if m.IO&1 == 1 { if m.IO & 1 == 1 {
m.AC = m.AC + (m.Mem[y]^mask); m.AC = m.AC + (m.Mem[y] ^ mask);
} else { } else {
m.AC = m.AC + 1 + m.Mem[y]; m.AC = m.AC + 1 + m.Mem[y];
} }
@ -251,18 +251,18 @@ func (m *M) run(inst Word, t Trapper) os.Error {
case opJMP: case opJMP:
m.PC = y; m.PC = y;
case opJSP: case opJSP:
m.AC = (m.OV<<17) + m.PC; m.AC = (m.OV << 17) + m.PC;
m.PC = y; m.PC = y;
case opSKP: case opSKP:
cond := y&0100 == 0100 && m.AC == 0 cond := y&0100 == 0100 && m.AC == 0 ||
|| y&0200 == 0200 && m.AC>>17 == 0 y&0200 == 0200 && m.AC >> 17 == 0 ||
|| y&0400 == 0400 && m.AC>>17 == 1 y&0400 == 0400 && m.AC >> 17 == 1 ||
|| y&01000 == 01000 && m.OV == 0 y&01000 == 01000 && m.OV == 0 ||
|| y&02000 == 02000 && m.IO>>17 == 0 y&02000 == 02000 && m.IO >> 17 == 0 ||
|| y&7 != 0 && !m.Flag[y&7] y&7 != 0 && !m.Flag[y&7] ||
|| y&070 != 0 && !m.Sense[(y&070)>>3] y&070 != 0 && !m.Sense[(y&070)>>3] ||
|| y&070 == 010; y&070 == 010;
if (ib==0) == cond { if (ib == 0) == cond {
m.PC++; m.PC++;
} }
if y&01000 == 01000 { if y&01000 == 01000 {
@ -275,41 +275,41 @@ func (m *M) run(inst Word, t Trapper) os.Error {
} }
switch (inst>>9)&017 { switch (inst>>9)&017 {
case 001: // rotate AC left case 001: // rotate AC left
m.AC = (m.AC<<1 | m.AC>>17) & mask; m.AC = (m.AC << 1 | m.AC >> 17)&mask;
case 002: // rotate IO left case 002: // rotate IO left
m.IO = (m.IO<<1 | m.IO>>17) & mask; m.IO = (m.IO << 1 | m.IO >> 17)&mask;
case 003: // rotate AC and IO left. case 003: // rotate AC and IO left.
w := uint64(m.AC)<<18 | uint64(m.IO); w := uint64(m.AC)<<18 | uint64(m.IO);
w = w<<1 | w>>35; w = w<<1 | w>>35;
m.AC = Word(w>>18) & mask; m.AC = Word(w>>18)&mask;
m.IO = Word(w) & mask; m.IO = Word(w)&mask;
case 005: // shift AC left (excluding sign bit) case 005: // shift AC left (excluding sign bit)
m.AC = (m.AC<<1 | m.AC>>17)&mask&^sign | m.AC&sign; m.AC = (m.AC << 1 | m.AC >> 17)&mask&^sign | m.AC & sign;
case 006: // shift IO left (excluding sign bit) case 006: // shift IO left (excluding sign bit)
m.IO = (m.IO<<1 | m.IO>>17)&mask&^sign | m.IO&sign; m.IO = (m.IO << 1 | m.IO >> 17)&mask&^sign | m.IO & sign;
case 007: // shift AC and IO left (excluding AC's sign bit) case 007: // shift AC and IO left (excluding AC's sign bit)
w := uint64(m.AC)<<18 | uint64(m.IO); w := uint64(m.AC)<<18 | uint64(m.IO);
w = w<<1 | w>>35; w = w<<1 | w>>35;
m.AC = Word(w>>18)&mask&^sign | m.AC&sign; m.AC = Word(w>>18)&mask&^sign | m.AC & sign;
m.IO = Word(w)&mask&^sign | m.AC&sign; m.IO = Word(w)&mask&^sign | m.AC & sign;
case 011: // rotate AC right case 011: // rotate AC right
m.AC = (m.AC>>1 | m.AC<<17) & mask; m.AC = (m.AC >> 1 | m.AC << 17)&mask;
case 012: // rotate IO right case 012: // rotate IO right
m.IO = (m.IO>>1 | m.IO<<17) & mask; m.IO = (m.IO >> 1 | m.IO << 17)&mask;
case 013: // rotate AC and IO right case 013: // rotate AC and IO right
w := uint64(m.AC)<<18 | uint64(m.IO); w := uint64(m.AC)<<18 | uint64(m.IO);
w = w>>1 | w<<35; w = w>>1 | w<<35;
m.AC = Word(w>>18) & mask; m.AC = Word(w>>18)&mask;
m.IO = Word(w) & mask; m.IO = Word(w)&mask;
case 015: // shift AC right (excluding sign bit) case 015: // shift AC right (excluding sign bit)
m.AC = m.AC>>1 | m.AC&sign; m.AC = m.AC >> 1 | m.AC & sign;
case 016: // shift IO right (excluding sign bit) case 016: // shift IO right (excluding sign bit)
m.IO = m.IO>>1 | m.IO&sign; m.IO = m.IO >> 1 | m.IO & sign;
case 017: // shift AC and IO right (excluding AC's sign bit) case 017: // shift AC and IO right (excluding AC's sign bit)
w := uint64(m.AC)<<18 | uint64(m.IO); w := uint64(m.AC)<<18 | uint64(m.IO);
w = w>>1; w = w>>1;
m.AC = Word(w>>18) | m.AC&sign; m.AC = Word(w>>18) | m.AC & sign;
m.IO = Word(w) & mask; m.IO = Word(w)&mask;
default: default:
goto Unknown; goto Unknown;
} }
@ -336,7 +336,7 @@ func (m *M) run(inst Word, t Trapper) os.Error {
m.PC--; m.PC--;
return HaltError(m.PC); return HaltError(m.PC);
} }
switch i, f := y&7, y&010==010; { switch i, f := y&7, y&010 == 010; {
case i == 7: case i == 7:
for i := 2; i < 7; i++ { for i := 2; i < 7; i++ {
m.Flag[i] = f; m.Flag[i] = f;
@ -346,7 +346,7 @@ func (m *M) run(inst Word, t Trapper) os.Error {
} }
default: default:
Unknown: Unknown:
return UnknownInstrError{inst, m.PC-1}; return UnknownInstrError{inst, m.PC - 1};
} }
return nil; return nil;
} }
@ -371,15 +371,15 @@ func (m *M) Load(r io.Reader) os.Error {
i := 1; i := 1;
a := Word(0); a := Word(0);
for ; i < len(line) && '0' <= line[i] && line[i] <= '7'; i++ { for ; i < len(line) && '0' <= line[i] && line[i] <= '7'; i++ {
a = a*8 + Word(line[i] - '0'); a = a*8 + Word(line[i]-'0');
} }
if i >= len(line) || line[i] != '\t' || i == 1{ if i >= len(line) || line[i] != '\t' || i == 1 {
continue; continue;
} }
v := Word(0); v := Word(0);
j := i; j := i;
for i++; i < len(line) && '0' <= line[i] && line[i] <= '7'; i++ { for i++; i < len(line) && '0' <= line[i] && line[i] <= '7'; i++ {
v = v*8 + Word(line[i] - '0'); v = v*8 + Word(line[i]-'0');
} }
if i == j { if i == j {
continue; continue;
@ -388,4 +388,3 @@ func (m *M) Load(r io.Reader) os.Error {
} }
return nil; return nil;
} }

View File

@ -75,23 +75,23 @@ func quitter(c <-chan bool) {
// instruction 02051. // instruction 02051.
type SpacewarPDP1 struct { type SpacewarPDP1 struct {
pdp1.M; pdp1.M;
nframe int; nframe int;
frameTime int64; frameTime int64;
ctxt draw.Context; ctxt draw.Context;
dx, dy int; dx, dy int;
screen draw.Image; screen draw.Image;
ctl pdp1.Word; ctl pdp1.Word;
kc <-chan int; kc <-chan int;
colorModel image.ColorModel; colorModel image.ColorModel;
cmap []image.Color; cmap []image.Color;
pix [][]uint8; pix [][]uint8;
} }
func min(a, b int) int { func min(a, b int) int {
if a < b { if a < b {
return a return a;
} }
return b return b;
} }
func (m *SpacewarPDP1) Init(ctxt draw.Context) { func (m *SpacewarPDP1) Init(ctxt draw.Context) {
@ -116,10 +116,10 @@ func (m *SpacewarPDP1) Init(ctxt draw.Context) {
} }
const ( const (
frameDelay = 56 * 1e6; // 56 ms frameDelay = 56*1e6; // 56 ms
) )
var ctlBits = [...]pdp1.Word { var ctlBits = [...]pdp1.Word{
'f': 0000001, 'f': 0000001,
'd': 0000002, 'd': 0000002,
'a': 0000004, 'a': 0000004,
@ -134,10 +134,10 @@ func (m *SpacewarPDP1) Step() os.Error {
if m.PC == 02051 { if m.PC == 02051 {
m.pollInput(); m.pollInput();
m.nframe++; m.nframe++;
if m.nframe&1 == 0 { if m.nframe & 1 == 0 {
m.flush(); m.flush();
t := time.Nanoseconds(); t := time.Nanoseconds();
if t >= m.frameTime + 3*frameDelay { if t >= m.frameTime + 3 * frameDelay {
m.frameTime = t; m.frameTime = t;
} else { } else {
m.frameTime += frameDelay; m.frameTime += frameDelay;
@ -154,10 +154,10 @@ func (m *SpacewarPDP1) Step() os.Error {
func (m *SpacewarPDP1) Trap(y pdp1.Word) { func (m *SpacewarPDP1) Trap(y pdp1.Word) {
switch y&077 { switch y&077 {
case 7: case 7:
x := int(m.AC+0400000) & 0777777; x := int(m.AC + 0400000)&0777777;
y := int(m.IO+0400000) & 0777777; y := int(m.IO + 0400000)&0777777;
x = x*m.dx / 0777777; x = x * m.dx / 0777777;
y = y*m.dy / 0777777; y = y * m.dy / 0777777;
if 0 <= x && x < m.dx && 0 <= y && y < m.dy { if 0 <= x && x < m.dx && 0 <= y && y < m.dy {
n := uint8(min(int(m.pix[y][x])+128, 255)); n := uint8(min(int(m.pix[y][x])+128, 255));
m.pix[y][x] = n; m.pix[y][x] = n;

View File

@ -12,16 +12,16 @@ import (
func TestInt(t *testing.T) { func TestInt(t *testing.T) {
reqs := NewInt("requests"); reqs := NewInt("requests");
if reqs.i != 0 { if reqs.i != 0 {
t.Errorf("reqs.i = %v, want 4", reqs.i) t.Errorf("reqs.i = %v, want 4", reqs.i);
} }
if reqs != Get("requests").(*Int) { if reqs != Get("requests").(*Int) {
t.Errorf("Get() failed.") t.Errorf("Get() failed.");
} }
reqs.Add(1); reqs.Add(1);
reqs.Add(3); reqs.Add(3);
if reqs.i != 4 { if reqs.i != 4 {
t.Errorf("reqs.i = %v, want 4", reqs.i) t.Errorf("reqs.i = %v, want 4", reqs.i);
} }
if s := reqs.String(); s != "4" { if s := reqs.String(); s != "4" {
@ -32,12 +32,12 @@ func TestInt(t *testing.T) {
func TestString(t *testing.T) { func TestString(t *testing.T) {
name := NewString("my-name"); name := NewString("my-name");
if name.s != "" { if name.s != "" {
t.Errorf("name.s = %q, want \"\"", name.s) t.Errorf("name.s = %q, want \"\"", name.s);
} }
name.Set("Mike"); name.Set("Mike");
if name.s != "Mike" { if name.s != "Mike" {
t.Errorf("name.s = %q, want \"Mike\"", name.s) t.Errorf("name.s = %q, want \"Mike\"", name.s);
} }
if s := name.String(); s != "\"Mike\"" { if s := name.String(); s != "\"Mike\"" {
@ -52,10 +52,10 @@ func TestMapCounter(t *testing.T) {
colours.Add("red", 2); colours.Add("red", 2);
colours.Add("blue", 4); colours.Add("blue", 4);
if x := colours.m["red"].(*Int).i; x != 3 { if x := colours.m["red"].(*Int).i; x != 3 {
t.Errorf("colours.m[\"red\"] = %v, want 3", x) t.Errorf("colours.m[\"red\"] = %v, want 3", x);
} }
if x := colours.m["blue"].(*Int).i; x != 4 { if x := colours.m["blue"].(*Int).i; x != 4 {
t.Errorf("colours.m[\"blue\"] = %v, want 4", x) t.Errorf("colours.m[\"blue\"] = %v, want 4", x);
} }
// colours.String() should be '{"red":3, "blue":4}', // colours.String() should be '{"red":3, "blue":4}',
@ -63,17 +63,17 @@ func TestMapCounter(t *testing.T) {
s := colours.String(); s := colours.String();
j, ok, errtok := json.StringToJson(s); j, ok, errtok := json.StringToJson(s);
if !ok { if !ok {
t.Errorf("colours.String() isn't valid JSON: %v", errtok) t.Errorf("colours.String() isn't valid JSON: %v", errtok);
} }
if j.Kind() != json.MapKind { if j.Kind() != json.MapKind {
t.Error("colours.String() didn't produce a map.") t.Error("colours.String() didn't produce a map.");
} }
red := j.Get("red"); red := j.Get("red");
if red.Kind() != json.NumberKind { if red.Kind() != json.NumberKind {
t.Error("red.Kind() is not a NumberKind.") t.Error("red.Kind() is not a NumberKind.");
} }
if x := red.Number(); x != 3 { if x := red.Number(); x != 3 {
t.Error("red = %v, want 3", x) t.Error("red = %v, want 3", x);
} }
} }

View File

@ -5,8 +5,8 @@
package math_test package math_test
import ( import (
. "math"; . "math";
"testing"; "testing";
) )
var vf = []float64{ var vf = []float64{

View File

@ -5,12 +5,12 @@
package math package math
const ( const (
uvnan = 0x7FF0000000000001; uvnan = 0x7FF0000000000001;
uvinf = 0x7FF0000000000000; uvinf = 0x7FF0000000000000;
uvneginf = 0xFFF0000000000000; uvneginf = 0xFFF0000000000000;
mask = 0x7FF; mask = 0x7FF;
shift = 64 - 11 - 1; shift = 64-11-1;
bias = 1022; bias = 1022;
) )
// Inf returns positive infinity if sign >= 0, negative infinity if sign < 0. // Inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
@ -32,7 +32,7 @@ func NaN() float64 {
// IsNaN returns whether f is an IEEE 754 ``not-a-number'' value. // IsNaN returns whether f is an IEEE 754 ``not-a-number'' value.
func IsNaN(f float64) (is bool) { func IsNaN(f float64) (is bool) {
x := Float64bits(f); x := Float64bits(f);
return uint32(x>>shift) & mask == mask && x != uvinf && x != uvneginf; return uint32(x>>shift)&mask == mask && x != uvinf && x != uvneginf;
} }
// IsInf returns whether f is an infinity, according to sign. // IsInf returns whether f is an infinity, according to sign.
@ -53,7 +53,7 @@ func Frexp(f float64) (frac float64, exp int) {
return; return;
} }
x := Float64bits(f); x := Float64bits(f);
exp = int((x>>shift)&mask) - bias; exp = int((x>>shift)&mask)-bias;
x &^= mask<<shift; x &^= mask<<shift;
x |= bias<<shift; x |= bias<<shift;
frac = Float64frombits(x); frac = Float64frombits(x);
@ -64,7 +64,7 @@ func Frexp(f float64) (frac float64, exp int) {
// It returns frac × 2<sup>exp</sup>. // It returns frac × 2<sup>exp</sup>.
func Ldexp(frac float64, exp int) float64 { func Ldexp(frac float64, exp int) float64 {
x := Float64bits(frac); x := Float64bits(frac);
exp += int(x>>shift) & mask; exp += int(x>>shift)&mask;
if exp <= 0 { if exp <= 0 {
return 0; // underflow return 0; // underflow
} }
@ -99,7 +99,6 @@ func Modf(f float64) (int float64, frac float64) {
x &^= 1<<(64-11-e) - 1; x &^= 1<<(64-11-e) - 1;
} }
int = Float64frombits(x); int = Float64frombits(x);
frac = f - int; frac = f-int;
return; return;
} }

View File

@ -27,4 +27,3 @@ func Float64bits(f float64) uint64 {
func Float64frombits(b uint64) float64 { func Float64frombits(b uint64) float64 {
return *(*float64)(unsafe.Pointer(&b)); return *(*float64)(unsafe.Pointer(&b));
} }