mirror of
https://github.com/ddidderr/wuffs.git
synced 2026-11-19 08:38:14 +01:00
Add saturating versions of += and -=
This commit is contained in:
1 parent
a20726a944
commit
04b4923d1d
3 files changed
+47
-32
No files matched your search
@@ -464,19 +464,26 @@ func (g *gen) cName(name string) string {
|
||||
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) {
|
||||
if typ.Decorator() == 0 {
|
||||
if qid := typ.QID(); qid[0] == t.IDBase {
|
||||
switch qid[1] {
|
||||
case t.IDU8:
|
||||
return 1, nil
|
||||
case t.IDU16:
|
||||
return 2, nil
|
||||
case t.IDU32:
|
||||
return 4, nil
|
||||
case t.IDU64:
|
||||
return 8, nil
|
||||
}
|
||||
if n := uintBits(typ.QID()); n != 0 {
|
||||
return n / 8, nil
|
||||
}
|
||||
}
|
||||
return 0, fmt.Errorf("unknown sizeof for %q", typ.Str(g.tm))
|
||||
|
||||
@@ -478,27 +478,15 @@ func (g *gen) writeExprBinaryOp(b *buffer, n *a.Expr, rp replacementPolicy, pp p
|
||||
op := n.Operator()
|
||||
switch op {
|
||||
case t.IDXBinaryTildeSatPlus, t.IDXBinaryTildeSatMinus:
|
||||
uType := ""
|
||||
qid := n.MType().QID()
|
||||
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 == "" {
|
||||
uBits := uintBits(n.MType().QID())
|
||||
if uBits == 0 {
|
||||
return fmt.Errorf("unsupported tilde-operator type %q", n.MType().Str(g.tm))
|
||||
}
|
||||
|
||||
uOp := "add"
|
||||
if op != t.IDXBinaryTildeSatPlus {
|
||||
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
|
||||
|
||||
case t.IDXBinaryAs:
|
||||
|
||||
@@ -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 {
|
||||
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 {
|
||||
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)
|
||||
if err := g.writeExpr(b, n.RHS(), replaceCallSuspendibles, parenthesesMandatory, depth); err != nil {
|
||||
return err
|
||||
}
|
||||
if tilde {
|
||||
b.writeb(')')
|
||||
}
|
||||
b.writes(";\n")
|
||||
return nil
|
||||
|
||||
|
||||
Reference in new issue
Block a user