[frameworks][native][libs]{math|ui} fix -Wimplicit-int-float-conversion
Some of the overloaded constructors accept templated types. This makes
it simple to specify Vec{2|3}/Quat's using integer literals. This could
result in precision loss for large integrals that aren't precisely
representable by IEEE 754 Single Precision floats. If you're creating
such primitives from such large values, and getting imprecision...well,
you kind of asked for it.
Add explicit casts to silence the warning.
Bug: 139945549
Test: mm
Change-Id: Icaaa8906157aa166f54d915fca643efd639fdafa
Signed-off-by: Nick Desaulniers <ndesaulniers@google.com>
diff --git a/libs/math/include/math/quat.h b/libs/math/include/math/quat.h
index 1936a2b..07573c5 100644
--- a/libs/math/include/math/quat.h
+++ b/libs/math/include/math/quat.h
@@ -109,7 +109,7 @@
// initialize from 4 values to w + xi + yj + zk
template<typename A, typename B, typename C, typename D>
- constexpr TQuaternion(A w, B x, C y, D z) : x(x), y(y), z(z), w(w) { }
+ constexpr TQuaternion(A w, B x, C y, D z) : x(static_cast<T>(x)), y(static_cast<T>(y)), z(static_cast<T>(z)), w(static_cast<T>(w)) { }
// initialize from a vec3 + a value to : v.xi + v.yj + v.zk + w
template<typename A, typename B>
diff --git a/libs/math/include/math/vec2.h b/libs/math/include/math/vec2.h
index a347633..e0adb7f 100644
--- a/libs/math/include/math/vec2.h
+++ b/libs/math/include/math/vec2.h
@@ -89,7 +89,7 @@
constexpr TVec2(A v) : x(v), y(v) { }
template<typename A, typename B>
- constexpr TVec2(A x, B y) : x(x), y(y) { }
+ constexpr TVec2(A x, B y) : x(static_cast<T>(x)), y(static_cast<T>(y)) { }
template<typename A>
explicit
diff --git a/libs/math/include/math/vec3.h b/libs/math/include/math/vec3.h
index 009fd84..21fb684 100644
--- a/libs/math/include/math/vec3.h
+++ b/libs/math/include/math/vec3.h
@@ -86,13 +86,13 @@
// handles implicit conversion to a tvec4. must not be explicit.
template<typename A, typename = typename std::enable_if<std::is_arithmetic<A>::value >::type>
- constexpr TVec3(A v) : x(v), y(v), z(v) { }
+ constexpr TVec3(A v) : x(static_cast<T>(v)), y(static_cast<T>(v)), z(static_cast<T>(v)) { }
template<typename A, typename B, typename C>
- constexpr TVec3(A x, B y, C z) : x(x), y(y), z(z) { }
+ constexpr TVec3(A x, B y, C z) : x(static_cast<T>(x)), y(static_cast<T>(y)), z(static_cast<T>(z)) { }
template<typename A, typename B>
- constexpr TVec3(const TVec2<A>& v, B z) : x(v.x), y(v.y), z(z) { }
+ constexpr TVec3(const TVec2<A>& v, B z) : x(v.x), y(v.y), z(static_cast<T>(z)) { }
template<typename A>
explicit
diff --git a/libs/ui/ColorSpace.cpp b/libs/ui/ColorSpace.cpp
index 7a14af1..df390e2 100644
--- a/libs/ui/ColorSpace.cpp
+++ b/libs/ui/ColorSpace.cpp
@@ -364,7 +364,11 @@
for (uint32_t z = 0; z < size; z++) {
for (int32_t y = int32_t(size - 1); y >= 0; y--) {
for (uint32_t x = 0; x < size; x++) {
- *data++ = connector.transform({x * m, y * m, z * m});
+ *data++ = connector.transform({
+ static_cast<float>(x) * m,
+ static_cast<float>(y) * m,
+ static_cast<float>(z) * m,
+ });
}
}
}
diff --git a/libs/ui/GraphicBufferAllocator.cpp b/libs/ui/GraphicBufferAllocator.cpp
index 0861a1f..1dd59f4 100644
--- a/libs/ui/GraphicBufferAllocator.cpp
+++ b/libs/ui/GraphicBufferAllocator.cpp
@@ -80,7 +80,7 @@
if (rec.size) {
StringAppendF(&result,
"%10p: %7.2f KiB | %4u (%4u) x %4u | %4u | %8X | 0x%" PRIx64 " | %s\n",
- list.keyAt(i), rec.size / 1024.0, rec.width, rec.stride, rec.height,
+ list.keyAt(i), static_cast<double>(rec.size) / 1024.0, rec.width, rec.stride, rec.height,
rec.layerCount, rec.format, rec.usage, rec.requestorName.c_str());
} else {
StringAppendF(&result,
@@ -90,7 +90,7 @@
}
total += rec.size;
}
- StringAppendF(&result, "Total allocated (estimate): %.2f KB\n", total / 1024.0);
+ StringAppendF(&result, "Total allocated (estimate): %.2f KB\n", static_cast<double>(total) / 1024.0);
result.append(mAllocator->dumpDebugInfo());
}
diff --git a/libs/ui/Region.cpp b/libs/ui/Region.cpp
index 55e3b99..1222cd6 100644
--- a/libs/ui/Region.cpp
+++ b/libs/ui/Region.cpp
@@ -339,10 +339,10 @@
size_t count = mStorage.size();
Rect* rects = mStorage.editArray();
while (count) {
- rects->left = static_cast<int32_t>(rects->left * sx + 0.5f);
- rects->right = static_cast<int32_t>(rects->right * sx + 0.5f);
- rects->top = static_cast<int32_t>(rects->top * sy + 0.5f);
- rects->bottom = static_cast<int32_t>(rects->bottom * sy + 0.5f);
+ rects->left = static_cast<int32_t>(static_cast<float>(rects->left) * sx + 0.5f);
+ rects->right = static_cast<int32_t>(static_cast<float>(rects->right) * sx + 0.5f);
+ rects->top = static_cast<int32_t>(static_cast<float>(rects->top) * sy + 0.5f);
+ rects->bottom = static_cast<int32_t>(static_cast<float>(rects->bottom) * sy + 0.5f);
rects++;
count--;
}
diff --git a/libs/ui/include/ui/Size.h b/libs/ui/include/ui/Size.h
index c39d8af..d9b713d 100644
--- a/libs/ui/include/ui/Size.h
+++ b/libs/ui/include/ui/Size.h
@@ -132,7 +132,7 @@
// Otherwise we leverage implicit conversion to safely compare values of
// different types, to ensure we return a value clamped to the range of
// ToType.
- return v < toLowest ? toLowest : (v > toHighest ? toHighest : static_cast<ToType>(v));
+ return v < toLowest ? toLowest : (static_cast<ToType>(v) > toHighest ? toHighest : static_cast<ToType>(v));
}
};