C intrinsics, std, cld

Andy 41 Reputation points
2020-10-30T09:49:05.997+00:00

I have an x64 C (not C++) dll that uses memmove on occasion. In some cases, the move is forward in overlapping memory, and memmove handles it properly as documented. For int * p, the kind of move I'm talking about is memmove(p+2,p+1,n).

I have a performance issue, and I'd like to try using intrinsics __movsd and __movsq. They compile to about 4 inline instructions, so they look promising. What I'd like to try is setting and clearing the direction flag like __std; __movsd(); __cld. Alas, in x64 I can't find direction flag manipulation intrinsics, and I can't use asm. Any ideas?

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,692 questions
{count} votes

Accepted answer
  1. Viorel 116.7K Reputation points
    2020-10-30T10:22:56.753+00:00

    The direction flag can be changed with __writeeflags. Check an example:

    unsigned long s[8] = { 100, 101, 102, 103, 104, 105, 106, 107 };
    unsigned long d[8] = { 0 };
    
    __writeeflags( __readeflags() | ( 1 << 10 ) );
    __movsd( d + 4, s + 4, 3 );
    

    It seems to work, however it does not generate just a simple std. Maybe consider adding an asm file to your project. Visual Studio is able to compile it.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.