Optimize interpolation algorithms

Interpolation in the form of va + (vb - va) * c is faster and prevents floating point issues for int/string
Followup to https://github.com/godotengine/godot/pull/22786
This commit is contained in:
qonnop 2018-10-07 15:40:50 +02:00
parent 5676632a3b
commit 50fae2b1a7
1 changed files with 6 additions and 7 deletions

View File

@ -3521,7 +3521,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &
//not as efficient but.. //not as efficient but..
real_t va = a; real_t va = a;
real_t vb = b; real_t vb = b;
r_dst = (1.0 - c) * va + vb * c; r_dst = va + (vb - va) * c;
} else { } else {
r_dst = a; r_dst = a;
@ -3542,16 +3542,13 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &
case INT: { case INT: {
int64_t va = a._data._int; int64_t va = a._data._int;
int64_t vb = b._data._int; int64_t vb = b._data._int;
if (va != vb) r_dst = int(va + (vb - va) * c);
r_dst = int((1.0 - c) * va + vb * c);
else //avoid int casting issues
r_dst = a;
} }
return; return;
case REAL: { case REAL: {
real_t va = a._data._real; real_t va = a._data._real;
real_t vb = b._data._real; real_t vb = b._data._real;
r_dst = (1.0 - c) * va + vb * c; r_dst = va + (vb - va) * c;
} }
return; return;
case STRING: { case STRING: {
@ -3559,7 +3556,9 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant &
String sa = *reinterpret_cast<const String *>(a._data._mem); String sa = *reinterpret_cast<const String *>(a._data._mem);
String sb = *reinterpret_cast<const String *>(b._data._mem); String sb = *reinterpret_cast<const String *>(b._data._mem);
String dst; String dst;
int csize = sb.length() * c + sa.length() * (1.0 - c); int sa_len = sa.length();
int sb_len = sb.length();
int csize = sa_len + (sb_len - sa_len) * c;
if (csize == 0) { if (csize == 0) {
r_dst = ""; r_dst = "";
return; return;