lang: have hasher_u32.update_u32 take a roslice

This commit is contained in:
Nigel Tao committed 2023-04-10 21:16:53 +10:00
1 parent ea3fbc37f5
commit 29c4122360
11 files changed
+68 -42

No files matched your search

+2 -2
View File
@@ -501,7 +501,7 @@ var funcsOther = [...]string{
"io_reader.mark() u64",
"io_reader.match7(a: u64) u32[..= 2]",
"io_reader.position() u64",
"io_reader.since(mark: u64) slice u8",
"io_reader.since(mark: u64) roslice u8",
"io_reader.valid_utf_8_length(up_to: u64) u64",
"io_reader.limited_copy_u32_to_slice!(up_to: u32, s: slice u8) u32",
@@ -876,7 +876,7 @@ var InterfaceFuncs = []string{
// ---- hasher_u32
"hasher_u32.set_quirk_enabled!(quirk: u32, enabled: bool)",
"hasher_u32.update_u32!(x: slice u8) u32",
"hasher_u32.update_u32!(x: roslice u8) u32",
// ---- image_decoder
+17 -11
View File
@@ -92,9 +92,12 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte,
funcs: map[t.QQID]*a.Func{},
localVars: map[t.QQID]typeMap{},
builtInSliceFuncs: map[t.QQID]*a.Func{},
builtInSliceU8Funcs: map[t.QQID]*a.Func{},
builtInTableFuncs: map[t.QQID]*a.Func{},
builtInRosliceFuncs: map[t.QQID]*a.Func{},
builtInRosliceU8Funcs: map[t.QQID]*a.Func{},
builtInRotableFuncs: map[t.QQID]*a.Func{},
builtInSliceFuncs: map[t.QQID]*a.Func{},
builtInSliceU8Funcs: map[t.QQID]*a.Func{},
builtInTableFuncs: map[t.QQID]*a.Func{},
builtInInterfaces: map[t.QID][]t.QQID{},
builtInInterfaceFuncs: map[t.QQID]*a.Func{},
@@ -102,20 +105,20 @@ func Check(tm *t.Map, files []*a.File, resolveUse func(usePath string) ([]byte,
}
for _, funcs := range builtin.Funcs {
if err := c.parseBuiltInFuncs(nil, funcs); err != nil {
if err := c.parseBuiltInFuncs(nil, nil, funcs); err != nil {
return nil, err
}
}
if err := c.parseBuiltInFuncs(c.builtInSliceFuncs, builtin.SliceFuncs); err != nil {
if err := c.parseBuiltInFuncs(c.builtInSliceFuncs, c.builtInRosliceFuncs, builtin.SliceFuncs); err != nil {
return nil, err
}
if err := c.parseBuiltInFuncs(c.builtInSliceU8Funcs, builtin.SliceU8Funcs); err != nil {
if err := c.parseBuiltInFuncs(c.builtInSliceU8Funcs, c.builtInRosliceU8Funcs, builtin.SliceU8Funcs); err != nil {
return nil, err
}
if err := c.parseBuiltInFuncs(c.builtInTableFuncs, builtin.TableFuncs); err != nil {
if err := c.parseBuiltInFuncs(c.builtInTableFuncs, c.builtInRotableFuncs, builtin.TableFuncs); err != nil {
return nil, err
}
if err := c.parseBuiltInFuncs(c.builtInInterfaceFuncs, builtin.InterfaceFuncs); err != nil {
if err := c.parseBuiltInFuncs(c.builtInInterfaceFuncs, nil, builtin.InterfaceFuncs); err != nil {
return nil, err
}
@@ -232,9 +235,12 @@ type Checker struct {
funcs map[t.QQID]*a.Func
localVars map[t.QQID]typeMap
builtInSliceFuncs map[t.QQID]*a.Func
builtInSliceU8Funcs map[t.QQID]*a.Func
builtInTableFuncs map[t.QQID]*a.Func
builtInRosliceFuncs map[t.QQID]*a.Func
builtInRosliceU8Funcs map[t.QQID]*a.Func
builtInRotableFuncs map[t.QQID]*a.Func
builtInSliceFuncs map[t.QQID]*a.Func
builtInSliceU8Funcs map[t.QQID]*a.Func
builtInTableFuncs map[t.QQID]*a.Func
builtInInterfaces map[t.QID][]t.QQID
builtInInterfaceFuncs map[t.QQID]*a.Func
+9 -4
View File
@@ -94,8 +94,10 @@ var (
typeExprX86AVX2Utility = a.NewTypeExpr(0, t.IDBase, t.IDX86AVX2Utility, nil, nil, nil)
typeExprX86M256I = a.NewTypeExpr(0, t.IDBase, t.IDX86M256I, nil, nil, nil)
typeExprSliceU8 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, typeExprU8)
typeExprTableU8 = a.NewTypeExpr(t.IDTable, 0, 0, nil, nil, typeExprU8)
typeExprRosliceU8 = a.NewTypeExpr(t.IDRoslice, 0, 0, nil, nil, typeExprU8)
typeExprRotableU8 = a.NewTypeExpr(t.IDRotable, 0, 0, nil, nil, typeExprU8)
typeExprSliceU8 = a.NewTypeExpr(t.IDSlice, 0, 0, nil, nil, typeExprU8)
typeExprTableU8 = a.NewTypeExpr(t.IDTable, 0, 0, nil, nil, typeExprU8)
)
func setPlaceholderMBoundsMType(n *a.Node) {
@@ -162,7 +164,7 @@ var builtInTypeMap = typeMap{
t.IDX86M256I: typeExprX86M256I,
}
func (c *Checker) parseBuiltInFuncs(m map[t.QQID]*a.Func, ss []string) error {
func (c *Checker) parseBuiltInFuncs(m map[t.QQID]*a.Func, mRonly map[t.QQID]*a.Func, ss []string) error {
return builtin.ParseFuncs(c.tm, ss, func(f *a.Func) error {
if err := c.checkFuncSignature1(f.AsNode(), false); err != nil {
return err
@@ -170,6 +172,9 @@ func (c *Checker) parseBuiltInFuncs(m map[t.QQID]*a.Func, ss []string) error {
if m != nil {
m[f.QQID()] = f
}
if (mRonly != nil) && f.Effect().Pure() {
mRonly[f.QQID()] = f
}
return nil
})
}
@@ -190,7 +195,7 @@ func (c *Checker) resolveFunc(typ *a.TypeExpr) (*a.Func, error) {
return f, nil
}
}
if lTyp.Eq(typeExprSliceU8) {
if lTyp.Eq(typeExprSliceU8) || lTyp.Eq(typeExprRosliceU8) {
if f := c.builtInSliceU8Funcs[qqid]; f != nil {
if (lTyp.Decorator() == t.IDSlice) || f.Effect().Pure() {
return f, nil
+21 -6
View File
@@ -332,9 +332,9 @@ func (q *checker) tcheckAssign(n *a.Assign) error {
return err
}
for l := lhs; l != nil; l = l.LHS().AsExpr() {
if l.MType().IsReadOnly() {
if (l.Operator() == t.IDOpenBracket) && l.LHS().MType().IsReadOnly() {
return fmt.Errorf("check: assignment %q: assignee fragment %q, of type %q, has read-only type",
n.Operator().Str(q.tm), l.Str(q.tm), l.MType().Str(q.tm))
n.Operator().Str(q.tm), l.LHS().AsExpr().Str(q.tm), l.LHS().AsExpr().MType().Str(q.tm))
}
}
lTyp := lhs.MType()
@@ -688,6 +688,22 @@ func (q *checker) tcheckExprCall(n *a.Expr, depth uint32) error {
return nil
}
func (c *Checker) isBuiltInSliceFunc(qqid t.QQID, typ *a.TypeExpr) bool {
if typ.Decorator() == t.IDRoslice {
return (c.builtInRosliceFuncs[qqid] != nil) ||
(typ.Eq(typeExprRosliceU8) && c.builtInRosliceU8Funcs[qqid] != nil)
}
return (c.builtInSliceFuncs[qqid] != nil) ||
(typ.Eq(typeExprSliceU8) && c.builtInSliceU8Funcs[qqid] != nil)
}
func (c *Checker) isBuiltInTableFunc(qqid t.QQID, typ *a.TypeExpr) bool {
if typ.Decorator() == t.IDRotable {
return c.builtInRotableFuncs[qqid] != nil
}
return c.builtInTableFuncs[qqid] != nil
}
func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
lhs := n.LHS().AsExpr()
if err := q.tcheckExpr(lhs, depth); err != nil {
@@ -703,17 +719,16 @@ func (q *checker) tcheckDot(n *a.Expr, depth uint32) error {
if lTyp.IsEitherSliceType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger1
if (q.c.builtInSliceFuncs[qqid] != nil) ||
((q.c.builtInSliceU8Funcs[qqid] != nil) && lTyp.Eq(typeExprSliceU8)) {
if q.c.isBuiltInSliceFunc(qqid, lTyp) {
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.AsNode(), nil, nil))
return nil
}
return fmt.Errorf("check: no slice method %q", n.Ident().Str(q.tm))
return fmt.Errorf("check: no %s method %q", lTyp.Decorator().Str(q.tm), n.Ident().Str(q.tm))
} else if lTyp.IsEitherTableType() {
qqid[0] = t.IDBase
qqid[1] = t.IDDagger2
if q.c.builtInTableFuncs[qqid] != nil {
if q.c.isBuiltInTableFunc(qqid, lTyp) {
n.SetMType(a.NewTypeExpr(t.IDFunc, 0, n.Ident(), lTyp.AsNode(), nil, nil))
return nil
}
+4 -4
View File
@@ -21,7 +21,7 @@ pub struct hasher? implements base.hasher_u32(
pub func hasher.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
}
pub func hasher.update_u32!(x: slice base.u8) base.u32 {
pub func hasher.update_u32!(x: roslice base.u8) base.u32 {
if not this.started {
this.started = true
this.state = 1
@@ -37,7 +37,7 @@ pub func hasher.update_u32!(x: slice base.u8) base.u32 {
return this.state
}
pri func hasher.up!(x: slice base.u8),
pri func hasher.up!(x: roslice base.u8),
choosy,
{
// The Adler-32 checksum's magic 65521 and 5552 numbers are discussed in
@@ -45,8 +45,8 @@ pri func hasher.up!(x: slice base.u8),
var s1 : base.u32
var s2 : base.u32
var remaining : slice base.u8
var p : slice base.u8
var remaining : roslice base.u8
var p : roslice base.u8
s1 = this.state.low_bits(n: 16)
s2 = this.state.high_bits(n: 16)
+3 -3
View File
@@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pri func hasher.up_arm_neon!(x: slice base.u8),
pri func hasher.up_arm_neon!(x: roslice base.u8),
choose cpu_arch >= arm_neon,
{
// These variables are the same as the non-SIMD version.
var s1 : base.u32
var s2 : base.u32
var remaining : slice base.u8
var p : slice base.u8
var remaining : roslice base.u8
var p : roslice base.u8
// The remaining variables are specific to the SIMD version.
+3 -3
View File
@@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pri func hasher.up_x86_sse42!(x: slice base.u8),
pri func hasher.up_x86_sse42!(x: roslice base.u8),
choose cpu_arch >= x86_sse42,
{
// These variables are the same as the non-SIMD version.
var s1 : base.u32
var s2 : base.u32
var remaining : slice base.u8
var p : slice base.u8
var remaining : roslice base.u8
var p : roslice base.u8
// The remaining variables are specific to the SIMD version.
+3 -3
View File
@@ -23,7 +23,7 @@ pub func ieee_hasher.set_quirk_enabled!(quirk: base.u32, enabled: base.bool) {
// TODO: should it be ? instead of ! because it's public, and might need a way
// to signal "initializer not called"? Should the return type, in the generated
// C code, be "struct{ uint32_t checksum; wuffs_crc32__status status }"?
pub func ieee_hasher.update_u32!(x: slice base.u8) base.u32 {
pub func ieee_hasher.update_u32!(x: roslice base.u8) base.u32 {
if this.state == 0 {
choose up = [
up_arm_crc32,
@@ -34,11 +34,11 @@ pub func ieee_hasher.update_u32!(x: slice base.u8) base.u32 {
return this.state
}
pri func ieee_hasher.up!(x: slice base.u8),
pri func ieee_hasher.up!(x: roslice base.u8),
choosy,
{
var s : base.u32
var p : slice base.u8
var p : roslice base.u8
s = 0xFFFF_FFFF ^ this.state
+2 -2
View File
@@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
pri func ieee_hasher.up_arm_crc32!(x: slice base.u8),
pri func ieee_hasher.up_arm_crc32!(x: roslice base.u8),
choose cpu_arch >= arm_crc32,
{
var p : slice base.u8
var p : roslice base.u8
var util : base.arm_crc32_utility
var s : base.arm_crc32_u32
+2 -2
View File
@@ -20,11 +20,11 @@
// up_x86_avx2 is exactly the same as up_x86_sse42 except for the "choose
// cpu_arch >= x86_avx2". With AVX, PCLMULQDQ has a three-operand form, not
// just a two-operand form: https://www.felixcloutier.com/x86/pclmulqdq
pri func ieee_hasher.up_x86_avx2!(x: slice base.u8),
pri func ieee_hasher.up_x86_avx2!(x: roslice base.u8),
choose cpu_arch >= x86_avx2,
{
var s : base.u32
var p : slice base.u8
var p : roslice base.u8
var util : base.x86_sse42_utility
var k : base.x86_m128i
+2 -2
View File
@@ -17,11 +17,11 @@
// See "SIMD Implementations" in README.md for a link to Gopal et al. "Fast CRC
// Computation for Generic Polynomials Using PCLMULQDQ Instruction".
pri func ieee_hasher.up_x86_sse42!(x: slice base.u8),
pri func ieee_hasher.up_x86_sse42!(x: roslice base.u8),
choose cpu_arch >= x86_sse42,
{
var s : base.u32
var p : slice base.u8
var p : roslice base.u8
var util : base.x86_sse42_utility
var k : base.x86_m128i