summaryrefslogtreecommitdiffstats
path: root/floatprops.h
diff options
context:
space:
mode:
authorErik Schnetter <schnetter@gmail.com>2013-02-05 11:55:57 -0500
committerErik Schnetter <schnetter@gmail.com>2013-02-05 11:55:57 -0500
commit0cc599b31462e4c0db0d565053147c589face24d (patch)
treee2243371822b11f8c938f0f6f9e5677cba49f74f /floatprops.h
parent6d525adeb65c5300bd748017459252a3f4576392 (diff)
downloadvecmathlib-0cc599b31462e4c0db0d565053147c589face24d.zip
vecmathlib-0cc599b31462e4c0db0d565053147c589face24d.tar.gz
Handle rint() overflow correctly
Diffstat (limited to 'floatprops.h')
-rw-r--r--floatprops.h20
1 files changed, 18 insertions, 2 deletions
diff --git a/floatprops.h b/floatprops.h
index c58dfdd..3c423c1 100644
--- a/floatprops.h
+++ b/floatprops.h
@@ -88,7 +88,15 @@ namespace vecmathlib {
{
static_assert(sizeof std::lrint(x) >= sizeof(int_t),
"lrint() has wrong return type");
- return std::lrint(x);
+ long res = std::lrint(x);
+ if (sizeof std::lrint(x) > sizeof(int_t)) {
+ if (res < std::numeric_limits<int_t>::min() ||
+ res > std::numeric_limits<int_t>::max())
+ {
+ return std::numeric_limits<int_t>::min();
+ }
+ }
+ return res;
}
};
@@ -157,7 +165,15 @@ namespace vecmathlib {
{
static_assert(sizeof std::lrint(x) >= sizeof(int_t),
"lrint() has wrong return type");
- return std::lrint(x);
+ long res = std::lrint(x);
+ if (sizeof std::lrint(x) > sizeof(int_t)) {
+ if (res < std::numeric_limits<int_t>::min() ||
+ res > std::numeric_limits<int_t>::max())
+ {
+ return std::numeric_limits<int_t>::min();
+ }
+ }
+ return res;
}
};
OpenPOWER on IntegriCloud