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

+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{}