![]() |
[QUOTE=R.D. Silverman;232315]I found the following in an Intel development manual:
"As discussed in section 2.3, some compilers do not implicitly recognize the RDTSC and CPUID function in inline assembly code. Compilers like Microsoft® Visual C++® 5.0 normally "guarantee" that any register affected by an inline assembly code section will not affect the C code around it. When overriding the compiler by using the emit statements, however, the compiler does not know those instructions are overwriting registers (RDTSC overwrites EAX and EDX, and CPUID overwrites EAX, EBX, ECX, and EDX). Thus, the compiler may not properly store away the affected registers, so this must be done manually by the programmer by pushing them onto the stack. There are a few cases where this will not matter. If the code being time measured is a stand-alone section of code, completely surrounded by the calls to RDTSC, then the register overwriting cannot affect the code around it. If the measured code section is written in assembly, and the variables are actually used inside of this section, the compiler will handle the stack allocation itself. Finally, it will not matter if affecting the correctness of the code around the measured section is not an issue while cycle testing." Note however, that I did a workaround such that the compiler does NOT in-line the clock sample code, but instead calls a subroutine. The emitted code is still mis-using the edx register a few lines later. It seems certain that the clock sample subroutine is not restoring the edx register when it returns.[/QUOTE] One possible (ugly!) workaround might be: saved_edx_value = save_edx(); stime = get_time(); restored_edx_value(saved_edx_value); where save_edx and restore_edx_value are simple hand-coded _asm routines. But this is [b]such[/b] a kludge! A better way is to rewrite get_time() in a way such that the compiler restores the edx register when it exits. I am unsure how to do that. Here is the current code: /************************************************************************/ /* */ /* Routine to access the clock. On Pentium prectime returns ticks */ /* */ /************************************************************************/ double get_time() { /* start of get_time */ return((double)prectime()); } /* end of get_time */ __int64 prectime(void) { __int64 t; unsigned int a,b; unsigned int *c = (unsigned int *)&t; _asm { _emit 0x0f; _emit 0x31; mov a,eax; mov b,edx; } c[0]=a; c[1]=b; return t; } |
[QUOTE=R.D. Silverman;232318]One possible (ugly!) workaround might be:
saved_edx_value = save_edx(); stime = get_time(); restored_edx_value(saved_edx_value); where save_edx and restore_edx_value are simple hand-coded _asm routines. But this is [b]such[/b] a kludge! A better way is to rewrite get_time() in a way such that the compiler restores the edx register when it exits. I am unsure how to do that. Here is the current code: /************************************************************************/ /* */ /* Routine to access the clock. On Pentium prectime returns ticks */ /* */ /************************************************************************/ double get_time() { /* start of get_time */ return((double)prectime()); } /* end of get_time */ __int64 prectime(void) { __int64 t; unsigned int a,b; unsigned int *c = (unsigned int *)&t; _asm { _emit 0x0f; _emit 0x31; mov a,eax; mov b,edx; } c[0]=a; c[1]=b; return t; }[/QUOTE] I suppose that I could manually insert code into prectime that saves/restores the edx register via: __int64 t; unsigned int a,b,tmp; unsigned int *c = (unsigned int *)&t; _asm { mov tmp, edx _emit 0x0f; _emit 0x31; mov a,eax; mov b,edx; mov edx,tmp } Yech! |
[QUOTE=R.D. Silverman;232319]I suppose that I could manually insert code into prectime that saves/restores
the edx register via: __int64 t; unsigned int a,b,tmp; unsigned int *c = (unsigned int *)&t; _asm { mov tmp, edx _emit 0x0f; _emit 0x31; mov a,eax; mov b,edx; mov edx,tmp } Yech![/QUOTE] Please see my earlier post -- It might not just be the edx register that you need to preserver. eax may also need to be preserved. |
[QUOTE=retina;232294]Unlikely because the debug code show the values being loaded from the stack. It would be extremely strange code that places pointers to the data on the stack AND loads eax/edx with pointers to the data and then calls the subroutine. There is no calling standard that defines that behaviour.[/QUOTE]
Debug code and release code need not have any relation to each other. This is a case where the optimizer is not realizing that eax/edx got clobbered. But without seeing the assembly of how the routine is _called_, it is just speculation. |
Can you post the code of your get_time function? Are you using opcodes using inline assembly or are you using __rdtsc() intrinsic ([url]http://msdn.microsoft.com/en-us/library/twchhe95.aspx[/url])?
|
[QUOTE=axn;232333]Can you post the code of your get_time function? Are you using opcodes using inline assembly or are you using __rdtsc() intrinsic ([url]http://msdn.microsoft.com/en-us/library/twchhe95.aspx[/url])?[/QUOTE]
?? See #23, #24 in this thread! |
[QUOTE=R.D. Silverman;232342]?? See #23, #24 in this thread![/QUOTE]
BTW, I did an [b]explicit[/b] save/restore of eax, edx, and the code now works fine. |
[QUOTE=R.D. Silverman;232343]BTW, I did an [b]explicit[/b] save/restore of eax, edx, and the code
now works fine.[/QUOTE] Oops. Didn't realize that was the code. Can you use the __rdtsc intrinsic instead? As in: [CODE]double get_time() { /* start of get_time */ return((double)__rdtsc()); } /* end of get_time */ [/CODE] Does the compiler recognize that eax/edx is getting clobbered in this case and saves it? |
[QUOTE=axn;232362]Oops. Didn't realize that was the code. Can you use the __rdtsc intrinsic instead? As in:
[CODE]double get_time() { /* start of get_time */ return((double)__rdtsc()); } /* end of get_time */ [/CODE] Does the compiler recognize that eax/edx is getting clobbered in this case and saves it?[/QUOTE] I don't know. It is moot at this point. BTW, when I first wrote this code, the intrinsic did not exist...... |
| All times are UTC. The time now is 08:06. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.