[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/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));
}
};