Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++][concepts] Implements concept helper __libcpp_integer #78086

Conversation

H-G-Hristov
Copy link
Contributor

...and tests.

@H-G-Hristov H-G-Hristov requested a review from a team as a code owner January 14, 2024 07:35
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jan 14, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 14, 2024

@llvm/pr-subscribers-libcxx

Author: Hristo Hristov (H-G-Hristov)

Changes

...and tests.


Full diff: https://github.com/llvm/llvm-project/pull/78086.diff

2 Files Affected:

  • (modified) libcxx/include/__concepts/arithmetic.h (+4)
  • (added) libcxx/test/libcxx/concepts/concepts.arithmetic/arithmetic.compile.pass.cpp (+118)
diff --git a/libcxx/include/__concepts/arithmetic.h b/libcxx/include/__concepts/arithmetic.h
index f41e4c9f2747cc..0c44f117805f36 100644
--- a/libcxx/include/__concepts/arithmetic.h
+++ b/libcxx/include/__concepts/arithmetic.h
@@ -42,9 +42,13 @@ concept floating_point = is_floating_point_v<_Tp>;
 
 template <class _Tp>
 concept __libcpp_unsigned_integer = __libcpp_is_unsigned_integer<_Tp>::value;
+
 template <class _Tp>
 concept __libcpp_signed_integer = __libcpp_is_signed_integer<_Tp>::value;
 
+template <class _Tp>
+concept __libcpp_integer = __libcpp_unsigned_integer<_Tp> || __libcpp_signed_integer<_Tp>;
+
 #endif // _LIBCPP_STD_VER >= 20
 
 _LIBCPP_END_NAMESPACE_STD
