why is my rep movsb not faster

Programming, for all ages and all languages.
Post Reply
songziming
Member
Member
Posts: 70
Joined: Fri Jun 28, 2013 1:48 am
Contact:

why is my rep movsb not faster

Post by songziming »

Tested on real machine, Intel core i3-5005U, 64-bit mode. CPUID shows enhanced rep movsb is supported.

Use rep movsb and rep movsq to copy from memory to framebuffer, and rep movsq about 8x faster than rep movsb. (EDIT: about 3~4 times, see the reply)

It seems rep movsb has no speed up at all. Source and target pointer are both 16-byte aligned. Is there any flag I have to enable?
Last edited by songziming on Thu Mar 28, 2024 5:55 am, edited 1 time in total.
Reinventing the Wheel, code: https://github.com/songziming/wheel
User avatar
BigBuda
Member
Member
Posts: 104
Joined: Fri Sep 03, 2021 5:20 pm

Re: why is my rep movsb not faster

Post by BigBuda »

I may be shooting in the dark here, but I recall reading something about all movs instructions being optimized the same way as movsb. If this is true (and hopefully someone with more knowledge on this will pitch in) then your results would make sense - movsq moves eight bytes at a time instead of one for movsb, i.e., it would not mean that you're not using the enhanced movsb, it would only mean that the relative difference in performance would be more or less the same, as both are (supposedly!!) optimized in equivalent ways. Please be aware that I'm not confident on the reliability of the original source.
Writing a bootloader in under 15 minutes: https://www.youtube.com/watch?v=0E0FKjvTA0M
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: why is my rep movsb not faster

Post by Octocontrabass »

songziming wrote:framebuffer
Intel wrote:Fast-string operation is used only if the source and destination addresses both use either the WB or WC memory types.
What memory type is your framebuffer?
songziming
Member
Member
Posts: 70
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Re: why is my rep movsb not faster

Post by songziming »

I also tested copy data from physical address zero to a dynamically allocated temp buffer. Copy size is 8 pages (32K). And time measure is using timestamp counter.

In this test no framebuffer is involved. There's no other running task, so no preemption.

"mem copy" means copy using rep movsb, "fast copy" means rep movsq. source and destination is always page aligned.
Attachments
real machine string copy test
real machine string copy test
Reinventing the Wheel, code: https://github.com/songziming/wheel
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: why is my rep movsb not faster

Post by Octocontrabass »

Your results are very inconsistent. Does your test program repeat the tests several times, or does it only run them once?

What is your CPU's TSC frequency?

Bit 0 of IA32_MISC_ENABLE (MSR 0x1A0) enables fast string instructions. It should already be set by firmware, but you might as well check.
songziming
Member
Member
Posts: 70
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Re: why is my rep movsb not faster

Post by songziming »

Bit 0 of IA32_MISC_ENABLE is enabled, I've checked.

What's the correct way to get TSC frequency? What I got from cpuid.15h and cpuid.16h is zero.

The buffer is allocated on the first run, and I ran the test several times. Interrupt is also disabled before the test.
Attachments
test result
test result
frequency got from cpuid
frequency got from cpuid
Reinventing the Wheel, code: https://github.com/songziming/wheel
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: why is my rep movsb not faster

Post by Octocontrabass »

songziming wrote:What's the correct way to get TSC frequency?
You're using a Broadwell CPU, so the TSC frequency is calculated by taking the value in bits 8-15 of MSR_PLATFORM_INFO (0xCE) and multiplying by 100MHz.
songziming wrote:The buffer is allocated on the first run, and I ran the test several times.
It doesn't count if you have to manually start each test run! The multiple test runs need to be completely automatic with nothing else between each run (so don't display results until all runs are complete).
songziming
Member
Member
Posts: 70
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Re: why is my rep movsb not faster

Post by songziming »

Octocontrabass wrote:The multiple test runs need to be completely automatic with nothing else between each run
I modified the test code, and the result seems valid. Time for movsb and movsq is close. I also tested copying data into framebuffer, their result is different.

So I think it's ok to use movsb for normal memcpy, and use special aligned copy in framebuffer driver.

btw, tsc frequency is 2GHz.
Attachments
time for copying to framebuffer and copying within ram
time for copying to framebuffer and copying within ram
Reinventing the Wheel, code: https://github.com/songziming/wheel
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: why is my rep movsb not faster

Post by Octocontrabass »

songziming wrote:I also tested copying data into framebuffer, their result is different.
What memory type is your framebuffer? Fast-string instructions only work with WB and WC, but the firmware will usually set the framebuffer to UC.
songziming
Member
Member
Posts: 70
Joined: Fri Jun 28, 2013 1:48 am
Contact:

Re: why is my rep movsb not faster

Post by songziming »

Octocontrabass wrote:What memory type is your framebuffer?
MTRR shows they're uncacheable, and I didn't set PAT/PCD/PWT bit in the page entry, IA32_PAT[7:0] is 6 (writeback). So the memory type for framebuffer is UC.
Reinventing the Wheel, code: https://github.com/songziming/wheel
Octocontrabass
Member
Member
Posts: 5513
Joined: Mon Mar 25, 2013 7:01 pm

Re: why is my rep movsb not faster

Post by Octocontrabass »

That's why it's slower. Try setting up your framebuffer for WC instead of UC.
Post Reply