From 60e537a026c037d8f3e5678acd148f9e44b4fb9c Mon Sep 17 00:00:00 2001 From: Tom de Vries Date: Fri, 11 Sep 2020 05:40:36 +0200 Subject: [PATCH] [nvptx] Fix printing of 128-bit constant (negative case) For this code: ... __int128 min_one = -1; ... we currently generate: ... .visible .global .align 8 .u64 min_one[2] = { -1, 0 }; ... Fix this in nvptx_assemble_value, such that we have instead: ... .visible .global .align 8 .u64 min_one[2] = { -1, -1 }; ... gcc/ChangeLog: * config/nvptx/nvptx.c (nvptx_assemble_value): Handle negative __int128. gcc/testsuite/ChangeLog: * gcc.target/nvptx/int128.c: New test. --- gcc/config/nvptx/nvptx.c | 5 ++++- gcc/testsuite/gcc.target/nvptx/int128.c | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.target/nvptx/int128.c diff --git a/gcc/config/nvptx/nvptx.c b/gcc/config/nvptx/nvptx.c index ffcbe59..4fca0ed 100644 --- a/gcc/config/nvptx/nvptx.c +++ b/gcc/config/nvptx/nvptx.c @@ -2050,13 +2050,16 @@ output_init_frag (rtx sym) static void nvptx_assemble_value (unsigned HOST_WIDE_INT val, unsigned size) { + bool negative_p + = val & (HOST_WIDE_INT_1U << (HOST_BITS_PER_WIDE_INT - 1)); + val &= ((unsigned HOST_WIDE_INT)2 << (size * BITS_PER_UNIT - 1)) - 1; for (unsigned part = 0; size; size -= part) { if (part * BITS_PER_UNIT == HOST_BITS_PER_WIDE_INT) /* Avoid undefined behaviour. */ - val = 0; + val = negative_p ? -1 : 0; else val >>= (part * BITS_PER_UNIT); part = init_frag.size - init_frag.offset; diff --git a/gcc/testsuite/gcc.target/nvptx/int128.c b/gcc/testsuite/gcc.target/nvptx/int128.c new file mode 100644 index 0000000..069dbbf --- /dev/null +++ b/gcc/testsuite/gcc.target/nvptx/int128.c @@ -0,0 +1,15 @@ +/* { dg-do run } */ +/* { dg-additional-options "-Wno-pedantic" } */ + +__int128 one = 1; +__int128 min_one = -1; +__int128 zero = 0; + +int +main (void) +{ + if (zero - one != min_one) + __builtin_abort (); + + return 0; +} -- 2.7.4