Add max and min methods

This commit is contained in:
Nigel Tao committed 2018-05-12 20:08:52 +10:00
1 parent dfc8e14f4f
commit a6bf59006f
4 files changed
+73 -3

No files matched your search

+38
View File
@@ -120,6 +120,44 @@ func (g *gen) writeExprOther(b *buffer, n *a.Expr, rp replacementPolicy, pp pare
b.writes(")))")
return nil
}
if isThatMethod(g.tm, n, t.IDMin, 1) {
x := n.LHS().Expr().LHS().Expr()
b.writes("wuffs_base__u")
if sz, err := g.sizeof(x.MType()); err != nil {
return err
} else {
b.printf("%d", 8*sz)
}
b.writes("__min(")
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(",")
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(")")
return nil
}
if isThatMethod(g.tm, n, t.IDMax, 1) {
x := n.LHS().Expr().LHS().Expr()
b.writes("wuffs_base__u")
if sz, err := g.sizeof(x.MType()); err != nil {
return err
} else {
b.printf("%d", 8*sz)
}
b.writes("__max(")
if err := g.writeExpr(b, x, rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(",")
if err := g.writeExpr(b, n.Args()[0].Arg().Value(), rp, parenthesesOptional, depth); err != nil {
return err
}
b.writes(")")
return nil
}
if isThatMethod(g.tm, n, t.IDIsError, 0) || isThatMethod(g.tm, n, t.IDIsOK, 0) ||
isThatMethod(g.tm, n, t.IDIsSuspension, 0) {
if pp == parenthesesMandatory {
+11
View File
@@ -81,12 +81,23 @@ var Types = []string{
var Funcs = []string{
"u8.high_bits(n u32[..8])(ret u8)",
"u8.low_bits(n u32[..8])(ret u8)",
"u8.max(x u8)(ret u8)",
"u8.min(x u8)(ret u8)",
"u16.high_bits(n u32[..16])(ret u16)",
"u16.low_bits(n u32[..16])(ret u16)",
"u16.max(x u16)(ret u16)",
"u16.min(x u16)(ret u16)",
"u32.high_bits(n u32[..32])(ret u32)",
"u32.low_bits(n u32[..32])(ret u32)",
"u32.max(x u32)(ret u32)",
"u32.min(x u32)(ret u32)",
"u64.high_bits(n u32[..64])(ret u64)",
"u64.low_bits(n u32[..64])(ret u64)",
"u64.max(x u64)(ret u64)",
"u64.min(x u64)(ret u64)",
// ---- status
+20 -3
View File
@@ -820,17 +820,34 @@ func (q *checker) bcheckExprOther(n *a.Expr, depth uint32) (*big.Int, *big.Int,
return nil, nil, err
}
// Special case for a numeric type's low_bits and high_bits methods.
// Special case for a numeric type's low_bits, high_bits, etc. methods.
// The bound on the output is dependent on bound on the input, similar
// to dependent types, and isn't expressible in Wuffs' function syntax
// and type system.
if methodName := lhs.Ident(); methodName == t.IDLowBits || methodName == t.IDHighBits {
if recv := lhs.MType().Receiver(); recv != nil && recv.IsNumType() {
if recv := lhs.MType().Receiver(); recv != nil && recv.IsNumType() {
switch methodName := lhs.Ident(); methodName {
case t.IDLowBits, t.IDHighBits:
_, aMax, err := q.bcheckExpr(n.Args()[0].Arg().Value(), depth)
if err != nil {
return nil, nil, err
}
return zero, bitMask(int(aMax.Int64())), nil
case t.IDMin, t.IDMax:
// TODO: lhs has already been bcheck'ed. There should be no
// need to bcheck lhs.LHS().Expr() twice.
lMin, lMax, err := q.bcheckExpr(lhs.LHS().Expr(), depth)
if err != nil {
return nil, nil, err
}
aMin, aMax, err := q.bcheckExpr(n.Args()[0].Arg().Value(), depth)
if err != nil {
return nil, nil, err
}
if methodName == t.IDMin {
return min(lMin, aMin), min(lMax, aMax), nil
} else {
return max(lMin, aMin), max(lMax, aMax), nil
}
}
}
+4
View File
@@ -465,6 +465,8 @@ const (
IDUnroll = ID(0x210)
IDSetMark = ID(0x211)
IDSinceMark = ID(0x212)
IDMax = ID(0x213)
IDMin = ID(0x214)
)
var builtInsByID = [nBuiltInIDs]string{
@@ -655,6 +657,8 @@ var builtInsByID = [nBuiltInIDs]string{
IDUnroll: "unroll",
IDSetMark: "set_mark",
IDSinceMark: "since_mark",
IDMax: "max",
IDMin: "min",
}
var builtInsByName = map[string]ID{}