diff --git a/libcxx/test/libcxx/concepts/concepts.arithmetic/arithmetic.compile.pass.cpp b/libcxx/test/libcxx/concepts/concepts.arithmetic/arithmetic.compile.pass.cpp
new file mode 100644
index 00000000000000..e234615f544133
--- /dev/null
+++ b/libcxx/test/libcxx/concepts/concepts.arithmetic/arithmetic.compile.pass.cpp
@@ -0,0 +1,118 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+#include <concepts>
+#include <numbers>
+
+struct SomeObject {};
+
+// Concept helpers for the internal type traits for the fundamental types.
+
+// template <class _Tp>
+// concept __libcpp_unsigned_integer;
+
+// Unsigned
+static_assert(std::__libcpp_unsigned_integer<unsigned char>);
+static_assert(std::__libcpp_unsigned_integer<unsigned short int>);
+static_assert(std::__libcpp_unsigned_integer<unsigned int>);
+static_assert(std::__libcpp_unsigned_integer<unsigned long int>);
+static_assert(std::__libcpp_unsigned_integer<unsigned long long int>);
+static_assert(std::__libcpp_unsigned_integer<unsigned short int>);
+#ifndef _LIBCPP_HAS_NO_INT128
+static_assert(std::__libcpp_unsigned_integer<__uint128_t>);
+#endif
+// Signed
+static_assert(!std::__libcpp_unsigned_integer<signed char>);
+static_assert(!std::__libcpp_unsigned_integer<short int>);
+static_assert(!std::__libcpp_unsigned_integer<int>);
+static_assert(!std::__libcpp_unsigned_integer<long int>);
+static_assert(!std::__libcpp_unsigned_integer<long long int>);
+static_assert(!std::__libcpp_unsigned_integer<short int>);
+#ifndef _LIBCPP_HAS_NO_INT128
+static_assert(!std::__libcpp_unsigned_integer<__int128_t>);
+#endif
+// Non-integer
+static_assert(!std::__libcpp_unsigned_integer<bool>);
+static_assert(!std::__libcpp_unsigned_integer<char8_t>);
+static_assert(!std::__libcpp_unsigned_integer<char16_t>);
+static_assert(!std::__libcpp_unsigned_integer<char32_t>);
+static_assert(!std::__libcpp_unsigned_integer<float>);
+static_assert(!std::__libcpp_unsigned_integer<double>);
+static_assert(!std::__libcpp_unsigned_integer<long double>);
+static_assert(!std::__libcpp_unsigned_integer<void>);
+static_assert(!std::__libcpp_unsigned_integer<SomeObject>);
+
+// template <class _Tp>
+// concept __libcpp_signed_integer;
+
+// Unsigned
+static_assert(!std::__libcpp_signed_integer<unsigned char>);
+static_assert(!std::__libcpp_signed_integer<unsigned short int>);
+static_assert(!std::__libcpp_signed_integer<unsigned int>);
+static_assert(!std::__libcpp_signed_integer<unsigned long int>);
+static_assert(!std::__libcpp_signed_integer<unsigned long long int>);
+static_assert(!std::__libcpp_signed_integer<unsigned short int>);
+#ifndef _LIBCPP_HAS_NO_INT128
+static_assert(!std::__libcpp_signed_integer<__uint128_t>);
+#endif
+// Signed
+static_assert(std::__libcpp_signed_integer<signed char>);
+static_assert(std::__libcpp_signed_integer<short int>);
+static_assert(std::__libcpp_signed_integer<int>);
+static_assert(std::__libcpp_signed_integer<long int>);
+static_assert(std::__libcpp_signed_integer<long long int>);
+static_assert(std::__libcpp_signed_integer<short int>);
+#ifndef _LIBCPP_HAS_NO_INT128
+static_assert(std::__libcpp_signed_integer<__int128_t>);
+#endif
+// Non-integer
+static_assert(!std::__libcpp_signed_integer<bool>);
+static_assert(!std::__libcpp_signed_integer<char8_t>);
+static_assert(!std::__libcpp_signed_integer<char16_t>);
+static_assert(!std::__libcpp_signed_integer<char32_t>);
+static_assert(!std::__libcpp_signed_integer<float>);
+static_assert(!std::__libcpp_signed_integer<double>);
+static_assert(!std::__libcpp_signed_integer<long double>);
+static_assert(!std::__libcpp_signed_integer<void>);
+static_assert(!std::__libcpp_signed_integer<SomeObject>);
+
+// template <class _Tp>
+// concept __libcpp_integer;
+
+// Unsigned
+static_assert(std::__libcpp_integer<unsigned char>);
+static_assert(std::__libcpp_integer<unsigned short int>);
+static_assert(std::__libcpp_integer<unsigned int>);
+static_assert(std::__libcpp_integer<unsigned long int>);
+static_assert(std::__libcpp_integer<unsigned long long int>);
+static_assert(std::__libcpp_integer<unsigned short int>);
+#ifndef _LIBCPP_HAS_NO_INT128
+static_assert(std::__libcpp_integer<__uint128_t>);
+#endif
+// Signed
+static_assert(std::__libcpp_integer<signed char>);
+static_assert(std::__libcpp_integer<short int>);
+static_assert(std::__libcpp_integer<int>);
+static_assert(std::__libcpp_integer<long int>);
+static_assert(std::__libcpp_integer<long long int>);
+static_assert(std::__libcpp_integer<short int>);
+#ifndef _LIBCPP_HAS_NO_INT128
+static_assert(std::__libcpp_integer<__int128_t>);
+#endif
+// Non-integer
+static_assert(!std::__libcpp_integer<bool>);
+static_assert(!std::__libcpp_integer<char8_t>);
+static_assert(!std::__libcpp_integer<char16_t>);
+static_assert(!std::__libcpp_integer<char32_t>);
+static_assert(!std::__libcpp_integer<float>);
+static_assert(!std::__libcpp_integer<double>);
+static_assert(!std::__libcpp_integer<long double>);
+static_assert(!std::__libcpp_integer<void>);
+static_assert(!std::__libcpp_integer<SomeObject>);

@H-G-Hristov
Copy link
Contributor Author

H-G-Hristov commented Jan 14, 2024

@mordante @frederick-vs-ja This adds std::__libcpp_integer concept as a prerequisite from this discussion: #77967 (comment)

@H-G-Hristov H-G-Hristov force-pushed the hgh/libcxx/concepts-added-__libcxx_is_integer-concept branch from 4ab449d to f2008de Compare January 14, 2024 08:06
@Zingam Zingam requested a review from mordante January 14, 2024 12:08
Copy link
Member

@mordante mordante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mordante @frederick-vs-ja This adds std::__libcpp_integer concept as a prerequisite from this discussion: #77967 (comment)

* Any ideas for an alternative name, e.g.
  
  * `std::__libcpp_true_integer`
  * `std::__libcpp_integer_type`

