public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH/RFA] libstdc++: provide conversion from day, month to unsigned long, PR99301
@ 2021-02-27 11:42 Hans-Peter Nilsson
  2021-02-27 12:54 ` Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Hans-Peter Nilsson @ 2021-02-27 11:42 UTC (permalink / raw)
  To: gcc-patches, libstdc++, cassio.neri

Since 97d6161f6a7fa712 / r11-7370 "libstdc++: More efficient
days from date" I see an additional 81 testsuite-errors for
cris-elf, with this in g++.log for one randomly picked
regressing test:

FAIL: g++.dg/cpp1y/pr57640.C  -std=c++2a (test for excess errors)
Excess errors:
/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2483:25: error: invalid 'static_cast' from type 'const std::chrono::month' to type 'uint32_t' {aka 'long unsigned int'}
/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2484:25: error: invalid 'static_cast' from type 'const std::chrono::day' to type 'uint32_t' {aka 'long unsigned int'}
/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2496:69: error: no matching function for call to 'std::chrono::duration<long long int, std::ratio<86400> >::duration(<brace-enclosed initializer list>)'

The commit shows conversions to uint32_t, which for
e.g. x86_64-linux is "unsigned int", and there are explicit
conversions to unsigned int for month and day (see patch
context).

But, "newlib ILP32 targets" have an uint32_t that is
effectively typedef'd "long unsigned int" (see
newlib-stdint.h UINT32_TYPE).

Better provide an unsigned long conversion aside the
unsigned ones.

Tested cris-elf.  Pending x86_64-linux regtest results, ok?

libstdc++-v3:
	PR libstdc++/99301
	* include/std/chrono (day, month): Provide
	conversion to unsigned long.
---
 libstdc++-v3/include/std/chrono | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index fcdaee7328ed..33fb0860b41a 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -1334,6 +1334,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator unsigned() const noexcept
       { return _M_d; }
 
+      constexpr explicit
+      operator unsigned long() const noexcept
+      { return _M_d; }
+
       constexpr bool
       ok() const noexcept
       { return 1 <= _M_d && _M_d <= 31; }
@@ -1443,6 +1447,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       operator unsigned() const noexcept
       { return _M_m; }
 
+      explicit constexpr
+      operator unsigned long() const noexcept
+      { return _M_m; }
+
       constexpr bool
       ok() const noexcept
       { return 1 <= _M_m && _M_m <= 12; }
-- 
2.11.0

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH/RFA] libstdc++: provide conversion from day, month to unsigned long, PR99301
  2021-02-27 11:42 [PATCH/RFA] libstdc++: provide conversion from day, month to unsigned long, PR99301 Hans-Peter Nilsson
@ 2021-02-27 12:54 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2021-02-27 12:54 UTC (permalink / raw)
  To: Hans-Peter Nilsson; +Cc: gcc-patches, libstdc++, cassio.neri

[-- Attachment #1: Type: text/plain, Size: 1496 bytes --]

On 27/02/21 12:42 +0100, Hans-Peter Nilsson via Libstdc++ wrote:
>Since 97d6161f6a7fa712 / r11-7370 "libstdc++: More efficient
>days from date" I see an additional 81 testsuite-errors for
>cris-elf, with this in g++.log for one randomly picked
>regressing test:
>
>FAIL: g++.dg/cpp1y/pr57640.C  -std=c++2a (test for excess errors)
>Excess errors:
>/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2483:25: error: invalid 'static_cast' from type 'const std::chrono::month' to type 'uint32_t' {aka 'long unsigned int'}
>/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2484:25: error: invalid 'static_cast' from type 'const std::chrono::day' to type 'uint32_t' {aka 'long unsigned int'}
>/x/gccobj/cris-elf/libstdc++-v3/include/chrono:2496:69: error: no matching function for call to 'std::chrono::duration<long long int, std::ratio<86400> >::duration(<brace-enclosed initializer list>)'
>
>The commit shows conversions to uint32_t, which for
>e.g. x86_64-linux is "unsigned int", and there are explicit
>conversions to unsigned int for month and day (see patch
>context).
>
>But, "newlib ILP32 targets" have an uint32_t that is
>effectively typedef'd "long unsigned int" (see
>newlib-stdint.h UINT32_TYPE).
>
>Better provide an unsigned long conversion aside the
>unsigned ones.

No, the allowed conversions are specific by the standard. The right
fix is to convert to unsigned explicitly.

I've pushed the attached patch after testing on x86_64-linux. This
should work for newlib ILP32 targets too.


[-- Attachment #2: patch.txt --]
[-- Type: text/x-patch, Size: 1369 bytes --]

commit 699672d4dccfb5579dbe48977bda86f6836225a0
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Sat Feb 27 12:50:53 2021

    libstdc++: Fix conversions from date types to integers [PR 99301]
    
    The conversions to integer types are explicit, so need to use the
    correct type. Converting to uint32_t only works if that is the same type
    as unsigned.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/99301
            * include/std/chrono (year_month_day::_M_days_since_epoch()):
            Convert chrono::month and chrono::day to unsigned before
            converting to uint32_t.

diff --git a/libstdc++-v3/include/std/chrono b/libstdc++-v3/include/std/chrono
index fcdaee7328e..11729aae708 100644
--- a/libstdc++-v3/include/std/chrono
+++ b/libstdc++-v3/include/std/chrono
@@ -2496,8 +2496,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
       auto constexpr __r2_e3 = static_cast<uint32_t>(536895458);
 
       const auto __y1 = static_cast<uint32_t>(static_cast<int>(_M_y)) - __z2;
-      const auto __m1 = static_cast<uint32_t>(_M_m);
-      const auto __d1 = static_cast<uint32_t>(_M_d);
+      const auto __m1 = static_cast<uint32_t>(static_cast<unsigned>(_M_m));
+      const auto __d1 = static_cast<uint32_t>(static_cast<unsigned>(_M_d));
 
       const auto __j  = static_cast<uint32_t>(__m1 < 3);
       const auto __y0 = __y1 - __j;

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-02-27 12:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-27 11:42 [PATCH/RFA] libstdc++: provide conversion from day, month to unsigned long, PR99301 Hans-Peter Nilsson
2021-02-27 12:54 ` Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).