C ++ 11: Is the new recurring continuous memory?

float* tempBuf = new float[maxVoices]();

Will the above result be in

1) memory aligned on 16 bytes?

2) a memory that is confirmed to be adjacent?

I want the following:

float tempBuf[maxVoices] __attribute__ ((aligned));

but as heap memory, it will be effective for the Apple Accelerate platform.

Thanks.

+4
source share
4 answers

The memory will be aligned for float, but not necessarily for, processor specific SIMD instructions. I strongly doubt your system sizeof(float) < 16, although this means that it is not aligned the way you want. The memory will be contiguous: &A[i] == &A[0] + i.

- , new std::aligned_storage<Length, Alignment> , , , .

struct FourFloats alignas(16) {float[4] floats;}; - . new FourFloats[(maxVoices+3)/4].

+10

, new .

, . :

template<class T, size_t A>
T* over_aligned(size_t N){
  static_assert(A <= alignof(std::max_align_t),
    "Over-alignment is implementation-defined."
  );
  static_assert( std::is_trivially_destructible<T>{},
    "Function does not store number of elements to destroy"
  );
  using Helper=std::aligned_storage_t<sizeof(T), A>;
  auto* ptr = new Helper[(N+sizeof(Helper)-1)/sizeof(Helper)];
  return new(ptr) T[N];
}

:

float* f = over_aligned<float,16>(37);

37 , 16 . .

, . . , , , , static assert, , , (yay).

, std::align T , T , T "" . .

+6
  • , . , 4 float ( 4 ), float s. 16 .
  • , ( , ?)

, K, std::align. . MSalter .

+2

tempBuf nullptr, ++ , tempBuf maxVoices float s.

(Remember to call delete[] tempBufafter you are done with it.)

0
source

Source: https://habr.com/ru/post/1627816/


All Articles