func (d *Uint32Div) Set(div uint32) { var L, L2, sh1, sh2, m uint32 switch div { case 0: panic("cannot divide by 0") case 1: m = 1 // parameters for d = 1 case 2: m = 1 sh1 = 1 // parameters for d = 2 default: // general case for d > 2 L = misc.BitScanReverse(div-1) + 1 // ceil(log2(d)) // 2^L, overflow to 0 if L = 32 if L < 32 { L2 = 1 << L } m = 1 + uint32((uint64(L2-div)<<32)/d) // multiplier sh1 = 1 sh2 = L - 1 // shift counts } d.multiplier = sse2.Set1Epi32(m) d.shift1 = sse2.SetEpi32(sh1, 0, 0, 0) d.shift2 = sse2.SetEpi32(sh2, 0, 0, 0) }
func TestAddEpi8(t *testing.T) { a := sse2.SetEpi32(0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c) b := sse2.SetEpi32(0x03020100, 0x07060504, 0x0b0a0908, 0x0f0e0d0c) c := sse2.AddEpi8(a, b) expect := x86.M128i{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30} if !reflect.DeepEqual(c, expect) { t.Fatal("got", c, "expected", expect) } t.Log("correctly got", expect) }
// vector of 4 32-bit unsigned integers func (u Uint32Div) DivSSE4(a M128i) M128i { t1 := sse2.MulEpu32(a, u.multiplier) // 32x32->64 bit unsigned multiplication of a[0] and a[2] t2 := sse2.SrliEpi64(t1, 32) // high dword of result 0 and 2 t3 := sse2.SrliEpi64(a, 32) // get a[1] and a[3] into position for multiplication t4 := sse2.MulEpu32(t3, u.multiplier) // 32x32->64 bit unsigned multiplication of a[1] and a[3] t5 := sse2.SetEpi32(-1, 0, -1, 0) // mask of dword 1 and 3 t7 := sse4.BlendvEpi8(t2, t4, t5) // blend two results t8 := sse2.SubEpi32(a, t7) // subtract t9 := sse2.SrlEpi32(t8, u.shift1) // shift right logical t10 := sse2.AddEpi32(t7, t9) // add return sse2.SrlEpi32(t10, d.u.shift2) // shift right logical }
// vector of 4 32-bit unsigned integers func (d Uint32Div) Div(a M128i) M128i { t1 := sse2.MulEpu32(a, u.multiplier) // 32x32->64 bit unsigned multiplication of a[0] and a[2] t2 := sse2.SrliEpi64(t1, 32) // high dword of result 0 and 2 t3 := sse2.SrliEpi64(a, 32) // get a[1] and a[3] into position for multiplication t4 := sse2.MulEpu32(t3, u.multiplier) // 32x32->64 bit unsigned multiplication of a[1] and a[3] t5 := sse2.SetEpi32(-1, 0, -1, 0) // mask of dword 1 and 3 t6 := sse2.AndSi128(t4, t5) // high dword of result 1 and 3 t7 := sse2.OrSi128(t2, t6) // combine all four results into one vector t8 := sse2.SubEpi32(a, t7) // subtract t9 := sse2.SrlEpi32(t8, u.shift1) // shift right logical t10 := sse2.AddEpi32(t7, t9) // add return sse2.SrlEpi32(t10, d.u.shift2) // shift right logical }
// Try some complex intrinsics (SSE2) // Doesn't test any values (since intrisics are unimplemented) // Converted from https://github.com/klauspost/rawspeed/blob/develop/RawSpeed/RawImageDataU16.cpp#L152 func TestComplex(t *testing.T) { full_scale_fp := 1000 half_scale_fp := 500 mDitherScale := true var sub_mul [4]x86.M128i var rand_mul x86.M128i sseround := sse2.SetEpi32(512, 512, 512, 512) ssesub2 := sse2.SetEpi32(32768, 32768, 32768, 32768) ssesign := sse2.SetEpi32(0x80008000, 0x80008000, 0x80008000, 0x80008000) sse_full_scale_fp := sse2.Set1Epi32(full_scale_fp | (full_scale_fp << 16)) sse_half_scale_fp := sse2.Set1Epi32(half_scale_fp >> 4) if mDitherScale { rand_mul = sse2.Set1Epi32(0x4d9f1d32) } else { rand_mul = sse2.SetzeroSi128() } rand_mask := sse2.Set1Epi32(0x00ff00ff) // 8 random bits width := 1024 height := 1024 // Emulate 1024 x 1024 x 16bpp input := make([]byte, 1024*1024*2) for y := 0; y < height; y++ { // Convert current line to []M128i line := x86.BytesToM128i(input[y*width*2 : y*width*2+width*2]) var sserandom x86.M128i if mDitherScale { sserandom = sse2.SetEpi32(width*1676+y*18000, width*2342+y*34311, width*4272+y*12123, width*1234+y*23464) } else { sserandom = sse2.SetzeroSi128() } var ssescale, ssesub x86.M128i if (y & 1) == 0 { ssesub = sub_mul[0] ssescale = sub_mul[1] } else { ssesub = sub_mul[2] ssescale = sub_mul[3] } for x, pix_low := range line { // Subtract black pix_low = sse2.SubsEpu16(pix_low, ssesub) // Multiply the two unsigned shorts and combine it to 32 bit result pix_high := sse2.MulhiEpu16(pix_low, ssescale) temp := sse2.MulloEpi16(pix_low, ssescale) pix_low = sse2.UnpackloEpi16(temp, pix_high) pix_high = sse2.UnpackhiEpi16(temp, pix_high) // Add rounder pix_low = sse2.AddEpi32(pix_low, sseround) pix_high = sse2.AddEpi32(pix_high, sseround) sserandom = sse2.XorSi128(sse2.MulhiEpi16(sserandom, rand_mul), sse2.MulloEpi16(sserandom, rand_mul)) rand_masked := sse2.AndSi128(sserandom, rand_mask) // Get 8 random bits rand_masked = sse2.MulloEpi16(rand_masked, sse_full_scale_fp) zero := sse2.SetzeroSi128() rand_lo := sse2.SubEpi32(sse_half_scale_fp, sse2.UnpackloEpi16(rand_masked, zero)) rand_hi := sse2.SubEpi32(sse_half_scale_fp, sse2.UnpackhiEpi16(rand_masked, zero)) pix_low = sse2.AddEpi32(pix_low, rand_lo) pix_high = sse2.AddEpi32(pix_high, rand_hi) // Shift down pix_low = sse2.SraiEpi32(pix_low, 10) pix_high = sse2.SraiEpi32(pix_high, 10) // Subtract to avoid clipping pix_low = sse2.SubEpi32(pix_low, ssesub2) pix_high = sse2.SubEpi32(pix_high, ssesub2) // Pack pix_low = sse2.PacksEpi32(pix_low, pix_high) // Shift sign off pix_low = sse2.XorSi128(pix_low, ssesign) line[x] = pix_low } } }