Is there memset() that accepts integers larger than char?

前端 未结 8 641
时光说笑
时光说笑 2020-12-05 00:25

Is there a version of memset() which sets a value that is larger than 1 byte (char)? For example, let\'s say we have a memset32() function, so using it we can do the followi

8条回答
  •  余生分开走
    2020-12-05 00:33

    Just for the record, the following uses memcpy(..) in the following pattern. Suppose we want to fill an array with 20 integers:

    --------------------
    
    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
    

    This takes O(lg(num)) applications of memcpy(..).

    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;
    }
    

    I thought it might be faster than a loop if memcpy(..) was optimised using some hardware block memory copy functionality, but it turns out that a simple loop is faster than the above with -O2 and -O3. (At least using MinGW GCC on Windows with my particular hardware.) Without the -O switch, on a 400 MB array the code above is about twice as fast as an equivalent loop, and takes 417 ms on my machine, while with optimisation they both go to about 300 ms. Which means that it takes approximately the same number of nanoseconds as bytes, and a clock cycle is about a nanosecond. So either there is no hardware block memory copy functionality on my machine, or the memcpy(..) implementation does not take advantage of it.

提交回复
热议问题