Fix compiler error

Submitted by yyc1992@gmail.com on July 19, 2017, 9:57 p.m.

Details

Message ID 20170719215716.6109-1-yyc1992@gmail.com
State New
Series "Fix compiler error"
Headers show

Commit Message

yyc1992@gmail.com July 19, 2017, 9:57 p.m.
From: Yichao Yu <yyc1992@gmail.com>

There's no overload for `std::abs` or `abs` that takes `unsigned int` as input so this causes
an ambiguity when both `std::abs` and `abs` are available in the namespace.
Explicitly cast the `unsigned int` to `int` to resolve the ambiguity.
---
 backend/src/backend/gen_insn_selection_optimize.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Patch hide | download patch | download mbox

diff --git a/backend/src/backend/gen_insn_selection_optimize.cpp b/backend/src/backend/gen_insn_selection_optimize.cpp
index 2ab2a7f9..8ecc1a74 100644
--- a/backend/src/backend/gen_insn_selection_optimize.cpp
+++ b/backend/src/backend/gen_insn_selection_optimize.cpp
@@ -459,13 +459,13 @@  namespace gbe
             {
               unsigned int s0 = src0.value.ud;
               if (src0.absolute)
-                s0 = abs(s0);
+                s0 = abs((int)s0);
               if (src0.negation)
                 s0 = -s0;
 
               unsigned int s1 = src1.value.ud;
               if (src1.absolute)
-                s1 = abs(s1);
+                s1 = abs((int)s1);
               if (src1.negation)
                 s1 = -s1;
 

Comments

Jan Beich July 20, 2017, 3:50 a.m.
yyc1992@gmail.com writes:

> From: Yichao Yu <yyc1992@gmail.com>
>
> There's no overload for `std::abs` or `abs` that takes `unsigned int` as input so this causes
> an ambiguity when both `std::abs` and `abs` are available in the namespace.
> Explicitly cast the `unsigned int` to `int` to resolve the ambiguity.
[...]
>                unsigned int s0 = src0.value.ud;
>                if (src0.absolute)
> -                s0 = abs(s0);
> +                s0 = abs((int)s0);

Can you add `assert(s0 <= INT_MAX)` before abs() call? Otherwise, a value
between INT_MAX and UINT_MAX may overflow `int` (or `long` with -m32).

$ cat >a.cc
#include <climits>
#include <iostream>

int main()
{
    unsigned int s0 = UINT_MAX;
    s0 = std::abs((int)s0);
    std::cout << s0 << std::endl;
    return 0;
}

$ c++ a.cc
$ ./a.out
1