Skip to content

Commit fabd391

Browse files
authored
Fix usage of char type builtins or mem-functions wrt constexpr-ness (#34, thanks to @mcskatkat) (#38)
Add reporting of various builtin-related macros
1 parent c1c3a57 commit fabd391

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

include/nonstd/string_view.hpp

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,41 @@ using std::operator<<;
299299

300300
// Presence of compiler intrinsics:
301301

302-
#define nssv_HAVE_BUILTIN_VER ( nssv_COMPILER_MSVC_VERSION >= 142 || nssv_COMPILER_GNUC_VERSION > 0 || nssv_COMPILER_CLANG_VERSION >= 400 || nssv_COMPILER_APPLECLANG_VERSION >= 900 )
302+
// Providing char-type specializations for compare() and length() that
303+
// use compiler intrinsics can improve compile- and run-time performance.
304+
//
305+
// The challenge is in using the right combinations of builtin availablity
306+
// and its constexpr-ness.
307+
//
308+
// | compiler | __builtin_memcmp (constexpr) | memcmp (constexpr) |
309+
// |----------|------------------------------|---------------------|
310+
// | clang | 4.0 (>= 4.0 ) | any (? ) |
311+
// | clang-a | 9.0 (>= 9.0 ) | any (? ) |
312+
// | gcc | any (constexpr) | any (? ) |
313+
// | msvc | >= 14.2 (>= 14.2 ) | any (? ) |
314+
315+
#define nssv_HAVE_BUILTIN_VER ( nssv_COMPILER_MSVC_VERSION >= 142 || nssv_COMPILER_GNUC_VERSION > 0 || nssv_COMPILER_CLANG_VERSION >= 400 || nssv_COMPILER_APPLECLANG_VERSION >= 900 )
316+
#define nssv_HAVE_BUILTIN_CE nssv_HAVE_BUILTIN_VER
317+
318+
#define nssv_HAVE_BUILTIN_MEMCMP ( (nssv_HAVE_CONSTEXPR_14 && nssv_HAVE_BUILTIN_CE) || !nssv_HAVE_CONSTEXPR_14 )
319+
#define nssv_HAVE_BUILTIN_STRLEN ( (nssv_HAVE_CONSTEXPR_11 && nssv_HAVE_BUILTIN_CE) || !nssv_HAVE_CONSTEXPR_11 )
303320

304321
#ifdef __has_builtin
305-
#define nssv_HAVE_BUILTIN( x ) __has_builtin( x )
322+
# define nssv_HAVE_BUILTIN( x ) __has_builtin( x )
306323
#else
307-
#define nssv_HAVE_BUILTIN( x ) 0
324+
# define nssv_HAVE_BUILTIN( x ) 0
308325
#endif
309326

310327
#if nssv_HAVE_BUILTIN(__builtin_memcmp) || nssv_HAVE_BUILTIN_VER
311-
#define nssv_BUILTIN_MEMCMP __builtin_memcmp
328+
# define nssv_BUILTIN_MEMCMP __builtin_memcmp
312329
#else
313-
#define nssv_BUILTIN_MEMCMP memcmp
330+
# define nssv_BUILTIN_MEMCMP memcmp
314331
#endif
315332

316333
#if nssv_HAVE_BUILTIN(__builtin_strlen) || nssv_HAVE_BUILTIN_VER
317-
#define nssv_BUILTIN_STRLEN __builtin_strlen
334+
# define nssv_BUILTIN_STRLEN __builtin_strlen
318335
#else
319-
#define nssv_BUILTIN_STRLEN strlen
336+
# define nssv_BUILTIN_STRLEN strlen
320337
#endif
321338

322339
// C++ feature usage:

test/string-view-main.t.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,18 @@ CASE( "presence of C++ library features" "[.stdlibrary]" )
101101
#endif
102102
}
103103

104+
CASE( "usage of compiler intrinsics" "[.intrinsics]" )
105+
{
106+
#if nssv_USES_STD_STRING_VIEW
107+
std::cout << "(Compiler version not available: using std::string_view)\n";
108+
#else
109+
nssv_PRESENT( nssv_HAVE_BUILTIN_VER );
110+
nssv_PRESENT( nssv_HAVE_BUILTIN_CE );
111+
nssv_PRESENT( nssv_HAVE_BUILTIN_MEMCMP );
112+
nssv_PRESENT( nssv_HAVE_BUILTIN_STRLEN );
113+
#endif
114+
}
115+
104116
int main( int argc, char * argv[] )
105117
{
106118
return lest::run( specification(), argc, argv );

0 commit comments

Comments
 (0)