memset.c 3.8 KB
Newer Older
A
arvinzzz 已提交
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
/*
 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

void *memset(void *dest, int c, size_t n)
{
    char *pos = dest;
    uint32_t c32 = 0;
    uint64_t c64 = 0;
L
lnlan 已提交
41
    size_t num = n;
A
arvinzzz 已提交
42

L
lnlan 已提交
43 44 45
    if (num == 0) {
        return dest;
    }
A
arvinzzz 已提交
46 47 48 49

    c = c & 0xFF;
    if (c) {
        c32 = c;
L
lnlan 已提交
50 51
        c32 |= c32 << 8; /* 8, Processed bits */
        c32 |= c32 << 16; /* 16, Processed bits */
A
arvinzzz 已提交
52
        c64 = c32;
L
lnlan 已提交
53
        c64 |= c64 << 32; /* 32, Processed bits */
A
arvinzzz 已提交
54 55
    }

L
lnlan 已提交
56 57 58 59
    if (((uintptr_t)(pos) & 7) != 0) { /* 7, Processed align */
        int unalignedCnt = 8 - ((uintptr_t)(pos) & 7); /* 7, 8, for calculate addr bits align */
        if (num >= unalignedCnt) {
            num = num - unalignedCnt;
A
arvinzzz 已提交
60
        } else {
L
lnlan 已提交
61 62
            unalignedCnt = num;
            num = 0;
A
arvinzzz 已提交
63 64 65 66 67 68 69 70
        }
        for (int loop = 1; loop <= unalignedCnt; ++loop) {
            *pos = (char)c;
            pos++;
        }
    }

    /* L32_byte_aligned */
L
lnlan 已提交
71
    while (num >= 32) { /* 32, byte aligned */
A
arvinzzz 已提交
72
        *(uint64_t *)(pos) = c64;
L
lnlan 已提交
73 74 75 76 77 78 79 80
        *(uint64_t *)(pos + 8) = c64; /* 8, size of uint64_t */
        *(uint64_t *)(pos + 16) = c64; /* 16, size of two uint64_t data */
        *(uint64_t *)(pos + 24) = c64; /* 24, size of three uint64_t data */
        num -= 32; /* 32, size of four uint64_t data */
        pos += 32; /* 32, size of four uint64_t data */
    }
    if (num == 0) {
        return dest;
A
arvinzzz 已提交
81 82 83
    }

    /* L16_byte_aligned */
L
lnlan 已提交
84
    if (num >= 16) { /* 16, byte aligned */
A
arvinzzz 已提交
85
        *(uint64_t *)(pos) = c64;
L
lnlan 已提交
86 87 88 89 90 91
        *(uint64_t *)(pos + 8) = c64; /* 8, size of uint64_t */
        num -= 16; /* 16, size of two uint64_t data */
        pos += 16; /* 16, size of two uint64_t data */
        if (num == 0) {
            return dest;
        }
A
arvinzzz 已提交
92 93 94
    }

    /* L8_byte_aligned */
L
lnlan 已提交
95
    if (num >= 8) { /* 8, byte aligned */
A
arvinzzz 已提交
96
        *(uint64_t *)(pos) = c64;
L
lnlan 已提交
97 98 99 100 101
        num -= 8; /* 8, size of uint64_t */
        pos += 8; /* 8, size of uint64_t */
        if (num == 0) {
            return dest;
        }
A
arvinzzz 已提交
102 103 104
    }

    /* L4_byte_aligned */
L
lnlan 已提交
105
    if (num >= 4) { /* 4, byte aligned */
A
arvinzzz 已提交
106
        *(uint32_t *)(pos) = c32;
L
lnlan 已提交
107 108 109 110 111
        num -= 4; /* 4, size of uint32_t */
        pos += 4; /* 4, size of uint32_t */
        if (num == 0) {
            return dest;
        }
A
arvinzzz 已提交
112
    }
L
lnlan 已提交
113
    while (num--) {
A
arvinzzz 已提交
114 115 116 117 118
        *pos++ = c;
    }

    return dest;
}