Inline From/ToRGB(A) to avoid losing SSE.

Otherwise it has to store it, which I'd like to avoid.
This commit is contained in:
Unknown W. Brackets committed 2014-03-17 23:03:04 -07:00
1 parent 1ce6bf399a
commit 416df17088
2 files changed
+108 -108

No files matched your search

-108
View File
@@ -66,59 +66,6 @@ float Vec2<float>::Normalize()
return len;
}
template<>
Vec3<float> Vec3<float>::FromRGB(unsigned int rgb)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgb);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec3<float>(_mm_mul_ps(_mm_cvtepi32_ps(c), _mm_set_ps1(1.0f / 255.0f)));
#else
return Vec3((rgb & 0xFF) * (1.0f/255.0f),
((rgb >> 8) & 0xFF) * (1.0f/255.0f),
((rgb >> 16) & 0xFF) * (1.0f/255.0f));
#endif
}
template<>
Vec3<int> Vec3<int>::FromRGB(unsigned int rgb)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgb);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec3<int>(c);
#else
return Vec3(rgb & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 16) & 0xFF);
#endif
}
template<>
unsigned int Vec3<float>::ToRGB() const
{
#if defined(_M_SSE)
__m128i c = _mm_cvtps_epi32(_mm_mul_ps(vec, _mm_set_ps1(255.0f)));
__m128i c16 = _mm_packs_epi32(c, c);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16)) & 0x00FFFFFF;
#else
return ((unsigned int)(r()*255.f)) +
((unsigned int)(g()*255.f*256.f)) +
((unsigned int)(b()*255.f*256.f*256.f));
#endif
}
template<>
unsigned int Vec3<int>::ToRGB() const
{
#if defined(_M_SSE)
__m128i c16 = _mm_packs_epi32(ivec, ivec);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16)) & 0x00FFFFFF;
#else
return (r()&0xFF) | ((g()&0xFF)<<8) | ((b()&0xFF)<<16);
#endif
}
template<>
float Vec3<float>::Length() const
{
@@ -233,61 +180,6 @@ float Vec3Packed<float>::Normalize()
return len;
}
template<>
Vec4<float> Vec4<float>::FromRGBA(unsigned int rgba)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgba);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec4<float>(_mm_mul_ps(_mm_cvtepi32_ps(c), _mm_set_ps1(1.0f / 255.0f)));
#else
return Vec4((rgba & 0xFF) * (1.0f/255.0f),
((rgba >> 8) & 0xFF) * (1.0f/255.0f),
((rgba >> 16) & 0xFF) * (1.0f/255.0f),
((rgba >> 24) & 0xFF) * (1.0f/255.0f));
#endif
}
template<>
Vec4<int> Vec4<int>::FromRGBA(unsigned int rgba)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgba);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec4<int>(c);
#else
return Vec4(rgba & 0xFF, (rgba >> 8) & 0xFF, (rgba >> 16) & 0xFF, (rgba >> 24) & 0xFF);
#endif
}
template<>
unsigned int Vec4<float>::ToRGBA() const
{
#if defined(_M_SSE)
__m128i c = _mm_cvtps_epi32(_mm_mul_ps(vec, _mm_set_ps1(255.0f)));
__m128i c16 = _mm_packs_epi32(c, c);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16));
#else
return ((unsigned int)(r()*255.f)) +
((unsigned int)(g()*255.f*256.f)) +
((unsigned int)(b()*255.f*256.f*256.f)) +
((unsigned int)(a()*255.f*256.f*256.f*256.f));
#endif
}
template<>
unsigned int Vec4<int>::ToRGBA() const
{
#if defined(_M_SSE)
__m128i c16 = _mm_packs_epi32(ivec, ivec);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16));
#else
return (r()&0xFF) | ((g()&0xFF)<<8) | ((b()&0xFF)<<16) | ((a()&0xFF)<<24);
#endif
}
template<>
float Vec4<float>::Length() const
{
+108
View File
@@ -891,6 +891,114 @@ inline Vec3Packed<T> Cross(const Vec3Packed<T>& a, const Vec3Packed<T>& b)
return Vec3Packed<T>(a.y*b.z-a.z*b.y, a.z*b.x-a.x*b.z, a.x*b.y-a.y*b.x);
}
template<>
inline Vec3<float> Vec3<float>::FromRGB(unsigned int rgb)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgb);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec3<float>(_mm_mul_ps(_mm_cvtepi32_ps(c), _mm_set_ps1(1.0f / 255.0f)));
#else
return Vec3((rgb & 0xFF) * (1.0f/255.0f),
((rgb >> 8) & 0xFF) * (1.0f/255.0f),
((rgb >> 16) & 0xFF) * (1.0f/255.0f));
#endif
}
template<>
inline Vec3<int> Vec3<int>::FromRGB(unsigned int rgb)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgb);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec3<int>(c);
#else
return Vec3(rgb & 0xFF, (rgb >> 8) & 0xFF, (rgb >> 16) & 0xFF);
#endif
}
template<>
inline unsigned int Vec3<float>::ToRGB() const
{
#if defined(_M_SSE)
__m128i c = _mm_cvtps_epi32(_mm_mul_ps(vec, _mm_set_ps1(255.0f)));
__m128i c16 = _mm_packs_epi32(c, c);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16)) & 0x00FFFFFF;
#else
return ((unsigned int)(r()*255.f)) +
((unsigned int)(g()*255.f*256.f)) +
((unsigned int)(b()*255.f*256.f*256.f));
#endif
}
template<>
inline unsigned int Vec3<int>::ToRGB() const
{
#if defined(_M_SSE)
__m128i c16 = _mm_packs_epi32(ivec, ivec);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16)) & 0x00FFFFFF;
#else
return (r()&0xFF) | ((g()&0xFF)<<8) | ((b()&0xFF)<<16);
#endif
}
template<>
inline Vec4<float> Vec4<float>::FromRGBA(unsigned int rgba)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgba);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec4<float>(_mm_mul_ps(_mm_cvtepi32_ps(c), _mm_set_ps1(1.0f / 255.0f)));
#else
return Vec4((rgba & 0xFF) * (1.0f/255.0f),
((rgba >> 8) & 0xFF) * (1.0f/255.0f),
((rgba >> 16) & 0xFF) * (1.0f/255.0f),
((rgba >> 24) & 0xFF) * (1.0f/255.0f));
#endif
}
template<>
inline Vec4<int> Vec4<int>::FromRGBA(unsigned int rgba)
{
#if defined(_M_SSE)
__m128i z = _mm_setzero_si128();
__m128i c = _mm_cvtsi32_si128(rgba);
c = _mm_unpacklo_epi16(_mm_unpacklo_epi8(c, z), z);
return Vec4<int>(c);
#else
return Vec4(rgba & 0xFF, (rgba >> 8) & 0xFF, (rgba >> 16) & 0xFF, (rgba >> 24) & 0xFF);
#endif
}
template<>
inline unsigned int Vec4<float>::ToRGBA() const
{
#if defined(_M_SSE)
__m128i c = _mm_cvtps_epi32(_mm_mul_ps(vec, _mm_set_ps1(255.0f)));
__m128i c16 = _mm_packs_epi32(c, c);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16));
#else
return ((unsigned int)(r()*255.f)) +
((unsigned int)(g()*255.f*256.f)) +
((unsigned int)(b()*255.f*256.f*256.f)) +
((unsigned int)(a()*255.f*256.f*256.f*256.f));
#endif
}
template<>
inline unsigned int Vec4<int>::ToRGBA() const
{
#if defined(_M_SSE)
__m128i c16 = _mm_packs_epi32(ivec, ivec);
return _mm_cvtsi128_si32(_mm_packus_epi16(c16, c16));
#else
return (r()&0xFF) | ((g()&0xFF)<<8) | ((b()&0xFF)<<16) | ((a()&0xFF)<<24);
#endif
}
}; // namespace Math3D
// linear interpolation via float: 0.0=begin, 1.0=end