1 /* ANSI C standard library function memcpy.
3 Copyright (c) 2002-2008 Tensilica Inc.
5 Permission is hereby granted, free of charge, to any person obtaining
6 a copy of this software and associated documentation files (the
7 "Software"), to deal in the Software without restriction, including
8 without limitation the rights to use, copy, modify, merge, publish,
9 distribute, sublicense, and/or sell copies of the Software, and to
10 permit persons to whom the Software is furnished to do so, subject to
11 the following conditions:
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 #include "xtensa-asm.h"
26 /* If the Xtensa Unaligned Load Exception option is not used, this
27 code can run a few cycles faster by relying on the low address bits
28 being ignored. However, if the code is then run with an Xtensa ISS
29 client that checks for unaligned accesses, it will produce a lot of
30 warning messages. Set this flag to disable the use of unaligned
31 accesses and keep the ISS happy. */
33 /* #define UNALIGNED_ADDRESSES_CHECKED XCHAL_UNALIGNED_LOAD_EXCEPTION */
34 #define UNALIGNED_ADDRESSES_CHECKED 1
37 /* void *memcpy (void *dst, const void *src, size_t len)
39 The algorithm is as follows:
41 If the destination is unaligned, align it by conditionally
42 copying 1- and/or 2-byte pieces.
44 If the source is aligned, copy 16 bytes with a loop, and then finish up
45 with 8, 4, 2, and 1-byte copies conditional on the length.
47 Else (if source is unaligned), do the same, but use SRC to align the
50 This code tries to use fall-through branches for the common
51 case of aligned source and destination and multiple of 4 (or 8) length. */
54 /* Byte by byte copy. */
58 .align XCHAL_INST_FETCH_WIDTH
62 /* Skip bytes to get proper alignment for three-byte loop */
63 .skip XCHAL_INST_FETCH_WIDTH - 3
70 add a7, a3, a4 // a7 = end address for source
74 #if XTENSA_ESP32_PSRAM_CACHE_FIX
81 #if XTENSA_ESP32_PSRAM_CACHE_FIX
90 /* Destination is unaligned. */
93 .Ldst1mod2: // dst is only byte aligned
95 /* Do short copies byte-by-byte. */
96 bltui a4, 7, .Lbytecopy
103 #if XTENSA_ESP32_PSRAM_CACHE_FIX
108 /* Return to main algorithm if dst is now aligned. */
109 bbci.l a5, 1, .Ldstaligned
111 .Ldst2mod4: // dst has 16-bit alignment
113 /* Do short copies byte-by-byte. */
114 bltui a4, 6, .Lbytecopy
123 #if XTENSA_ESP32_PSRAM_CACHE_FIX
128 /* dst is now aligned; return to main algorithm. */
134 .type memcpy, @function
137 /* a2 = dst, a3 = src, a4 = len */
139 mov a5, a2 // copy dst so that a2 is return value
140 bbsi.l a2, 0, .Ldst1mod2
141 bbsi.l a2, 1, .Ldst2mod4
144 /* Get number of loop iterations with 16B per iteration. */
147 /* Check if source is aligned. */
149 bnez a8, .Lsrcunaligned
151 /* Destination and source are word-aligned, use word copy. */
157 add a8, a8, a3 // a8 = end of last 16B source chunk
160 #if XTENSA_ESP32_PSRAM_CACHE_FIX
192 #if !XCHAL_HAVE_LOOPS
196 /* Copy any leftover pieces smaller than 16B. */
210 #if XTENSA_ESP32_PSRAM_CACHE_FIX
223 #if XTENSA_ESP32_PSRAM_CACHE_FIX
234 #if XTENSA_ESP32_PSRAM_CACHE_FIX
244 #if XTENSA_ESP32_PSRAM_CACHE_FIX
250 /* Destination is aligned; source is unaligned. */
254 /* Avoid loading anything for zero-length copies. */
257 /* Copy 16 bytes per iteration for word-aligned dst and
259 ssa8 a3 // set shift amount from byte offset
260 #if UNALIGNED_ADDRESSES_CHECKED
261 srli a11, a8, 30 // save unalignment offset for below
262 sub a3, a3, a11 // align a3
264 l32i a6, a3, 0 // load first word
270 add a10, a10, a3 // a10 = end of last 16B source chunk
286 #if !XCHAL_HAVE_LOOPS
313 #if UNALIGNED_ADDRESSES_CHECKED
314 add a3, a3, a11 // readjust a3 with correct misalignment
328 #if XTENSA_ESP32_PSRAM_CACHE_FIX
336 #if XTENSA_ESP32_PSRAM_CACHE_FIX
343 .size memcpy, . - memcpy