Skip to contentSkip to conversation

Using %cr2 to reference "current"

DISCUSSION Mikael Pettersson 1 participant 1 message Nov 6, 2001

lore
Share & tools
lore
#0 lore raw
Quoted 9 lines · Linus Torvalds
	/* Return "current" in %eax, trash %edx */
	do_get_current:
		movl $0x0003c000,%eax	// 4 bits at bit 14
		movl $-16384,%edx	// remove low 14 bits
		andl $esp,%eax
		andl $esp,%edx
		shrl $7,%eax		// color it by 128 bytes
		addl %edx,%eax
		ret
Quoted 5 lines

...
I would not be surprised if "mov %cr2,%reg" will break a netburst trace cache entity, or even cause microcode to be executed. While I _guarantee_ that all future Intel CPU's will continue to be fast at mixtures of simple arithmetic operations like "add" and "and".

On my Pentium 4:
- 6.30 cycles to copy %cr2 to %eax
- 1.05 cycles to compute a non-coloured current by masking %esp
- 2.31 cycles to compute a coloured current by your code above

I did some tests on using %cr2 for get_processor_id() a while ago, but it was clearly slower (58% on P6, 20% on K6-III, 3% on P5MMX) than *((%esp & mask)+offset), even though the latter also does a load.

/Mikael