fix float <---> uint16_t conversion, with Mike's tests.

BUG=skia:
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1679713002

patch from issue 1679713002 at patchset 1 (http://crrev.com/1679713002#ps1)
CQ_EXTRA_TRYBOTS=client.skia:Test-Ubuntu-GCC-GCE-CPU-AVX2-x86_64-Release-SKNX_NO_SIMD-Trybot

Review URL: https://codereview.chromium.org/1676893002
This commit is contained in:
mtklein 2016-02-08 05:54:38 -08:00 committed by Commit bot
parent a58ec2162c
commit 629f25a7ef
2 changed files with 57 additions and 1 deletions

View File

@ -350,7 +350,16 @@ template<> inline Sk4i SkNx_cast<int, float, 4>(const Sk4f& src) {
template<> inline Sk4h SkNx_cast<uint16_t, float, 4>(const Sk4f& src) {
auto _32 = _mm_cvttps_epi32(src.fVec);
return _mm_packus_epi16(_32, _32);
// Ideally we'd use _mm_packus_epi32 here. But that's SSE4.1+.
#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSSE3
// With SSSE3, we can just shuffle the low 2 bytes from each lane right into place.
const int _ = ~0;
return _mm_shuffle_epi8(_32, _mm_setr_epi8(0,1, 4,5, 8,9, 12,13, _,_,_,_,_,_,_,_));
#else
// With SSE2, we have to emulate _mm_packus_epi32 with _mm_packs_epi32:
_32 = _mm_sub_epi32(_32, _mm_set1_epi32((int)0x00008000));
return _mm_add_epi16(_mm_packs_epi32(_32, _32), _mm_set1_epi16((short)0x8000));
#endif
}
template<> inline Sk4b SkNx_cast<uint8_t, float, 4>(const Sk4f& src) {

View File

@ -224,3 +224,50 @@ DEF_TEST(SkNx_abs, r) {
REPORTER_ASSERT(r, fs.kth<2>() == 2.0f);
REPORTER_ASSERT(r, fs.kth<3>() == 4.0f);
}
#include "SkRandom.h"
static void dump(const Sk4f& f4, const Sk4h& h4) {
SkDebugf("%g %g %g %g --> %d %d %d %d\n",
f4.kth<0>(), f4.kth<1>(), f4.kth<2>(), f4.kth<3>(),
h4.kth<0>(), h4.kth<1>(), h4.kth<2>(), h4.kth<3>());
}
DEF_TEST(SkNx_u16_float, r) {
{
// u16 --> float
auto h4 = Sk4h(15, 17, 257, 65535);
auto f4 = SkNx_cast<float>(h4);
dump(f4, h4);
REPORTER_ASSERT(r, f4.kth<0>() == 15.0f);
REPORTER_ASSERT(r, f4.kth<1>() == 17.0f);
REPORTER_ASSERT(r, f4.kth<2>() == 257.0f);
REPORTER_ASSERT(r, f4.kth<3>() == 65535.0f);
}
{
// float -> u16
auto f4 = Sk4f(15, 17, 257, 65535);
auto h4 = SkNx_cast<uint16_t>(f4);
dump(f4, h4);
REPORTER_ASSERT(r, h4.kth<0>() == 15);
REPORTER_ASSERT(r, h4.kth<1>() == 17);
REPORTER_ASSERT(r, h4.kth<2>() == 257);
REPORTER_ASSERT(r, h4.kth<3>() == 65535);
}
// starting with any u16 value, we should be able to have a perfect round-trip in/out of floats
//
SkRandom rand;
for (int i = 0; i < 0; ++i) {
const uint16_t s16[4] {
(uint16_t)rand.nextU16(), (uint16_t)rand.nextU16(),
(uint16_t)rand.nextU16(), (uint16_t)rand.nextU16(),
};
auto u4_0 = Sk4h::Load(s16);
auto f4 = SkNx_cast<float>(u4_0);
auto u4_1 = SkNx_cast<uint16_t>(f4);
uint16_t d16[4];
u4_1.store(d16);
REPORTER_ASSERT(r, !memcmp(s16, d16, sizeof(s16)));
}
}