[gjs] arg: Simplify max safe integer check
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs] arg: Simplify max safe integer check
- Date: Mon, 13 Mar 2017 20:23:33 +0000 (UTC)
commit 8de05187ed99bfc8a7b6a721430e49635286e6e2
Author: Philip Chimento <philip chimento gmail com>
Date: Sat Mar 11 11:51:05 2017 -0800
arg: Simplify max safe integer check
Newer, stricter, compilers, such as VS2013 and GCC on Fedora 26, will warn
about calling std::abs() on an unsigned integer, since it's ambiguous which
signed integer it should convert to.
Now that we've settled on a cross-platform form of the check, it doesn't
actually have to be a template, and can just be done inline. Therefore we
can skip calling std::abs() on the unsigned number.
https://bugzilla.gnome.org/show_bug.cgi?id=779399
gi/arg.cpp | 13 ++++---------
1 files changed, 4 insertions(+), 9 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index b4941a0..5502b00 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -2573,13 +2573,8 @@ gjs_object_from_g_hash (JSContext *context,
return result;
}
-template<typename T>
-static bool
-is_safe_to_store_in_double(T number)
-{
- static T max_safe_int = T(1) << std::numeric_limits<double>::digits;
- return (std::abs(number) <= max_safe_int);
-}
+static const int64_t MAX_SAFE_INT64 =
+ int64_t(1) << std::numeric_limits<double>::digits;
bool
gjs_value_from_g_argument (JSContext *context,
@@ -2616,14 +2611,14 @@ gjs_value_from_g_argument (JSContext *context,
break;
case GI_TYPE_TAG_INT64:
- if (!is_safe_to_store_in_double(arg->v_int64))
+ if (std::abs(arg->v_int64) > MAX_SAFE_INT64)
g_warning("Value %" G_GINT64_FORMAT " cannot be safely stored in "
"a JS Number and may be rounded", arg->v_int64);
value_p.setNumber(static_cast<double>(arg->v_int64));
break;
case GI_TYPE_TAG_UINT64:
- if (!is_safe_to_store_in_double(arg->v_uint64))
+ if (arg->v_uint64 > MAX_SAFE_INT64)
g_warning("Value %" G_GUINT64_FORMAT " cannot be safely stored in "
"a JS Number and may be rounded", arg->v_uint64);
value_p.setNumber(static_cast<double>(arg->v_uint64));
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]