Skip to content

Commit 21e2bdc

Browse files
committed
Fixed std::bit_ceil, made it behave the same as C23 stdc_bit_ceil
1 parent e1a11b1 commit 21e2bdc

File tree

2 files changed

+7
-22
lines changed
  • src/libcxx/include
  • test/standalone/stdbit/src

2 files changed

+7
-22
lines changed

src/libcxx/include/bit

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ int bit_width(_Tp __t) noexcept;
289289

290290
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
291291
int bit_width(unsigned char __t) noexcept {
292-
return std::numeric_limits<unsigned char>::digits- __ez80_clzc(__t);
292+
return std::numeric_limits<unsigned char>::digits - __ez80_clzc(__t);
293293
}
294294

295295
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
@@ -326,44 +326,32 @@ _Tp bit_ceil(_Tp __t) noexcept;
326326

327327
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
328328
unsigned char bit_ceil(unsigned char __t) noexcept {
329-
return
330-
(__t <= static_cast<unsigned char>(std::numeric_limits<signed char>::min()))
331-
? ((__t != 0) ? (static_cast<unsigned char>(1) << bit_width<unsigned char>(__t - 1)) : 1) : 0;
329+
return ((__t < 2) ? 1 : (static_cast<unsigned char>(2) << (bit_width<unsigned char>(__t - 1) - 1)));
332330
}
333331

334332
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
335333
unsigned short bit_ceil(unsigned short __t) noexcept {
336-
return
337-
(__t <= static_cast<unsigned short>(std::numeric_limits<signed short>::min()))
338-
? ((__t != 0) ? (static_cast<unsigned short>(1) << bit_width<unsigned short>(__t - 1)) : 1) : 0;
334+
return ((__t < 2) ? 1 : (static_cast<unsigned short>(2) << (bit_width<unsigned short>(__t - 1) - 1)));
339335
}
340336

341337
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
342338
unsigned int bit_ceil(unsigned int __t) noexcept {
343-
return
344-
(__t <= static_cast<unsigned int>(std::numeric_limits<signed int>::min()))
345-
? ((__t != 0) ? (static_cast<unsigned int>(1) << bit_width<unsigned int>(__t - 1)) : 1) : 0;
339+
return ((__t < 2) ? 1 : (static_cast<unsigned int>(2) << (bit_width<unsigned int>(__t - 1) - 1)));
346340
}
347341

348342
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
349343
unsigned long bit_ceil(unsigned long __t) noexcept {
350-
return
351-
(__t <= static_cast<unsigned long>(std::numeric_limits<signed long>::min()))
352-
? ((__t != 0) ? (static_cast<unsigned long>(1) << bit_width<unsigned long>(__t - 1)) : 1) : 0;
344+
return ((__t < 2) ? 1 : (static_cast<unsigned long>(2) << (bit_width<unsigned long>(__t - 1) - 1)));
353345
}
354346

355347
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
356348
unsigned __int48 bit_ceil(unsigned __int48 __t) noexcept {
357-
return
358-
(__t <= static_cast<unsigned __int48>(std::numeric_limits<signed __int48>::min()))
359-
? ((__t != 0) ? (static_cast<unsigned __int48>(1) << bit_width<unsigned __int48>(__t - 1)) : 1) : 0;
349+
return ((__t < 2) ? 1 : (static_cast<unsigned __int48>(2) << (bit_width<unsigned __int48>(__t - 1) - 1)));
360350
}
361351

362352
template <> _EZCXX_NODISCARD _EZCXX_INLINE constexpr
363353
unsigned long long bit_ceil(unsigned long long __t) noexcept {
364-
return
365-
(__t <= static_cast<unsigned long long>(std::numeric_limits<signed long long>::min()))
366-
? ((__t != 0) ? (static_cast<unsigned long long>(1) << bit_width<unsigned long long>(__t - 1)) : 1) : 0;
354+
return ((__t < 2) ? 1 : (static_cast<unsigned long long>(2) << (bit_width<unsigned long long>(__t - 1) - 1)));
367355
}
368356

369357
//------------------------------------------------------------------------------

test/standalone/stdbit/src/main.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,15 +349,12 @@ int test_bit_ceil(void) {
349349
C((std::bit_ceil( one_u32) == one_u32));
350350
C((std::bit_ceil( one_u48) == one_u48));
351351
C((std::bit_ceil( one_u64) == one_u64));
352-
// optional undefined behaviour
353-
#if 0
354352
C((std::bit_ceil(umax_u8 ) == zero_u8 ));
355353
C((std::bit_ceil(umax_u16) == zero_u16));
356354
C((std::bit_ceil(umax_u24) == zero_u24));
357355
C((std::bit_ceil(umax_u32) == zero_u32));
358356
C((std::bit_ceil(umax_u48) == zero_u48));
359357
C((std::bit_ceil(umax_u64) == zero_u64));
360-
#endif
361358
C((std::bit_ceil(smax_u8 ) == smin_u8 ));
362359
C((std::bit_ceil(smax_u16) == smin_u16));
363360
C((std::bit_ceil(smax_u24) == smin_u24));

0 commit comments

Comments
 (0)