| 1 |
/* go-memmove.c -- memmove |
| 2 |
|
| 3 |
Copyright 2021 The Go Authors. All rights reserved. |
| 4 |
Use of this source code is governed by a BSD-style |
| 5 |
license that can be found in the LICENSE file. */ |
| 6 |
|
| 7 |
#include "runtime.h" |
| 8 |
|
| 9 |
void gomemmove(void *, void *, uintptr) |
| 10 |
__asm__ (GOSYM_PREFIX "runtime.memmove") |
| 11 |
__attribute__ ((no_split_stack)); |
| 12 |
|
| 13 |
// This implementation is necessary since |
| 14 |
// the __builtin_memmove might use __libc_memmove |
| 15 |
// which doesn't require atomicity of 8 byte |
| 16 |
// moves. |
| 17 |
|
| 18 |
void |
| 19 |
gomemmove (void *dst, void *src, uintptr len) |
| 20 |
{ |
| 21 |
#if !defined(__PPC64__) |
| 22 |
__builtin_memmove(dst, src, len); |
| 23 |
#else |
| 24 |
uint64 offset, tail; |
| 25 |
int64 rem; |
| 26 |
uint64 dwords; |
| 27 |
uint64 i; |
| 28 |
char *bdst,*bsrc; |
| 29 |
|
| 30 |
rem = len; |
| 31 |
|
| 32 |
if (len == 0) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
// If src and dst don't have the same 8 byte alignment then |
| 37 |
// there is no issue with copying pointer atomicity. Use the |
| 38 |
// builtin. |
| 39 |
if (((uint64)dst % 8) != ((uint64)src % 8) || len < 8) { |
| 40 |
__builtin_memmove(dst, src, len); |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Length >= 8 && same ptr alignment |
| 45 |
offset = (uint64)dst % 8; |
| 46 |
|
| 47 |
// If not 8 byte alignment, move the intial bytes. |
| 48 |
if (offset > 0) { |
| 49 |
__builtin_memmove(dst, src, 8-offset); |
| 50 |
dst += (8-offset); |
| 51 |
src += (8-offset); |
| 52 |
rem -= (8-offset); |
| 53 |
} |
| 54 |
|
| 55 |
// Move the tail bytes to make the backward move |
| 56 |
// easier. |
| 57 |
tail = rem % 8; |
| 58 |
if (tail > 0) { |
| 59 |
__builtin_memmove(dst+rem-tail, src+rem-tail, tail); |
| 60 |
rem -= tail; |
| 61 |
} |
| 62 |
|
| 63 |
if (rem == 0) { |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
// Must now be 8 byte alignment and rem is multiple of 8. |
| 68 |
dwords = len>>3; |
| 69 |
|
| 70 |
// Determine if a backwards move is needed |
| 71 |
// Forward or backward, move all doublewords |
| 72 |
|
| 73 |
if ((uint64)(dst - src) < (uint64)rem) { |
| 74 |
bdst = dst+rem-8; |
| 75 |
bsrc = src+rem-8; |
| 76 |
for (i = 0; i<dwords; i++) { |
| 77 |
*(uint64*)bdst = *(uint64*)bsrc; |
| 78 |
bdst -= 8; |
| 79 |
bsrc -= 8; |
| 80 |
} |
| 81 |
} else { |
| 82 |
for (i = 0; i<dwords; i++) { |
| 83 |
*(uint64*)dst = *(uint64*)src; |
| 84 |
dst += 8; |
| 85 |
src += 8; |
| 86 |
} |
| 87 |
} |
| 88 |
#endif |
| 89 |
} |