* I have not replaced `__is_safe_integral_cmp` in this patch as suggested [[libc++][numeric] P0543R3: Saturation arithmetic #77967 (comment)](https://github.com/llvm/llvm-project/pull/77967#discussion_r1451099830). I can do it in a follow-up patch.

Thanks. I would keep the name as is. Why do you want to provide different names.

LGTM modulo some small nits.

@H-G-Hristov
Copy link
Contributor Author

@mordante @frederick-vs-ja This adds std::__libcpp_integer concept as a prerequisite from this discussion: #77967 (comment)

* Any ideas for an alternative name, e.g.
  
  * `std::__libcpp_true_integer`
  * `std::__libcpp_integer_type`

* I have not replaced `__is_safe_integral_cmp` in this patch as suggested [[libc++][numeric] P0543R3: Saturation arithmetic #77967 (comment)](https://github.com/llvm/llvm-project/pull/77967#discussion_r1451099830). I can do it in a follow-up patch.

Thanks. I would keep the name as is. Why do you want to provide different names.

Thank you! Never mind the question then.

@H-G-Hristov
Copy link
Contributor Author

86\test\std\utilities\format\format.functions\Output\formatted_size.pass.cpp.dir\t.tmp.exe'
# .---command stderr------------
# | Traceback (most recent call last):
# |   File "C:\ws\src\libcxx\utils\run.py", line 72, in <module>
# |     exit(main())
# |   File "C:\ws\src\libcxx\utils\run.py", line 68, in main
# |     return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
# |   File "c:\python39\lib\subprocess.py", line 349, in call
# |     with Popen(*popenargs, **kwargs) as p:
# |   File "c:\python39\lib\subprocess.py", line 951, in __init__
# |     self._execute_child(args, executable, preexec_fn, close_fds,
# |   File "c:\python39\lib\subprocess.py", line 1420, in _execute_child
# |     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
# | OSError: [WinError 225] Operation did not complete successfully because the file contains a virus or potentially unwanted 

@mordante
Copy link
Member

86\test\std\utilities\format\format.functions\Output\formatted_size.pass.cpp.dir\t.tmp.exe'
# .---command stderr------------
# | Traceback (most recent call last):
# |   File "C:\ws\src\libcxx\utils\run.py", line 72, in <module>
# |     exit(main())
# |   File "C:\ws\src\libcxx\utils\run.py", line 68, in main
# |     return subprocess.call(commandLine, cwd=args.execdir, env=env, shell=False)
# |   File "c:\python39\lib\subprocess.py", line 349, in call
# |     with Popen(*popenargs, **kwargs) as p:
# |   File "c:\python39\lib\subprocess.py", line 951, in __init__
# |     self._execute_child(args, executable, preexec_fn, close_fds,
# |   File "c:\python39\lib\subprocess.py", line 1420, in _execute_child
# |     hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
# | OSError: [WinError 225] Operation did not complete successfully because the file contains a virus or potentially unwanted 

I've seen that happen before :-/

Copy link
Member

@mordante mordante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks! Please ping me when you've update your other patch with these changes.

@H-G-Hristov
Copy link
Contributor Author

LGTM! Thanks! Please ping me when you've update your other patch with these changes.

Thank you! I'll ping you when I am ready with the other patch. I still need to implement additional tests.

@Zingam Zingam merged commit bddd8f4 into llvm:main Jan 14, 2024
45 checks passed
@H-G-Hristov H-G-Hristov deleted the hgh/libcxx/concepts-added-__libcxx_is_integer-concept branch January 14, 2024 22:06
Zingam added a commit that referenced this pull request Jan 22, 2024
Implements: https://wg21.link/P0543R3
- https://eel.is/c++draft/numeric.sat

Additional references:
- Division: https://eel.is/c++draft/expr.mul#4
- Arithmetic conversions: https://eel.is/c++draft/expr.arith.conv#1
- Clang builtins:
https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions

Depends on: #78086

---------

Co-authored-by: Zingam <zingam@outlook.com>
Co-authored-by: Mark de Wever <zar-rpg@xs4all.nl>
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
…#78086)

...and tests.

---------

Co-authored-by: Zingam <zingam@outlook.com>
blueboxd pushed a commit to blueboxd/libcxx that referenced this pull request Feb 7, 2024
Implements: https://wg21.link/P0543R3
- https://eel.is/c++draft/numeric.sat

Additional references:
- Division: https://eel.is/c++draft/expr.mul#4
- Arithmetic conversions: https://eel.is/c++draft/expr.arith.conv#1
- Clang builtins:
https://clang.llvm.org/docs/LanguageExtensions.html#builtin-functions

Depends on: llvm/llvm-project#78086

---------

Co-authored-by: Zingam <zingam@outlook.com>
Co-authored-by: Mark de Wever <zar-rpg@xs4all.nl>
NOKEYCHECK=True
GitOrigin-RevId: 03c19e91e8d8cb706b58e02d69f80caeaf7eb0f4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants