Вопрос по c – Есть ли memset (), который принимает целые числа больше чем char?

30

Error: User Rate Limit Exceeded

int32_t array[10];
memset32(array, 0xDEADBEEF, sizeof(array));

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Ваш Ответ

8   ответов
31
void memset64( void * dest, uint64_t value, uintptr_t size )
{
  uintptr_t i;
  for( i = 0; i < (size & (~7)); i+=8 )
  {
    memcpy( ((char*)dest) + i, &value, 8 );
  }  
  for( ; i < size; i++ )
  {
    ((char*)dest)[i] = ((char*)&value)[i&7];
  }  
}

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded gnobal
6

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

1

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

//pseudo code
asm
{
    rep stosq ...
}

Error: User Rate Limit Exceeded

10

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
4

wmemset(3)Error: User Rate Limit Exceeded

0

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
2

Error: User Rate Limit Exceeded

inline void memset32(void *buf, uint32_t n, int32_t c)
{
  __asm {
  mov ecx, n
  mov eax, c
  mov edi, buf
  rep stosd
  }
}

Error: User Rate Limit Exceeded

for(uint32_t i = 0;i < n;i++)
{
  ((int_32 *)buf)[i] = c;
}

Error: User Rate Limit Exceeded

Error: User Rate Limit ExceededcldError: User Rate Limit Exceededsizeof arrayError: User Rate Limit Exceeded
7

Error: User Rate Limit Exceededmemcpy(..)Error: User Rate Limit Exceeded

--------------------

First copy one:
N-------------------

Then copy it to the neighbour:
NN------------------

Then copy them to make four:
NNNN----------------

And so on:
NNNNNNNN------------

NNNNNNNNNNNNNNNN----

Then copy enough to fill the array:
NNNNNNNNNNNNNNNNNNNN

Error: User Rate Limit Exceededmemcpy(..).

int *memset_int(int *ptr, int value, size_t num) {
    if (num < 1) return ptr;
    memcpy(ptr, &value, sizeof(int));
    size_t start = 1, step = 1;
    for ( ; start + step <= num; start += step, step *= 2)
        memcpy(ptr + start, ptr, sizeof(int) * step);

    if (start < num)
        memcpy(ptr + start, ptr, sizeof(int) * (num - start));
    return ptr;
}

Error: User Rate Limit Exceededmemcpy(..)Error: User Rate Limit Exceededmemcpy(..)Error: User Rate Limit Exceeded

Современные процессоры могут выполнять простой цикл достаточно быстро, чтобы насыщать шину памяти, что делает избыточными инструкции перемещения / копирования блоков.
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы