lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Joakim Tjernlund |
| 3 | * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org> |
| 4 | * |
| 5 | * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball. |
| 6 | */ |
| 7 | |
| 8 | /* These are carefully optimized mem*() functions for PPC written in C. |
| 9 | * Don't muck around with these function without checking the generated |
| 10 | * assembler code. |
| 11 | * It is possible to optimize these significantly more by using specific |
| 12 | * data cache instructions(mainly dcbz). However that requires knownledge |
| 13 | * about the CPU's cache line size. |
| 14 | * |
| 15 | * BUG ALERT! |
| 16 | * The cache instructions on MPC8xx CPU's are buggy(they don't update |
| 17 | * the DAR register when causing a DTLB Miss/Error) and cannot be |
| 18 | * used on 8xx CPU's without a kernel patch to work around this |
| 19 | * problem. |
| 20 | */ |
| 21 | |
| 22 | #include <string.h> |
| 23 | |
| 24 | |
| 25 | void *memmove(void *to, const void *from, size_t n) |
| 26 | { |
| 27 | unsigned long rem, chunks, tmp1, tmp2; |
| 28 | unsigned char *tmp_to; |
| 29 | unsigned char *tmp_from = (unsigned char *)from; |
| 30 | |
| 31 | if (tmp_from >= (unsigned char *)to) |
| 32 | return memcpy(to, from, n); |
| 33 | chunks = n / 8; |
| 34 | tmp_from += n; |
| 35 | tmp_to = to + n; |
| 36 | if (!chunks) |
| 37 | goto lessthan8; |
| 38 | rem = (unsigned long )tmp_to % 4; |
| 39 | if (rem) |
| 40 | goto align; |
| 41 | copy_chunks: |
| 42 | do { |
| 43 | /* make gcc to load all data, then store it */ |
| 44 | tmp1 = *(unsigned long *)(tmp_from-4); |
| 45 | tmp_from -= 8; |
| 46 | tmp2 = *(unsigned long *)tmp_from; |
| 47 | *(unsigned long *)(tmp_to-4) = tmp1; |
| 48 | tmp_to -= 8; |
| 49 | *(unsigned long *)tmp_to = tmp2; |
| 50 | } while (--chunks); |
| 51 | lessthan8: |
| 52 | n = n % 8; |
| 53 | if (n >= 4) { |
| 54 | *(unsigned long *)(tmp_to-4) = *(unsigned long *)(tmp_from-4); |
| 55 | tmp_from -= 4; |
| 56 | tmp_to -= 4; |
| 57 | n = n-4; |
| 58 | } |
| 59 | if (!n ) return to; |
| 60 | do { |
| 61 | *--tmp_to = *--tmp_from; |
| 62 | } while (--n); |
| 63 | |
| 64 | return to; |
| 65 | align: |
| 66 | rem = 4 - rem; |
| 67 | n = n - rem; |
| 68 | do { |
| 69 | *--tmp_to = *--tmp_from; |
| 70 | } while (--rem); |
| 71 | chunks = n / 8; |
| 72 | if (chunks) |
| 73 | goto copy_chunks; |
| 74 | goto lessthan8; |
| 75 | } |
| 76 | libc_hidden_def(memmove) |