Skip to content

Instantly share code, notes, and snippets.

@shilrobot
Last active March 11, 2017 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shilrobot/41cbe3acc63a949d5b599059259b09a0 to your computer and use it in GitHub Desktop.
Save shilrobot/41cbe3acc63a949d5b599059259b09a0 to your computer and use it in GitHub Desktop.
Color4 Lerp(Color4 a, Color4 b, float t)
{
// nice branchless SIMD color lerp
__m128 one = _mm_set1_ps(1);
__m128 t4 = _mm_set1_ps(t);
t4 = _mm_max_ps(t4, _mm_setzero_ps());
t4 = _mm_min_ps(t4, one);
__m128i a8 = _mm_set1_epi32(a.rgba);
__m128i b8 = _mm_set1_epi32(b.rgba);
__m128 aFloat = _mm_cvtepi32_ps(
_mm_unpacklo_epi16(
_mm_unpacklo_epi8(
a8,
_mm_setzero_si128()
),
_mm_setzero_si128()
)
);
__m128 bFloat = _mm_cvtepi32_ps(
_mm_unpacklo_epi16(
_mm_unpacklo_epi8(
b8,
_mm_setzero_si128()
),
_mm_setzero_si128()
)
);
aFloat = _mm_mul_ps(aFloat, _mm_sub_ps(one, t4));
bFloat = _mm_mul_ps(bFloat, t4);
__m128 cFloat = _mm_add_ps(aFloat, bFloat);
cFloat = _mm_add_ps(cFloat, _mm_set1_ps(0.5f));
__m128i c8 = _mm_packus_epi16(
_mm_packs_epi32(
_mm_cvtps_epi32(cFloat),
_mm_setzero_si128()
),
_mm_setzero_si128()
);
return Color4(*((uint32_t*)&c8));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment