Add saturating versions of += and -=

This commit is contained in:
Nigel Tao committed 2018-04-26 13:42:52 +10:00
1 parent a20726a944
commit 04b4923d1d
3 files changed
+47 -32

No files matched your search

+18 -11
View File
@@ -464,19 +464,26 @@ func (g *gen) cName(name string) string {
return string(s) return string(s)
} }
func uintBits(qid t.QID) uint32 {
if qid[0] == t.IDBase {
switch qid[1] {
case t.IDU8:
return 8
case t.IDU16:
return 16
case t.IDU32:
return 32
case t.IDU64:
return 64
}
}
return 0
}
func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) { func (g *gen) sizeof(typ *a.TypeExpr) (uint32, error) {
if typ.Decorator() == 0 { if typ.Decorator() == 0 {
if qid := typ.QID(); qid[0] == t.IDBase { if n := uintBits(typ.QID()); n != 0 {
switch qid[1] { return n / 8, nil
case t.IDU8:
return 1, nil
case t.IDU16:
return 2, nil
case t.IDU32:
return 4, nil
case t.IDU64:
return 8, nil
}
} }
} }
return 0, fmt.Errorf("unknown sizeof for %q", typ.Str(g.tm)) return 0, fmt.Errorf("unknown sizeof for %q", typ.Str(g.tm))
+3 -15
View File
@@ -478,27 +478,15 @@ func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp p
op := n.Operator() op := n.Operator()
switch op { switch op {
case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus: case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus:
uType := "" uBits := uintBits(n.MType().QID())
qid := n.MType().QID() if uBits == 0 {
switch qid[1] {
case t.IDU8:
uType = "8"
case t.IDU16:
uType = "16"
case t.IDU32:
uType = "32"
case t.IDU64:
uType = "64"
}
if qid[0] != t.IDBase || uType == "" {
return fmt.Errorf("unsupported tilde-operator type %q", n.MType().Str(g.tm)) return fmt.Errorf("unsupported tilde-operator type %q", n.MType().Str(g.tm))
} }
uOp := "add" uOp := "add"
if op != t.IDXBinaryTildeSatPlus { if op != t.IDXBinaryTildeSatPlus {
uOp = "sub" uOp = "sub"
} }
b.printf("wuffs_base__u%s__sat_%s", uType, uOp) b.printf("wuffs_base__u%d__sat_%s", uBits, uOp)
opName, tilde = ",", true opName, tilde = ",", true
case t.IDXBinaryAs: case t.IDXBinaryAs:
+26 -6
View File
@@ -78,19 +78,39 @@ func (g *gen) writeStatement(b *buffer, n *a.Node, depth uint32) error {
if err := g.writeSuspendibles(b, n.RHS(), depth); err != nil { if err := g.writeSuspendibles(b, n.RHS(), depth); err != nil {
return err return err
} }
opName, tilde := "", false
op := n.Operator()
switch op {
case t.IDTildeSatPlusEq, t.IDTildeSatMinusEq:
uBits := uintBits(n.LHS().MType().QID())
if uBits == 0 {
return fmt.Errorf("unsupported tilde-operator type %q", n.LHS().MType().Str(g.tm))
}
uOp := "add"
if op != t.IDTildeSatPlusEq {
uOp = "sub"
}
b.printf("wuffs_base__u%d__sat_%s_indirect(&", uBits, uOp)
opName, tilde = ",", true
default:
opName = cOpNames[0xFF&op]
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
}
}
if err := g.writeExpr(b, n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { if err := g.writeExpr(b, n.LHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err return err
} }
// TODO: `~sat+=` and `~sat-=` need special consideration.
op := n.Operator()
opName := cOpNames[0xFF&op]
if opName == "" {
return fmt.Errorf("unrecognized operator %q", op.AmbiguousForm().Str(g.tm))
}
b.writes(opName) b.writes(opName)
if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil { if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
return err return err
} }
if tilde {
b.writeb(')')
}
b.writes(";\n") b.writes(";\n")
return nil return nil