libcruft-util/coord/store.hpp

168 lines
4.9 KiB
C++
Raw Normal View History

/*
2015-04-13 18:05:28 +10:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
2015-04-13 18:05:28 +10:00
* http://www.apache.org/licenses/LICENSE-2.0
*
2015-04-13 18:05:28 +10:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Copyright 2015-2017 Danny Robson <danny@nerdcruft.net>
*/
2017-08-28 12:25:23 +10:00
#ifndef CRUFT_UTIL_COORD_STORE_HPP
#define CRUFT_UTIL_COORD_STORE_HPP
#include "fwd.hpp"
#include "../preprocessor.hpp"
#include "../platform.hpp"
#include <cstddef>
#include <type_traits>
///////////////////////////////////////////////////////////////////////////////
// Calculate a reasonable alignment for the given type and arity given what we
// know about the platform. Only intended to be used with alignas specifiers.
namespace util::coord::detail {
template <typename T>
constexpr
2017-08-28 12:25:23 +10:00
std::size_t
alignment (std::size_t S)
{
(void)S;
#if defined(__SSE_MATH__)
// Align to 16 if we have 4x floats on SSE/NEON. There are other
// possiblities, but we don't care about them right at this point.
if (!std::is_same<T,float>::value)
return alignof (T);
if (S % 4 == 0)
return 16;
#elif defined (__ARM_NEON__)
// TODO: deal with alignment issues before adding alignas specifiers
#endif
return alignof (T);
}
}
#define SIMD_ALIGN(S,T) alignas (util::coord::detail::alignment<T> (S))
2017-01-05 15:06:49 +11:00
///////////////////////////////////////////////////////////////////////////////
// defines the named member variables that a coordinate type is composed of
#define DEFINE_COORD_STORE(TAG,...) \
namespace util::coord { \
template <typename T> \
struct store< \
VA_ARGS_COUNT(__VA_ARGS__), \
T, \
TAG \
> { \
union { \
struct { \
T __VA_ARGS__; \
}; \
T data[VA_ARGS_COUNT(__VA_ARGS__)]; \
}; \
}; \
}
2017-11-23 17:24:11 +11:00
#define DEFINE_STORE(KLASS,...) \
template <typename T> \
struct util::coord::store< \
VA_ARGS_COUNT(__VA_ARGS__), \
T, \
::util::KLASS< \
VA_ARGS_COUNT(__VA_ARGS__), \
T \
> \
> { \
union { \
T data[VA_ARGS_COUNT(__VA_ARGS__)]; \
struct { T __VA_ARGS__; }; \
}; \
};
DEFINE_STORE(extent,w)
DEFINE_STORE(extent,w,h)
DEFINE_STORE(extent,w,h,d)
DEFINE_STORE(point, x)
DEFINE_STORE(point, x, y)
DEFINE_STORE(point, x, y, z)
DEFINE_STORE(point, x, y, z, w)
DEFINE_STORE(vector, x)
DEFINE_STORE(vector, x, y)
DEFINE_STORE(vector, x, y, z)
DEFINE_STORE(vector, x, y, z, w)
#undef DEFINE_STORE
#if 0
template <typename T>
struct util::coord::store<1,T,::util::extent<1,T>> {
2017-11-23 17:24:11 +11:00
union { T data[1]; struct { T w; }; };
};
template <typename T>
struct util::coord::store<2,T,::util::extent<2,T>> {
union { struct { T w, h; }; T data[2]; };
};
template <typename T>
struct util::coord::store<3,T,::util::extent<3,T>> {
union { struct { T w, h, d; }; T data[3]; };
};
template <typename T>
struct util::coord::store<1,T,::util::point<1,T>> {
union { struct { T x; }; T data[1]; };
};
template <typename T>
struct util::coord::store<2,T,::util::point<2,T>> {
union { struct { T x, y; }; T data[2]; };
};
template <typename T>
struct util::coord::store<3,T,::util::point<3,T>> {
union { struct { T x, y, z; }; T data[3]; };
};
template <typename T>
struct util::coord::store<4,T,::util::point<4,T>> {
union { struct { T x, y, z, w; }; T data[4]; };
};
template <typename T>
struct util::coord::store<1,T,::util::vector<1,T>> {
union { struct { T x; }; T data[1]; };
};
template <typename T>
struct util::coord::store<2,T,::util::vector<2,T>> {
union { struct { T x, y; }; T data[2]; };
};
template <typename T>
struct util::coord::store<3,T,::util::vector<3,T>> {
union { struct { T x, y, z; }; T data[3]; };
};
template <typename T>
struct util::coord::store<4,T,::util::vector<4,T>> {
union { struct { T x, y, z, w; }; T data[4]; };
};
2017-11-23 17:24:11 +11:00
#endif
#endif