The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | #include <string.h> |
| 29 | |
| 30 | void *memmove(void *dst, const void *src, size_t n) |
| 31 | { |
| 32 | const char *p = src; |
| 33 | char *q = dst; |
| 34 | if (__builtin_expect(q < p, 1)) { |
| 35 | return memcpy(dst, src, n); |
| 36 | } else { |
| 37 | #define PRELOAD_DISTANCE 64 |
| 38 | /* a semi-optimized memmove(). we're preloading the src and dst buffers |
| 39 | * as we go */ |
| 40 | size_t c0, c1, i; |
| 41 | p += n; |
| 42 | q += n; |
| 43 | /* note: we preload the destination as well, because the 1-byte at a time |
| 44 | * copy below doesn't take advantage of the write-buffer, we need |
| 45 | * to use the cache instead as a poor man's write-combiner */ |
| 46 | __builtin_prefetch(p-1); |
| 47 | __builtin_prefetch(q-1); |
| 48 | if (PRELOAD_DISTANCE > 32) { |
| 49 | __builtin_prefetch(p-(32+1)); |
| 50 | __builtin_prefetch(q-(32+1)); |
| 51 | } |
| 52 | /* do the prefetech as soon as possible, prevent the compiler to |
| 53 | * reorder the instructions above the prefetch */ |
| 54 | asm volatile("":::"memory"); |
| 55 | c0 = n & 0x1F; /* cache-line is 32 bytes */ |
| 56 | c1 = n >> 5; |
| 57 | while ( c1-- ) { |
| 58 | /* ARMv6 can have up to 3 memory access outstanding */ |
| 59 | __builtin_prefetch(p - (PRELOAD_DISTANCE+1)); |
| 60 | __builtin_prefetch(q - (PRELOAD_DISTANCE+1)); |
| 61 | asm volatile("":::"memory"); |
| 62 | for (i=0 ; i<32 ; i++) { |
| 63 | *--q = *--p; |
| 64 | } |
| 65 | } |
| 66 | while ( c0-- ) { |
| 67 | *--q = *--p; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | return dst; |
| 72 | } |