Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

x86: use of __builtin to create 'rep movs[bwlq]'

723 views
Skip to first unread message

andreyvul

unread,
Sep 30, 2009, 9:02:36 PM9/30/09
to
Is there a builtin so that a custom memcpy(void *d, const void *s,
size_t n) , where sizeof(*s)==sizeof(*d)==(1,2,4,8)
and gcc generates
BITS 32:
mov n, %ecx
mov s, %esi
mov d, %edi
rep movs[bwl]

BITS 64
mov n, %rcx
mov s, %rsi
mov d, %rdi
rep movs[bwlq]

?
Kind of like __movs[bwdq](d, s, n) intrinsic in MSVC.

But gcc's __builtin_movsd is _mm_movsd, which is not what I wanted at
all.

Pardon my ASM, I'm used to Intel syntax.

Jan Seiffert

unread,
Oct 1, 2009, 8:04:54 PM10/1/09
to

Your best chance is __builtin_memcpy(), but it may not generate a rep movs[x].
It's pros:
- (mostly) always inlined memcpy
- compiler can use knowledge of copy (alignment, size) to strenght reduce stuff
Cons:
- Compiler is still free to not use rep movs[x]
- Compiler may still get it wrong (can not deduce size at compile time, etc.)

Otherwise you have to use inline asm. This way you can "build" your own
compatible substitute for __movs[bwdq].

like in a header compat_helper.h:
#ifdef __GNUC__
static inline void *__movsb(void *d, const void *s, size_t n)
{
asm("rep movsb"
: "=D" (d),
"=S" (s),
"=c" (n)
: "0" (d),
"1" (s),
"2" (n)
: "memory");
return d;
}

static inline void *__movsw(void *d, const void *s, size_t n)
{
asm("rep movsw"
: "=D" (d),
"=S" (s),
"=c" (n)
: "0" (d),
"1" (s),
"2" (n/2)
: "memory");
return d;
}
...
#elif defined(__MSVC__)
/* nothing */
#else
# error "no compat helper for your compiler"
#endif

Greetings
Jan

--
/home
sweet
/home

0 new messages