• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 
memset(void * dest,int c,size_t n)36 void *memset(void *dest, int c, size_t n)
37 {
38     char *pos = dest;
39     uint32_t c32 = 0;
40     uint64_t c64 = 0;
41     size_t num = n;
42 
43     if (num == 0) {
44         return dest;
45     }
46 
47     c = c & 0xFF;
48     if (c) {
49         c32 = c;
50         c32 |= c32 << 8; /* 8, Processed bits */
51         c32 |= c32 << 16; /* 16, Processed bits */
52         c64 = c32;
53         c64 |= c64 << 32; /* 32, Processed bits */
54     }
55 
56     if (((uintptr_t)(pos) & 7) != 0) { /* 7, Processed align */
57         int unalignedCnt = 8 - ((uintptr_t)(pos) & 7); /* 7, 8, for calculate addr bits align */
58         if (num >= unalignedCnt) {
59             num = num - unalignedCnt;
60         } else {
61             unalignedCnt = num;
62             num = 0;
63         }
64         for (int loop = 1; loop <= unalignedCnt; ++loop) {
65             *pos = (char)c;
66             pos++;
67         }
68     }
69 
70     /* L32_byte_aligned */
71     while (num >= 32) { /* 32, byte aligned */
72         *(uint64_t *)(pos) = c64;
73         *(uint64_t *)(pos + 8) = c64; /* 8, size of uint64_t */
74         *(uint64_t *)(pos + 16) = c64; /* 16, size of two uint64_t data */
75         *(uint64_t *)(pos + 24) = c64; /* 24, size of three uint64_t data */
76         num -= 32; /* 32, size of four uint64_t data */
77         pos += 32; /* 32, size of four uint64_t data */
78     }
79     if (num == 0) {
80         return dest;
81     }
82 
83     /* L16_byte_aligned */
84     if (num >= 16) { /* 16, byte aligned */
85         *(uint64_t *)(pos) = c64;
86         *(uint64_t *)(pos + 8) = c64; /* 8, size of uint64_t */
87         num -= 16; /* 16, size of two uint64_t data */
88         pos += 16; /* 16, size of two uint64_t data */
89         if (num == 0) {
90             return dest;
91         }
92     }
93 
94     /* L8_byte_aligned */
95     if (num >= 8) { /* 8, byte aligned */
96         *(uint64_t *)(pos) = c64;
97         num -= 8; /* 8, size of uint64_t */
98         pos += 8; /* 8, size of uint64_t */
99         if (num == 0) {
100             return dest;
101         }
102     }
103 
104     /* L4_byte_aligned */
105     if (num >= 4) { /* 4, byte aligned */
106         *(uint32_t *)(pos) = c32;
107         num -= 4; /* 4, size of uint32_t */
108         pos += 4; /* 4, size of uint32_t */
109         if (num == 0) {
110             return dest;
111         }
112     }
113     while (num--) {
114         *pos++ = c;
115     }
116 
117     return dest;
118 }