Skip to content

Commit cc9ca38

Browse files
authored
Fix C++ version detection for MSVC (#1342)
* Fix C++ version detection for MSVC * Test C++ version in MSVC
1 parent 913188e commit cc9ca38

9 files changed

Lines changed: 75 additions & 25 deletions

File tree

.github/workflows/cxx-versions.yml

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,46 @@ on: [push, pull_request]
33
concurrency:
44
group: ${{ github.workflow }}-${{ github.job }}-${{ github.ref }}
55
cancel-in-progress: true
6+
67
jobs:
7-
build:
8+
build-unix:
9+
name: 'Unix C++${{ matrix.cxx-version }}'
810
runs-on: ubuntu-latest
911
strategy:
1012
matrix:
1113
cxx-version: [14, 17, 20]
1214
steps:
1315
- uses: actions/checkout@v6
1416
- name: Setup
15-
run: cmake -B _build -DBUILD_TESTS=ON -DDOWNLOAD_DOCTEST=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=${{matrix.cxx-version}}
17+
run: cmake -B build/ -DBUILD_TESTS=ON -DDOWNLOAD_DOCTEST=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=${{matrix.cxx-version}}
18+
- name: Build
19+
run: cmake --build build/
20+
- name: Test
21+
run: ./build/test/test_xsimd
22+
23+
build-msvc:
24+
name: 'MSVC C++${{ matrix.cxx-version }}'
25+
defaults:
26+
run:
27+
shell: bash {0}
28+
runs-on: windows-2022
29+
strategy:
30+
matrix:
31+
cxx-version: [14, 17, 20]
32+
steps:
33+
- name: Setup compiler
34+
uses: ilammy/msvc-dev-cmd@v1
35+
with:
36+
arch: amd64
37+
- name: Setup Ninja
38+
run: |
39+
python3 -m pip install --upgrade pip setuptools wheel
40+
python3 -m pip install ninja
41+
- uses: actions/checkout@v6
42+
- name: Setup
43+
run: cmake -B build/ -DBUILD_TESTS=ON -DDOWNLOAD_DOCTEST=ON -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_STANDARD=${{matrix.cxx-version}} -G Ninja
1644
- name: Build
17-
run: cmake --build _build
45+
run: cmake --build build/
46+
- name: Test
47+
run: ./build/test/test_xsimd
1848

include/xsimd/arch/common/xsimd_common_bit.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
#ifndef XSIMD_BIT_HPP
66
#define XSIMD_BIT_HPP
77

8-
#if __cplusplus > 202002L
8+
#include "../../config/xsimd_config.hpp"
9+
10+
#if XSIMD_CPP_VERSION > 202002L
911

1012
#include <version>
1113

include/xsimd/config/xsimd_config.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,19 @@
2727
#define XSIMD_LITTLE_ENDIAN
2828
#endif
2929

30+
/**
31+
* @ingroup xsimd_config_macro
32+
*
33+
* Normalized C++ version number, equivalent to __cplusplus but also works with
34+
* MSVC which requires /Zc:__cplusplus to set it correctly (otherwise it's always
35+
* 199711L). Use this instead of __cplusplus throughout the codebase.
36+
*/
37+
#if defined(_MSC_VER) && !defined(__clang__)
38+
#define XSIMD_CPP_VERSION _MSVC_LANG
39+
#else
40+
#define XSIMD_CPP_VERSION __cplusplus
41+
#endif
42+
3043
/**
3144
* high level free functions
3245
*

include/xsimd/config/xsimd_cpu_features_x86.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include <cstdint>
1818
#include <cstring>
1919
#include <type_traits>
20-
#if __cplusplus >= 201703L
20+
#if XSIMD_CPP_VERSION >= 201703L
2121
#include <string_view>
2222
#endif
2323

@@ -249,7 +249,7 @@ namespace xsimd
249249
return m_manufacturer_id;
250250
}
251251

252-
#if __cplusplus >= 201703L
252+
#if XSIMD_CPP_VERSION >= 201703L
253253
constexpr std::string_view manufacturer_id() const noexcept
254254
{
255255
return { m_manufacturer_id.data(), m_manufacturer_id.size() };
@@ -816,7 +816,7 @@ namespace xsimd
816816
return leaf0().manufacturer_id_raw();
817817
}
818818

819-
#if __cplusplus >= 201703L
819+
#if XSIMD_CPP_VERSION >= 201703L
820820
inline std::string_view manufacturer_id() const noexcept
821821
{
822822
return leaf0().manufacturer_id();

include/xsimd/config/xsimd_macros.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#ifndef XSIMD_MACROS_HPP
1313
#define XSIMD_MACROS_HPP
1414

15+
#include "./xsimd_config.hpp"
16+
1517
#if defined(__VEC__)
1618
#define XSIMD_INLINE inline
1719
#elif defined __has_attribute
@@ -43,7 +45,7 @@
4345
#endif
4446
#endif
4547

46-
#if !defined(XSIMD_NO_DISCARD) && __cplusplus >= 201703L
48+
#if !defined(XSIMD_NO_DISCARD) && XSIMD_CPP_VERSION >= 201703L
4749
// this means that the previous tests failed, but we are using C++17 or higher
4850
#define XSIMD_NO_DISCARD [[nodiscard]]
4951
#endif
@@ -63,7 +65,7 @@
6365
#define XSIMD_IF_CONSTEXPR if constexpr
6466
#endif
6567

66-
#if !defined(XSIMD_IF_CONSTEXPR) && __cplusplus >= 201703L
68+
#if !defined(XSIMD_IF_CONSTEXPR) && XSIMD_CPP_VERSION >= 201703L
6769
// this means that the previous test failed, but we are using C++17 or higher
6870
#define XSIMD_IF_CONSTEXPR if constexpr
6971
#endif

include/xsimd/types/xsimd_batch.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <complex>
1717

1818
#include "../config/xsimd_arch.hpp"
19+
#include "../config/xsimd_config.hpp"
1920
#include "../config/xsimd_macros.hpp"
2021
#include "../memory/xsimd_alignment.hpp"
2122
#include "./xsimd_batch_fwd.hpp"
@@ -283,7 +284,7 @@ namespace xsimd
283284
XSIMD_INLINE batch logical_or(batch const& other) const noexcept;
284285
};
285286

286-
#if __cplusplus < 201703L
287+
#if XSIMD_CPP_VERSION < 201703L
287288
template <class T, class A>
288289
constexpr std::size_t batch<T, A>::size;
289290
#endif
@@ -363,7 +364,7 @@ namespace xsimd
363364
static XSIMD_INLINE register_type make_register(std::index_sequence<>, V... v) noexcept;
364365
};
365366

366-
#if __cplusplus < 201703L
367+
#if XSIMD_CPP_VERSION < 201703L
367368
template <class T, class A>
368369
constexpr std::size_t batch_bool<T, A>::size;
369370
#endif
@@ -509,7 +510,7 @@ namespace xsimd
509510
real_batch m_imag;
510511
};
511512

512-
#if __cplusplus < 201703L
513+
#if XSIMD_CPP_VERSION < 201703L
513514
template <class T, class A>
514515
constexpr std::size_t batch<std::complex<T>, A>::size;
515516
#endif

include/xsimd/types/xsimd_batch_constant.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <functional>
1717
#include <utility>
1818

19+
#include "../config/xsimd_config.hpp"
1920
#include "./xsimd_batch.hpp"
2021
#include "./xsimd_utils.hpp"
2122

@@ -408,7 +409,7 @@ namespace xsimd
408409
return {};
409410
}
410411

411-
#if __cplusplus >= 202002L
412+
#if XSIMD_CPP_VERSION >= 202002L
412413
template <std::array Arr, class A, std::size_t... Is>
413414
XSIMD_INLINE constexpr batch_constant<typename decltype(Arr)::value_type, A, Arr[Is]...>
414415
make_batch_constant(std::index_sequence<Is...>) noexcept
@@ -431,7 +432,7 @@ namespace xsimd
431432
return {};
432433
}
433434

434-
#if __cplusplus >= 202002L
435+
#if XSIMD_CPP_VERSION >= 202002L
435436
template <typename T, std::array Arr, class A, std::size_t... Is>
436437
XSIMD_INLINE constexpr batch_bool_constant<T, A, Arr[Is]...>
437438
make_batch_bool_constant(std::index_sequence<Is...>) noexcept
@@ -497,7 +498,7 @@ namespace xsimd
497498
return {};
498499
}
499500

500-
#if __cplusplus >= 202002L
501+
#if XSIMD_CPP_VERSION >= 202002L
501502
/**
502503
* @brief Build a @c batch_constant from a std::array (C++20)
503504
*
@@ -524,7 +525,7 @@ namespace xsimd
524525
return {};
525526
}
526527

527-
#if __cplusplus >= 202002L
528+
#if XSIMD_CPP_VERSION >= 202002L
528529
/**
529530
* @brief Build a @c batch_constant from a std::array of boolean (C++20)
530531
*

include/xsimd/types/xsimd_traits.hpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <xtl/xcomplex.hpp>
2121
#endif
2222

23+
#include "../config/xsimd_config.hpp"
2324
#include "./xsimd_batch_fwd.hpp"
2425
#include "./xsimd_utils.hpp"
2526

@@ -60,7 +61,7 @@ namespace xsimd
6061
static constexpr size_t size = 1;
6162
};
6263

63-
#if __cplusplus < 201703L
64+
#if XSIMD_CPP_VERSION < 201703L
6465
template <class T>
6566
constexpr size_t simd_traits_impl<T, false>::size;
6667
#endif
@@ -73,7 +74,7 @@ namespace xsimd
7374
static constexpr size_t size = type::size;
7475
};
7576

76-
#if __cplusplus < 201703L
77+
#if XSIMD_CPP_VERSION < 201703L
7778
template <class T>
7879
constexpr size_t simd_traits_impl<T, true>::size;
7980
#endif
@@ -139,7 +140,7 @@ namespace xsimd
139140
static constexpr size_t size = simd_traits<type>::size;
140141
};
141142

142-
#if __cplusplus < 201703L
143+
#if XSIMD_CPP_VERSION < 201703L
143144
template <class T>
144145
constexpr size_t revert_simd_traits<T>::size;
145146
#endif
@@ -151,7 +152,7 @@ namespace xsimd
151152
static constexpr size_t size = batch<T>::size;
152153
};
153154

154-
#if __cplusplus < 201703L
155+
#if XSIMD_CPP_VERSION < 201703L
155156
template <class T>
156157
constexpr size_t revert_simd_traits<batch<T>>::size;
157158
#endif
@@ -257,7 +258,7 @@ namespace xsimd
257258
static constexpr bool is_complex = detail::is_complex<T>::value; ///< True if T is complex or a batch of complex values.
258259
};
259260

260-
#if __cplusplus < 201703L
261+
#if XSIMD_CPP_VERSION < 201703L
261262
template <class T>
262263
constexpr bool batch_traits<T>::is_batch;
263264
template <class T>
@@ -280,7 +281,7 @@ namespace xsimd
280281
static constexpr bool is_complex = detail::is_complex<T>::value;
281282
};
282283

283-
#if __cplusplus < 201703L
284+
#if XSIMD_CPP_VERSION < 201703L
284285
template <class T, class A>
285286
constexpr bool batch_traits<batch<T, A>>::is_batch;
286287
template <class T, class A>
@@ -303,7 +304,7 @@ namespace xsimd
303304
static constexpr bool is_complex = false;
304305
};
305306

306-
#if __cplusplus < 201703L
307+
#if XSIMD_CPP_VERSION < 201703L
307308
template <class T, class A>
308309
constexpr bool batch_traits<batch_bool<T, A>>::is_batch;
309310
template <class T, class A>

test/test_batch_constant.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct constant_batch_test
4747

4848
void test_init_from_array() const
4949
{
50-
#if __cplusplus >= 202002L
50+
#if XSIMD_CPP_VERSION >= 202002L
5151
constexpr array_type expected = []()
5252
{
5353
array_type out = {};
@@ -285,7 +285,7 @@ struct constant_bool_batch_test
285285

286286
void test_init_from_array() const
287287
{
288-
#if __cplusplus >= 202002L
288+
#if XSIMD_CPP_VERSION >= 202002L
289289
constexpr bool_array_type expected = []()
290290
{
291291
bool_array_type out = {};

0 commit comments

Comments
 (0)