summaryrefslogtreecommitdiffstats
path: root/third_party/xsimd/include/xsimd/memory/xsimd_alignment.hpp
blob: 2b3b350880e96fc9f19b6b2fd6cc613655b4689f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/***************************************************************************
 * Copyright (c) Johan Mabille, Sylvain Corlay, Wolf Vollprecht and         *
 * Martin Renou                                                             *
 * Copyright (c) QuantStack                                                 *
 * Copyright (c) Serge Guelton                                              *
 *                                                                          *
 * Distributed under the terms of the BSD 3-Clause License.                 *
 *                                                                          *
 * The full license is in the file LICENSE, distributed with this software. *
 ****************************************************************************/

#ifndef XSIMD_ALIGNMENT_HPP
#define XSIMD_ALIGNMENT_HPP

#include "../types/xsimd_utils.hpp"
#include "xsimd_aligned_allocator.hpp"

namespace xsimd
{
    /**
     * @struct aligned_mode
     * @brief tag for load and store of aligned memory.
     */
    struct aligned_mode
    {
    };

    /**
     * @struct unaligned_mode
     * @brief tag for load and store of unaligned memory.
     */
    struct unaligned_mode
    {
    };

    /***********************
     * Allocator alignment *
     ***********************/

    template <class A>
    struct allocator_alignment
    {
        using type = unaligned_mode;
    };

    template <class T, size_t N>
    struct allocator_alignment<aligned_allocator<T, N>>
    {
        using type = aligned_mode;
    };

    template <class A>
    using allocator_alignment_t = typename allocator_alignment<A>::type;

    /***********************
     * container alignment *
     ***********************/

    template <class C, class = void>
    struct container_alignment
    {
        using type = unaligned_mode;
    };

    template <class C>
    struct container_alignment<C, detail::void_t<typename C::allocator_type>>
    {
        using type = allocator_alignment_t<typename C::allocator_type>;
    };

    template <class C>
    using container_alignment_t = typename container_alignment<C>::type;

    /*********************
     * alignment checker *
     *********************/

    /**
     * Checks whether pointer \c ptr is aligned according the alignment
     * requirements of \c Arch.
     * @return true if the alignment requirements are met
     */
    template <class Arch = default_arch>
    inline bool is_aligned(void const* ptr)
    {
        return (reinterpret_cast<uintptr_t>(ptr) % static_cast<uintptr_t>(Arch::alignment())) == 0;
    }

}

#endif