summaryrefslogtreecommitdiff
path: root/thirdparty/embree-aarch64/common/sys/array.h
blob: 77722a39f6704a09bd0f22fc9e5841b33f36dbd5 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright 2009-2020 Intel Corporation
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include "platform.h"
#include "alloc.h"

namespace embree
{
  /*! static array with static size */
  template<typename T, size_t N>
    class array_t
    {
    public:

      /********************** Iterators  ****************************/

      __forceinline T* begin() const { return items; };
      __forceinline T* end  () const { return items+N; };


      /********************** Capacity ****************************/

      __forceinline bool   empty    () const { return N == 0; }
      __forceinline size_t size     () const { return N; }
      __forceinline size_t max_size () const { return N; }
            

      /******************** Element access **************************/

      __forceinline       T& operator[](size_t i)       { assert(i < N); return items[i]; }
      __forceinline const T& operator[](size_t i) const { assert(i < N); return items[i]; }

      __forceinline       T& at(size_t i)       { assert(i < N); return items[i]; }
      __forceinline const T& at(size_t i) const { assert(i < N); return items[i]; }

      __forceinline T& front() const { assert(N > 0); return items[0]; };
      __forceinline T& back () const { assert(N > 0); return items[N-1]; };

      __forceinline       T* data()       { return items; };
      __forceinline const T* data() const { return items; };

    private:
      T items[N];
    };

  /*! static array with dynamic size */
  template<typename T, size_t N>
    class darray_t
    {
    public:

      __forceinline darray_t () : M(0) {}

      __forceinline darray_t (const T& v) : M(0) {
        for (size_t i=0; i<N; i++) items[i] = v;
      }

      /********************** Iterators  ****************************/

      __forceinline T* begin() const { return items; };
      __forceinline T* end  () const { return items+M; };


      /********************** Capacity ****************************/

      __forceinline bool   empty    () const { return M == 0; }
      __forceinline size_t size     () const { return M; }
      __forceinline size_t capacity () const { return N; }
      __forceinline size_t max_size () const { return N; }
      
      void resize(size_t new_size) {
        assert(new_size < max_size());
        M = new_size;
      }

      /******************** Modifiers **************************/

      __forceinline void push_back(const T& v) 
      {
        assert(M+1 < max_size());
        items[M++] = v;
      }

      __forceinline void pop_back() 
      {
        assert(!empty());
        M--;
      }

      __forceinline void clear() {
        M = 0;
      }

      /******************** Element access **************************/

      __forceinline       T& operator[](size_t i)       { assert(i < M); return items[i]; }
      __forceinline const T& operator[](size_t i) const { assert(i < M); return items[i]; }

      __forceinline       T& at(size_t i)       { assert(i < M); return items[i]; }
      __forceinline const T& at(size_t i) const { assert(i < M); return items[i]; }

      __forceinline T& front() const { assert(M > 0); return items[0]; };
      __forceinline T& back () const { assert(M > 0); return items[M-1]; };

      __forceinline       T* data()       { return items; };
      __forceinline const T* data() const { return items; };

    private:
      size_t M;
      T items[N];
    };

  /*! dynamic sized array that is allocated on the stack */
#define dynamic_large_stack_array(Ty,Name,N,max_stack_bytes) StackArray<Ty,max_stack_bytes> Name(N)
  template<typename Ty, size_t max_stack_bytes>
    struct __aligned(64) StackArray
  {
    __forceinline StackArray (const size_t N)
      : N(N)
    {
      if (N*sizeof(Ty) <= max_stack_bytes) 
        data = &arr[0];
      else
        data = (Ty*) alignedMalloc(N*sizeof(Ty),64); 
    }

    __forceinline ~StackArray () {
      if (data != &arr[0]) alignedFree(data);
    }

    __forceinline operator       Ty* ()       { return data; }
    __forceinline operator const Ty* () const { return data; }

    __forceinline       Ty& operator[](const int i)       { assert(i>=0 && i<N); return data[i]; }
    __forceinline const Ty& operator[](const int i) const { assert(i>=0 && i<N); return data[i]; }

    __forceinline       Ty& operator[](const unsigned i)       { assert(i<N); return data[i]; }
    __forceinline const Ty& operator[](const unsigned i) const { assert(i<N); return data[i]; }

#if defined(__X86_64__) || defined(__aarch64__)
    __forceinline       Ty& operator[](const size_t i)       { assert(i<N); return data[i]; }
    __forceinline const Ty& operator[](const size_t i) const { assert(i<N); return data[i]; }
#endif

  private:
    Ty arr[max_stack_bytes/sizeof(Ty)];
    Ty* data;
    size_t N;

  private:
    StackArray (const StackArray& other) DELETED; // do not implement
    StackArray& operator= (const StackArray& other) DELETED; // do not implement

  };

  /*! dynamic sized array that is allocated on the stack */
  template<typename Ty, size_t max_stack_elements, size_t max_total_elements>
    struct __aligned(64) DynamicStackArray
  {
    __forceinline DynamicStackArray ()
      : data(&arr[0]) {}

    __forceinline ~DynamicStackArray ()
    {
      if (!isStackAllocated())
        delete[] data;
    }

    __forceinline bool isStackAllocated() const {
      return data == &arr[0];
    }

    __forceinline size_t size() const
    {
      if (isStackAllocated()) return max_stack_elements;
      else return max_total_elements;
    }

    __forceinline void resize(size_t M)
    {
      assert(M <= max_total_elements);
      if (likely(M <= max_stack_elements)) return;
      if (likely(!isStackAllocated())) return;

      data = new Ty[max_total_elements];
      
      for (size_t i=0; i<max_stack_elements; i++)
        data[i] = arr[i];
    }

    __forceinline operator       Ty* ()       { return data; }
    __forceinline operator const Ty* () const { return data; }

    __forceinline       Ty& operator[](const int i)      { assert(i>=0 && i<max_total_elements); resize(i+1); return data[i]; }
    __forceinline       Ty& operator[](const unsigned i) { assert(i<max_total_elements); resize(i+1); return data[i]; }

#if defined(__X86_64__) || defined(__aarch64__)
    __forceinline       Ty& operator[](const size_t i) { assert(i<max_total_elements); resize(i+1); return data[i]; }
#endif

    __forceinline DynamicStackArray (const DynamicStackArray& other)
      : data(&arr[0]) 
    {
      for (size_t i=0; i<other.size(); i++)
        this->operator[] (i) = other[i];
    }
     
    DynamicStackArray& operator= (const DynamicStackArray& other)
    {
      for (size_t i=0; i<other.size(); i++)
        this->operator[] (i) = other[i];

      return *this;
    }

  private:
    Ty arr[max_stack_elements];
    Ty* data;
  };
}