Из моего понимания Википедии я могу найти индекс следующего ближайшего элемента с правильным выравниванием с помощью следующей бить. < /p>
Assuming the address of the 1st element has the correct alignment.
Assuming the index_alignment is a power of 2.
new_index = (current_index + index_alignment - 1) & ~(index_alignment - 1).
new_address = address_of_1st_element + new_index
index_alignment is 16 bytes/sizeof(type of element) for SSE.
< /code>
Можно ли использовать это непосредственно по адресу, чтобы найти следующий ближайший выравниваемый адрес с любого данного адреса? (Это так быстрее?) < /p>
Я думал о следующем, чтобы сделать это быстро.new_address = (current_address + alignment - 1) & ~(alignment -1)
alignment here is 16 for SSE.
< /code>
Когда я реализую это, я обнаружил, что следующий код не может скомпилировать ... < /p>
Код, фиксированный с предложениями Salva и Rotem < /p>
#include
#include
#define ALIGNMENT 16
using namespace std;
uint32_t* fix_ptr(uint32_t* ptr){
return (uint32_t*)(
(uintptr_t(ptr) + uintptr_t(ALIGNMENT) - 1)
&~ uintptr_t(ALIGNMENT - 1)
);
}
void print_ptr (uint32_t* ptr){
cout
Подробнее здесь: https://stackoverflow.com/questions/383 ... ry-address