Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
<complex>: Force aligning to the total size of both components in com…
Browse files Browse the repository at this point in the history
…plex<T>
  • Loading branch information
wmaxey committed Apr 27, 2021
1 parent 4b1a1df commit 3896f6b
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <cuda/std/complex>

// template<class T>
// class complex
// {
// public:
// typedef T value_type;
// ...
// };

#include <cuda/std/complex>
#include <cuda/std/type_traits>

#include "test_macros.h"

template <class T>
__host__ __device__ void
test()
{
typedef cuda::std::complex<T> C;

static_assert(sizeof(C) == (sizeof(T)*2), "wrong size");
static_assert(alignof(C) == (alignof(T)*2), "misaligned");
}

int main(int, char**)
{
test<float>();
test<double>();
// CUDA treats long double as double
// test<long double>();

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <cuda/std/complex>

// template<class T>
// class complex
// {
// public:
// typedef T value_type;
// ...
// };

#define _LIBCUDACXX_CUDA_ABI_VERSION 3

#include <cuda/std/complex>
#include <cuda/std/type_traits>

#include "test_macros.h"

template <class T>
__host__ __device__ void
test()
{
typedef cuda::std::complex<T> C;

static_assert(sizeof(C) == (sizeof(T)*2), "wrong size");
static_assert(alignof(C) == (alignof(T)), "misaligned");
}

int main(int, char**)
{
test<float>();
test<double>();
// CUDA treats long double as double
// test<long double>();

return 0;
}
4 changes: 2 additions & 2 deletions include/cuda/std/detail/__config
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@
(_LIBCUDACXX_CUDA_API_VERSION % 1000)

#ifndef _LIBCUDACXX_CUDA_ABI_VERSION_LATEST
# define _LIBCUDACXX_CUDA_ABI_VERSION_LATEST 3
# define _LIBCUDACXX_CUDA_ABI_VERSION_LATEST 4
#endif

#ifdef _LIBCUDACXX_CUDA_ABI_VERSION
# if _LIBCUDACXX_CUDA_ABI_VERSION != 2 && _LIBCUDACXX_CUDA_ABI_VERSION != 3
# if _LIBCUDACXX_CUDA_ABI_VERSION != 2 && _LIBCUDACXX_CUDA_ABI_VERSION != 3 && _LIBCUDACXX_CUDA_ABI_VERSION != 4
# error Unsupported libcu++ ABI version requested. Please define _LIBCUDACXX_CUDA_ABI_VERSION to either 2 or 3.
# endif
#else
Expand Down
14 changes: 10 additions & 4 deletions libcxx/include/complex
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ template<class T, class charT, class traits>
#pragma GCC system_header
#endif

# if _LIBCUDACXX_CUDA_ABI_VERSION > 3
# define _LIBCUDACXX_COMPLEX_ALIGNAS(V) _ALIGNAS(V)
# else
# define _LIBCUDACXX_COMPLEX_ALIGNAS(V)
# endif

_LIBCUDACXX_BEGIN_NAMESPACE_STD

template<class _Tp> class _LIBCUDACXX_TEMPLATE_VIS complex;
Expand All @@ -264,7 +270,7 @@ template<class _Tp> _LIBCUDACXX_INLINE_VISIBILITY
complex<_Tp> operator/(const complex<_Tp>& __x, const complex<_Tp>& __y);

template<class _Tp>
class _LIBCUDACXX_TEMPLATE_VIS complex
class _LIBCUDACXX_COMPLEX_ALIGNAS(2*sizeof(_Tp))_LIBCUDACXX_TEMPLATE_VIS complex
{
public:
typedef _Tp value_type;
Expand Down Expand Up @@ -328,7 +334,7 @@ template<> class complex<long double>;
#endif // _LIBCUDACXX_HAS_COMPLEX_LONG_DOUBLE

template<>
class _LIBCUDACXX_TEMPLATE_VIS complex<float>
class _LIBCUDACXX_COMPLEX_ALIGNAS(2*sizeof(float))_LIBCUDACXX_TEMPLATE_VIS complex<float>
{
float __re_;
float __im_;
Expand Down Expand Up @@ -386,7 +392,7 @@ public:
};

template<>
class _LIBCUDACXX_TEMPLATE_VIS complex<double>
class _LIBCUDACXX_COMPLEX_ALIGNAS(2*sizeof(double))_LIBCUDACXX_TEMPLATE_VIS complex<double>
{
double __re_;
double __im_;
Expand Down Expand Up @@ -444,7 +450,7 @@ public:
};

template<>
class _LIBCUDACXX_TEMPLATE_VIS complex<long double>
class _LIBCUDACXX_COMPLEX_ALIGNAS(2*sizeof(long double))_LIBCUDACXX_TEMPLATE_VIS complex<long double>
{
#ifndef _LIBCUDACXX_HAS_COMPLEX_LONG_DOUBLE
public:
Expand Down

0 comments on commit 3896f6b

Please sign in to comment.