Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search a string while a character is met

Status
Not open for further replies.

TheGreyBeast

Programmer
Apr 8, 2005
19
RO
In Visual C++, the functions memcmp, memcpy and memset are all intrinsics (which makes them really fast), but is there a way to make memchr intrinsic too?

Also, I'd like to make a new intrinsic function which searches a string and stops if it encounters a character other than al. For example, if al is 0 and the string is 00 00 00 06 FF ... then the string will repeat 3 times. The code in asm for this is:

Code:
xor al, al  ; al = 0
repe scasb  ; repeat and stop when the string contains
            ; a value other than 0

The question is: How can I make my own intrinsic function, or at least achieve the same performance as if it was an intrinsic function? How can I do this to achieve the best performance? Thanks.
 
Hello.

Not pretty sure about what you want to do; But I think you should use inline assembler and #pragma intrisic

In fact, I don't know the difference (if any) between #pragma intrisic and the keyword inline.

However, if you are going to write assembly, I think you should use RISC instructions (repe scasb looks like a CISC instruction) since the actual x86 processors implement RISC cores and "interpreters" for CISC instructions.

Hope it helps!

Polu.
 
The difference between intrinsic and inline is that in inline code you need to write C code. If you write inline assembler code, it will do a lot of crap on performance. For example, if I write my own inline memcpy, and then use the standard intrinsic memcpy, it's a little difference in the code.

Code:
//Code1 -- standard memcpy
memcpy((void*)buffer,(void*)screen,128);

// turns into something like...
mov ecx, 128/4
mov edi, OFFSET buffer
mov esi, OFFSET screen
repe scasd

Code:
//Code2 -- my memcpy
mymemcpy((void*)buffer,(void*)screen,128);

// turns into something like...
mov DWORD PTR [ebp+12], ecx
mov DWORD PTR [ebp+4], edi
mov DWORD PTR [ebp+8], esi

mov ecx, 128/4
mov edi, OFFSET buffer
mov esi, OFFSET screen
repe scasd

mov ecx, DWORD PTR [ebp+12]
mov edi, DWORD PTR [ebp+4]
mov esi, DWORD PTR [ebp+8]

There are some extra useless instructions.. The intrinsic functions are smart. For example, if I have the code __mov ecx, x where x is a local variable, and the compiler stores the variable x in eax for some time, then it should have been generated as: mov ecx, eax. But if I don't use intrinsic functions, it just generates something like:
Code:
mov DWORD PTR x[ebp], eax
mov ecx, DWORD PTR x[ebp]

Why isn't it smart for my inline assembler too?

Thanks anyway for your help. :)

There is something in each of us we don't know. Something with great power.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top