From 50fae2b1a7a7a5f3029c50f9884ccdde0762c8a3 Mon Sep 17 00:00:00 2001 From: qonnop Date: Sun, 7 Oct 2018 15:40:50 +0200 Subject: [PATCH] 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 --- core/variant_op.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/core/variant_op.cpp b/core/variant_op.cpp index f230f12b5de..d193858966f 100644 --- a/core/variant_op.cpp +++ b/core/variant_op.cpp @@ -3521,7 +3521,7 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & //not as efficient but.. real_t va = a; real_t vb = b; - r_dst = (1.0 - c) * va + vb * c; + r_dst = va + (vb - va) * c; } else { r_dst = a; @@ -3542,16 +3542,13 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & case INT: { int64_t va = a._data._int; int64_t vb = b._data._int; - if (va != vb) - r_dst = int((1.0 - c) * va + vb * c); - else //avoid int casting issues - r_dst = a; + r_dst = int(va + (vb - va) * c); } return; case REAL: { real_t va = a._data._real; real_t vb = b._data._real; - r_dst = (1.0 - c) * va + vb * c; + r_dst = va + (vb - va) * c; } return; case STRING: { @@ -3559,7 +3556,9 @@ void Variant::interpolate(const Variant &a, const Variant &b, float c, Variant & String sa = *reinterpret_cast(a._data._mem); String sb = *reinterpret_cast(b._data._mem); 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) { r_dst = ""; return;