2 * linux/arch/alpha/lib/memcpy.c
4 * Copyright (C) 1995 Linus Torvalds
8 * This is a reasonably optimized memcpy() routine.
12 * Note that the C code is written to be optimized into good assembly. However,
13 * at this point gcc is unable to sanely compile "if (n >= 0)", resulting in a
14 * explicit compare against 0 (instead of just using the proper "blt reg, xx" or
15 * "bge reg, xx"). I hope alpha-gcc will be fixed to notice this eventually..
18 #include <linux/types.h>
19 #include <linux/export.h>
22 * This should be done in one go with ldq_u*2/mask/stq_u. Do it
23 * with a macro so that we can fix it up later..
25 #define ALIGN_DEST_TO8_UP(d,s,n) \
29 *(char *) d = *(char *) s; \
32 #define ALIGN_DEST_TO8_DN(d,s,n) \
37 *(char *) d = *(char *) s; \
41 * This should similarly be done with ldq_u*2/mask/stq. The destination
42 * is aligned, but we don't fill in a full quad-word
44 #define DO_REST_UP(d,s,n) \
47 *(char *) d = *(char *) s; \
50 #define DO_REST_DN(d,s,n) \
54 *(char *) d = *(char *) s; \
58 * This should be done with ldq/mask/stq. The source and destination are
59 * aligned, but we don't fill in a full quad-word
61 #define DO_REST_ALIGNED_UP(d,s,n) DO_REST_UP(d,s,n)
62 #define DO_REST_ALIGNED_DN(d,s,n) DO_REST_DN(d,s,n)
65 * This does unaligned memory copies. We want to avoid storing to
66 * an unaligned address, as that would do a read-modify-write cycle.
67 * We also want to avoid double-reading the unaligned reads.
69 * Note the ordering to try to avoid load (and address generation) latencies.
71 static inline void __memcpy_unaligned_up (unsigned long d, unsigned long s,
74 ALIGN_DEST_TO8_UP(d,s,n);
75 n -= 8; /* to avoid compare against 8 in the loop */
77 unsigned long low_word, high_word;
78 __asm__("ldq_u %0,%1":"=r" (low_word):"m" (*(unsigned long *) s));
81 __asm__("ldq_u %0,%1":"=r" (high_word):"m" (*(unsigned long *)(s+8)));
83 __asm__("extql %1,%2,%0"
85 :"r" (low_word), "r" (s));
86 __asm__("extqh %1,%2,%0"
88 :"r" (high_word), "r" (s));
90 *(unsigned long *) d = low_word | tmp;
99 static inline void __memcpy_unaligned_dn (unsigned long d, unsigned long s,
102 /* I don't understand AXP assembler well enough for this. -Tim */
106 * (char *) --d = * (char *) --s;
110 * Hmm.. Strange. The __asm__ here is there to make gcc use an integer register
111 * for the load-store. I don't know why, but it would seem that using a floating
112 * point register for the move seems to slow things down (very small difference,
115 * Note the ordering to try to avoid load (and address generation) latencies.
117 static inline void __memcpy_aligned_up (unsigned long d, unsigned long s,
120 ALIGN_DEST_TO8_UP(d,s,n);
124 __asm__("ldq %0,%1":"=r" (tmp):"m" (*(unsigned long *) s));
127 *(unsigned long *) d = tmp;
131 DO_REST_ALIGNED_UP(d,s,n);
133 static inline void __memcpy_aligned_dn (unsigned long d, unsigned long s,
138 ALIGN_DEST_TO8_DN(d,s,n);
143 __asm__("ldq %0,%1":"=r" (tmp):"m" (*(unsigned long *) s));
146 *(unsigned long *) d = tmp;
149 DO_REST_ALIGNED_DN(d,s,n);
152 void * memcpy(void * dest, const void *src, size_t n)
154 if (!(((unsigned long) dest ^ (unsigned long) src) & 7)) {
155 __memcpy_aligned_up ((unsigned long) dest, (unsigned long) src,
159 __memcpy_unaligned_up ((unsigned long) dest, (unsigned long) src, n);
162 EXPORT_SYMBOL(memcpy);