From d4c23d091a5fc9db37723ed2ed67f4a36816b066 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Thu, 18 Feb 2021 20:29:12 +1100 Subject: [PATCH] Add base.x86_sse42_utility.make_m128i_single_u32 --- internal/cgen/builtin.go | 24 +++++++---------------- lang/builtin/builtin.go | 3 +++ lang/token/list.go | 4 ---- std/png/decode_filter_x86_sse42.wuffs | 28 +++++++++++++++------------ 4 files changed, 26 insertions(+), 33 deletions(-) diff --git a/internal/cgen/builtin.go b/internal/cgen/builtin.go index 26aeb210..cbbbb1ae 100644 --- a/internal/cgen/builtin.go +++ b/internal/cgen/builtin.go @@ -456,7 +456,7 @@ func (g *gen) writeBuiltinCPUArch(b *buffer, recv *a.Expr, method t.ID, args []* armNeon := recv.MType().Eq(typeExprARMNeon64) || recv.MType().Eq(typeExprARMNeon128) switch method { - case t.IDLoadU32, t.IDLoadU64, t.IDLoadSlice128: + case t.IDLoadSlice128: if !sideEffectsOnly { // Generate a two part expression using the comma operator: "(etc, // return_empty_struct call)". The final part is a function call @@ -468,25 +468,11 @@ func (g *gen) writeBuiltinCPUArch(b *buffer, recv *a.Expr, method t.ID, args []* return err } - switch method { - case t.IDLoadU32: - b.writes(" = _mm_cvtsi32_si128((int32_t)(") - case t.IDLoadU64: - b.writes(" = _mm_cvtsi64_si128((int64_t)(") - case t.IDLoadSlice128: - b.writes(" = _mm_lddqu_si128((const __m128i*)(const void*)(") - } - + b.writes(" = _mm_lddqu_si128((const __m128i*)(const void*)(") if err := g.writeExpr(b, args[0].AsArg().Value(), false, depth); err != nil { return err } - - switch method { - case t.IDLoadSlice128: - b.writes(".ptr))") - default: - b.writes("))") - } + b.writes(".ptr))") if !sideEffectsOnly { b.writes(", wuffs_base__make_empty_struct())") @@ -862,6 +848,10 @@ func (g *gen) writeBuiltinCPUArchX86(b *buffer, recv *a.Expr, method t.ID, args fName, tName = "_mm_set1_epi32", "int32_t" case "make_m128i_repeat_u64": fName, tName = "_mm_set1_epi64x", "int64_t" + case "make_m128i_single_u32": + fName, tName = "_mm_cvtsi32_si128", "int32_t" + case "make_m128i_single_u64": + fName, tName = "_mm_cvtsi64x_si128", "int64_t" case "make_m128i_zeroes": fName, tName = "_mm_setzero_si128", "" default: diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index a3a8f961..e9eb743b 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -677,6 +677,9 @@ var Funcs = []string{ "x86_sse42_utility.make_m128i_repeat_u32(a: u32) x86_m128i", "x86_sse42_utility.make_m128i_repeat_u64(a: u64) x86_m128i", + "x86_sse42_utility.make_m128i_single_u32(a: u32) x86_m128i", + "x86_sse42_utility.make_m128i_single_u64(a: u64) x86_m128i", + "x86_sse42_utility.make_m128i_zeroes() x86_m128i", // ---- x86_m128i diff --git a/lang/token/list.go b/lang/token/list.go index 6f88e876..4ffd216a 100644 --- a/lang/token/list.go +++ b/lang/token/list.go @@ -702,8 +702,6 @@ const ( minBuiltInLoad = 0x380 maxBuiltInLoad = 0x387 - IDLoadU32 = ID(0x380) - IDLoadU64 = ID(0x381) IDLoadSlice128 = ID(0x382) IDLoadSlice256 = ID(0x383) IDLoadSlice512 = ID(0x384) @@ -1119,8 +1117,6 @@ var builtInsByID = [nBuiltInIDs]string{ IDX86M128I: "x86_m128i", - IDLoadU32: "load_u32", - IDLoadU64: "load_u64", IDLoadSlice128: "load_slice128", IDLoadSlice256: "load_slice256", IDLoadSlice512: "load_slice512", diff --git a/std/png/decode_filter_x86_sse42.wuffs b/std/png/decode_filter_x86_sse42.wuffs index 5e9b7fb7..92515417 100644 --- a/std/png/decode_filter_x86_sse42.wuffs +++ b/std/png/decode_filter_x86_sse42.wuffs @@ -24,16 +24,17 @@ // { // var curr : slice base.u8 // +// var util : base.x86_sse42_utility // var x128 : base.x86_m128i // var a128 : base.x86_m128i // // iterate (curr = args.curr)(length: 4, advance: 3, unroll: 2) { -// x128.load_u32!(a: curr.peek_u32le()) +// x128 = util.make_m128i_single_u32(a: curr.peek_u32le()) // x128 = x128._mm_add_epi8(b: a128) // a128 = x128 // curr.poke_u24le!(a: x128.truncate_u32()) // } else (length: 3, advance: 3, unroll: 1) { -// x128.load_u32!(a: curr.peek_u24le_as_u32()) +// x128 = util.make_m128i_single_u32(a: curr.peek_u24le_as_u32()) // x128 = x128._mm_add_epi8(b: a128) // curr.poke_u24le!(a: x128.truncate_u32()) // } @@ -48,11 +49,12 @@ pri func decoder.filter_1_distance_4_x86_sse42!(curr: slice base.u8), { var curr : slice base.u8 + var util : base.x86_sse42_utility var x128 : base.x86_m128i var a128 : base.x86_m128i iterate (curr = args.curr)(length: 4, advance: 4, unroll: 2) { - x128.load_u32!(a: curr.peek_u32le()) + x128 = util.make_m128i_single_u32(a: curr.peek_u32le()) x128 = x128._mm_add_epi8(b: a128) a128 = x128 curr.poke_u32le!(a: x128.truncate_u32()) @@ -96,7 +98,7 @@ pri func decoder.filter_3_distance_4_x86_sse42!(curr: slice base.u8, prev: slice // Add the predictor to the residual and, for the next iteration, // set its previous pixel, a128, to this one, x128. - x128.load_u32!(a: curr.peek_u32le()) + x128 = util.make_m128i_single_u32(a: curr.peek_u32le()) x128 = x128._mm_add_epi8(b: p128) a128 = x128 curr.poke_u32le!(a: x128.truncate_u32()) @@ -106,7 +108,7 @@ pri func decoder.filter_3_distance_4_x86_sse42!(curr: slice base.u8, prev: slice k128 = util.make_m128i_repeat_u8(a: 0x01) iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) { // Load the pixel from the row above. - b128.load_u32!(a: prev.peek_u32le()) + b128 = util.make_m128i_single_u32(a: prev.peek_u32le()) // The predictor, p128, is the average (rounded down) of the // previous pixel, a128, and the pixel above, b128. @@ -119,7 +121,7 @@ pri func decoder.filter_3_distance_4_x86_sse42!(curr: slice base.u8, prev: slice // Add the predictor to the residual and, for the next iteration, // set its previous pixel, a128, to this one, x128. - x128.load_u32!(a: curr.peek_u32le()) + x128 = util.make_m128i_single_u32(a: curr.peek_u32le()) x128 = x128._mm_add_epi8(b: p128) a128 = x128 curr.poke_u32le!(a: x128.truncate_u32()) @@ -144,6 +146,7 @@ pri func decoder.filter_4_distance_3_x86_sse42!(curr: slice base.u8, prev: slice var curr : slice base.u8 var prev : slice base.u8 + var util : base.x86_sse42_utility var x128 : base.x86_m128i var a128 : base.x86_m128i var b128 : base.x86_m128i @@ -157,7 +160,7 @@ pri func decoder.filter_4_distance_3_x86_sse42!(curr: slice base.u8, prev: slice // § The advance is 3, not 4. iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 3, unroll: 2) { - b128.load_u32!(a: prev.peek_u32le()) + b128 = util.make_m128i_single_u32(a: prev.peek_u32le()) b128 = b128._mm_unpacklo_epi8(b: z128) pa128 = b128._mm_sub_epi16(b: c128) pb128 = a128._mm_sub_epi16(b: c128) @@ -171,7 +174,7 @@ pri func decoder.filter_4_distance_3_x86_sse42!(curr: slice base.u8, prev: slice mask: smallest128._mm_cmpeq_epi16(b: pb128))._mm_blendv_epi8( b: a128, mask: smallest128._mm_cmpeq_epi16(b: pa128)) - x128.load_u32!(a: curr.peek_u32le()) + x128 = util.make_m128i_single_u32(a: curr.peek_u32le()) x128 = x128._mm_unpacklo_epi8(b: z128) x128 = x128._mm_add_epi8(b: p128) a128 = x128 @@ -183,7 +186,7 @@ pri func decoder.filter_4_distance_3_x86_sse42!(curr: slice base.u8, prev: slice // § The length and advance are both 3, not 4. } else (length: 3, advance: 3, unroll: 1) { // § peek_u24le_as_u32 replaces peek_u32le. - b128.load_u32!(a: prev.peek_u24le_as_u32()) + b128 = util.make_m128i_single_u32(a: prev.peek_u24le_as_u32()) b128 = b128._mm_unpacklo_epi8(b: z128) pa128 = b128._mm_sub_epi16(b: c128) pb128 = a128._mm_sub_epi16(b: c128) @@ -198,7 +201,7 @@ pri func decoder.filter_4_distance_3_x86_sse42!(curr: slice base.u8, prev: slice b: a128, mask: smallest128._mm_cmpeq_epi16(b: pa128)) // § peek_u24le_as_u32 replaces peek_u32le. - x128.load_u32!(a: curr.peek_u24le_as_u32()) + x128 = util.make_m128i_single_u32(a: curr.peek_u24le_as_u32()) x128 = x128._mm_unpacklo_epi8(b: z128) x128 = x128._mm_add_epi8(b: p128) // § These assignments are unnecessary; this is the last iteration. @@ -216,6 +219,7 @@ pri func decoder.filter_4_distance_4_x86_sse42!(curr: slice base.u8, prev: slice var curr : slice base.u8 var prev : slice base.u8 + var util : base.x86_sse42_utility var x128 : base.x86_m128i var a128 : base.x86_m128i var b128 : base.x86_m128i @@ -229,7 +233,7 @@ pri func decoder.filter_4_distance_4_x86_sse42!(curr: slice base.u8, prev: slice iterate (curr = args.curr, prev = args.prev)(length: 4, advance: 4, unroll: 2) { // Load the pixel from the row above. - b128.load_u32!(a: prev.peek_u32le()) + b128 = util.make_m128i_single_u32(a: prev.peek_u32le()) // Convert from u8 to i16 by unpacking it with zeroes. b128 = b128._mm_unpacklo_epi8(b: z128) @@ -264,7 +268,7 @@ pri func decoder.filter_4_distance_4_x86_sse42!(curr: slice base.u8, prev: slice // its previous pixels, a128 and c128, to x128 and b128. // // Again, convert between u8 and i16 by unpacking and re-packing. - x128.load_u32!(a: curr.peek_u32le()) + x128 = util.make_m128i_single_u32(a: curr.peek_u32le()) x128 = x128._mm_unpacklo_epi8(b: z128) x128 = x128._mm_add_epi8(b: p128) a128 = x128