VISR  0.11.6
Versatile Interactive Scene Renderer
alignment.hpp
Go to the documentation of this file.
1 /* Copyright Institute of Sound and Vibration Research - All rights reserved */
2 
3 #ifndef VISR_LIBEFL_ALIGNMENT_HPP_INCLUDED
4 #define VISR_LIBEFL_ALIGNMENT_HPP_INCLUDED
5 
6 #include <cstddef>
7 #include <ciso646> // for 'or' (should be obsolete in C++11, but MSVC 2013 still needs it)
8 
17 namespace visr
18 {
19 namespace efl
20 {
21 
27 inline bool alignmentIsPowerOfTwo( std::size_t alignmentVal )
28 {
29  return ((alignmentVal != 0) && ((alignmentVal & (~alignmentVal + 1)) == alignmentVal));
30 }
31 
44 template<typename T>
45 bool checkAlignment( T const * ptr, std::size_t alignment )
46 {
47  if( alignment == 0 )
48  {
49  return true;
50  }
51  std::size_t const actualAlignment = alignment * sizeof( T );
52  // we assume that alignment is a power of two
53  if( !alignmentIsPowerOfTwo(alignment) )
54  {
55  return false;
56  }
57  std::ptrdiff_t const bitMask = static_cast<std::ptrdiff_t>(actualAlignment - 1);
58  std::ptrdiff_t ptrVal = reinterpret_cast<std::ptrdiff_t>(ptr); // todo: make this sound & safe
59  return (ptrVal bitand bitMask) == 0;
60 }
61 
70 inline std::size_t nextAlignedSize( std::size_t size, std::size_t alignment )
71 {
72  if( alignment == 0 )
73  {
74  return size;
75  }
76  std::size_t const rem = size % alignment;
77  if( rem == 0 )
78  {
79  return size;
80  }
81  else
82  {
83  return (size / alignment + 1) * alignment;
84  }
85 }
86 
87 } // namespace efl
88 } // namespace visr
89 
90 #endif // #ifndef VISR_LIBEFL_ALIGNMENT_HPP_INCLUDED
bool alignmentIsPowerOfTwo(std::size_t alignmentVal)
Definition: alignment.hpp:27
std::size_t nextAlignedSize(std::size_t size, std::size_t alignment)
Definition: alignment.hpp:70
bool checkAlignment(T const *ptr, std::size_t alignment)
Definition: alignment.hpp:45
Definition: options.cpp:10