1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *  * Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  *  * Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in
12  *    the documentation and/or other materials provided with the
13  *    distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /* generic C version for any machine */
30 
31 #include <cutils/memory.h>
32 
33 #ifdef __clang__
34 __attribute__((no_sanitize("integer")))
35 #endif
android_memset16(uint16_t * dst,uint16_t value,size_t size)36 void android_memset16(uint16_t* dst, uint16_t value, size_t size)
37 {
38    /* optimized version of
39       size >>= 1;
40       while (size--)
41         *dst++ = value;
42    */
43 
44    size >>= 1;
45    if (((uintptr_t)dst & 2) && size) {
46       /* fill unpaired first elem separately */
47       *dst++ = value;
48       size--;
49    }
50    /* dst is now 32-bit-aligned */
51    /* fill body with 32-bit pairs */
52    uint32_t value32 = (((uint32_t)value) << 16) | ((uint32_t)value);
53    android_memset32((uint32_t*) dst, value32, size<<1);
54    if (size & 1) {
55       dst[size-1] = value;  /* fill unpaired last elem */
56    }
57 }
58 
59 
60 #ifdef __clang__
61 __attribute__((no_sanitize("integer")))
62 #endif
android_memset32(uint32_t * dst,uint32_t value,size_t size)63 void android_memset32(uint32_t* dst, uint32_t value, size_t size)
64 {
65    /* optimized version of
66       size >>= 2;
67       while (size--)
68          *dst++ = value;
69    */
70 
71    size >>= 2;
72    if (((uintptr_t)dst & 4) && size) {
73       /* fill unpaired first 32-bit elem separately */
74       *dst++ = value;
75       size--;
76    }
77    /* dst is now 64-bit aligned */
78    /* fill body with 64-bit pairs */
79    uint64_t value64 = (((uint64_t)value) << 32) | ((uint64_t)value);
80    uint64_t* dst64 = (uint64_t*)dst;
81 
82    while (size >= 12) {
83       dst64[0] = value64;
84       dst64[1] = value64;
85       dst64[2] = value64;
86       dst64[3] = value64;
87       dst64[4] = value64;
88       dst64[5] = value64;
89       size  -= 12;
90       dst64 += 6;
91    }
92 
93    /* fill remainder with original 32-bit single-elem loop */
94    dst = (uint32_t*) dst64;
95    while (size != 0) {
96        size--;
97       *dst++ = value;
98    }
99 
100 }
101