This is the mail archive of the gcc-prs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

c/1642: problem with inline assembler: result variable not finished (?) when used



>Number:         1642
>Category:       c
>Synopsis:       problem with inline assembler: result variable not finished (?) when used
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sat Jan 13 02:46:01 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     mirar@mirar.org
>Release:        gcc version 2.97 20010108 (experimental)
>Organization:
>Environment:
tsunami% uname -a
Linux tsunami.roxen.se 2.4.0-test12 #13 SMP Tue Jan 9 00:08:33 CET 2001 i686 unknown
>Description:
I know I'm using an experimental version, and I know
I'm using inline assembler, but before I revert to 
a more stable version I thought I should mention a 
possible error (or you tell me what I did wrong):

This function:

long long gethrtime()
{
   static int n=0;
   long long now;
...
   fprintf(stderr,"%d %Lf %lld %Lf\n",n,hrtime_conv,now,now*hrtime_conv);
   fprintf(stderr,"%d %Lf %lld %Lf\n",n,hrtime_conv,now,now*hrtime_conv);
   n++;
...
}

print outs

0 1.586797 1332934056304 2115095965335.408524
0 1.586797 9729595119984 15438893833718.518690

ie, "now" changes even though it's a local variable.

If I did something wrong, please explain.
>How-To-Repeat:
This is the program with all neccesary context:

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>

static long long hrtime_rtsc_zero;
static long long hrtime_rtsc_last;
static struct timeval hrtime_timeval_zero;
static long double hrtime_conv=0.0;

#define RTSC(x)   							\
   __asm__ __volatile__ (  "rdtsc"                			\
			   :"=a" (((unsigned long*)&x)[0]), 		\
			   "=d" (((unsigned long*)&x)[1]))

#define GETTIMEOFDAY(X) gettimeofday((X),(void *)0)

void own_gethrtime_init()
{
   RTSC(hrtime_rtsc_zero);
   hrtime_rtsc_last=hrtime_rtsc_zero;
   GETTIMEOFDAY(&hrtime_timeval_zero);
}

void own_gethrtime_update(struct timeval *ptr)
{
   long long td,t,now;
   RTSC(now);
   GETTIMEOFDAY(ptr);
   td=((long long)ptr->tv_sec-hrtime_timeval_zero.tv_sec)*1000000000+
      ((long long)ptr->tv_usec-hrtime_timeval_zero.tv_usec)*1000;
   hrtime_rtsc_last=now;
   t=now-hrtime_rtsc_zero;
   if (t) hrtime_conv=((long double)td)/t;
}

long long gethrtime()
{
   static int n=0;
   long long now;
   struct timeval dummy;

   if (hrtime_conv==0.0) own_gethrtime_update(&dummy);

   RTSC(now);

#if 0
/* 2 seconds between updates */
   if (now-hrtime_rtsc_last > 2000000000) 
   {
      own_gethrtime_update(&dummy);
      return gethrtime();
   }
#endif

   fprintf(stderr,"%d %Lf %lld %Lf\n",n,hrtime_conv,now,now*hrtime_conv);
   fprintf(stderr,"%d %Lf %lld %Lf\n",n,hrtime_conv,now,now*hrtime_conv);
   n++;

   return (long long) ( (long double)now * hrtime_conv );
}

int main()
{
   own_gethrtime_init();
   usleep(10);
   gethrtime();
}

Resulting in this assembler (-Wall -O3 -S, no warnings):

	.file	"testgcc.c"
	.version	"01.01"
gcc2_compiled.:
	.data
	.align 16
	.type	hrtime_conv,@object
	.size	hrtime_conv,12
hrtime_conv:
	.long	0x0,0x0,0x0
	.align 4
	.type	n.0,@object
	.size	n.0,4
n.0:
	.long	0
	.section	.rodata
.LC1:
	.string	"%d %Lf %lld %Lf\n"
	.text
	.align 16
.globl gethrtime
	.type	gethrtime,@function
gethrtime:
	fldt	hrtime_conv
	pushl	%ebp
	movl	%esp, %ebp
	fldz
	fxch	%st(1)
	pushl	%edi
	pushl	%esi
	fucompp
	fnstsw	%ax
	pushl	%ebx
	andb	$69, %ah
	subl	$60, %esp
	xorb	$64, %ah
	jne	.L21
#APP
	rdtsc
#NO_APP
	movl	$1000000000, %edi
	subl	$8, %esp
	leal	-40(%ebp), %ebx
	pushl	$0
	pushl	%ebx
	movl	%edx, -44(%ebp)
	movl	%eax, -48(%ebp)
	call	gettimeofday
	movl	-40(%ebp), %eax
	movl	%eax, %esi
	movl	%eax, %ebx
	movl	hrtime_timeval_zero, %eax
	sarl	$31, %ebx
	cltd
	subl	%eax, %esi
	movl	%esi, %eax
	sbbl	%edx, %ebx
	mull	%edi
	movl	%eax, %esi
	movl	-36(%ebp), %eax
	movl	%edx, %edi
	movl	%ebx, %edx
	imull	$1000000000, %edx, %edx
	movl	%eax, %ecx
	movl	%eax, %ebx
	movl	hrtime_timeval_zero+4, %eax
	sarl	$31, %ebx
	addl	%edx, %edi
	cltd
	subl	%eax, %ecx
	movl	%ecx, %eax
	sbbl	%edx, %ebx
	movl	%ebx, %edx
	shldl	$2, %eax, %edx
	sall	$2, %eax
	addl	%ecx, %eax
	adcl	%ebx, %edx
	movl	%eax, %ecx
	movl	%edx, %ebx
	shldl	$2, %ecx, %ebx
	sall	$2, %ecx
	addl	%eax, %ecx
	adcl	%edx, %ebx
	movl	%ecx, %eax
	movl	%ebx, %edx
	shldl	$2, %eax, %edx
	sall	$2, %eax
	movl	%eax, -64(%ebp)
	addl	%ecx, -64(%ebp)
	movl	%edx, -60(%ebp)
	movl	-44(%ebp), %edx
	movl	-64(%ebp), %ecx
	adcl	%ebx, -60(%ebp)
	movl	%edx, hrtime_rtsc_last+4
	movl	-60(%ebp), %eax
	shldl	$3, %ecx, %eax
	sall	$3, %ecx
	movl	%ecx, -64(%ebp)
	movl	%eax, -60(%ebp)
	movl	-48(%ebp), %eax
	addl	%esi, -64(%ebp)
	movl	%eax, hrtime_rtsc_last
	adcl	%edi, -60(%ebp)
	subl	hrtime_rtsc_zero, %eax
	movl	%eax, %ebx
	sbbl	hrtime_rtsc_zero+4, %edx
	addl	$16, %esp
	orl	%edx, %ebx
	je	.L21
	pushl	%edx
	pushl	%eax
	fildll	(%esp)
	addl	$8, %esp
	fildll	-64(%ebp)
	fdivp	%st, %st(1)
	fstpt	hrtime_conv
.L21:
#APP
	rdtsc
#NO_APP
	movl	n.0, %edi
	movl	-52(%ebp), %esi
	subl	$4, %esp
	movl	%eax, -56(%ebp)
	movl	-56(%ebp), %eax
	pushl	%esi
	fldt	hrtime_conv
	pushl	%eax
	fildll	(%esp)
	subl	$4, %esp
	movl	%edx, -52(%ebp)
	movl	stderr, %edx
	fmul	%st(1), %st
	fstpt	(%esp)
	pushl	%esi
	pushl	%eax
	subl	$12, %esp
	fstpt	(%esp)
	pushl	%edi
	pushl	$.LC1
	pushl	%edx
	call	fprintf
	fldt	hrtime_conv
	movl	-52(%ebp), %ecx
	addl	$44, %esp
	movl	-56(%ebp), %eax
	pushl	%ecx
	pushl	%eax
	fildll	(%esp)
	subl	$4, %esp
	fmul	%st(1), %st
	fstpt	(%esp)
	pushl	%ecx
	pushl	%eax
	movl	n.0, %eax
	subl	$12, %esp
	fstpt	(%esp)
	pushl	%eax
	movl	stderr, %eax
	pushl	$.LC1
	pushl	%eax
	call	fprintf
	fldt	hrtime_conv
	addl	$48, %esp
	fildll	-56(%ebp)
	incl	n.0
	fmulp	%st, %st(1)
	fnstcw	-20(%ebp)
	movl	-20(%ebp), %ecx
	movb	$12, -19(%ebp)
	fldcw	-20(%ebp)
	movl	%ecx, -20(%ebp)
	fistpll	-32(%ebp)
	fldcw	-20(%ebp)
	movl	-32(%ebp), %eax
	movl	-28(%ebp), %edx
	leal	-12(%ebp), %esp
	popl	%ebx
	popl	%esi
	popl	%edi
	popl	%ebp
	ret
.Lfe1:
	.size	gethrtime,.Lfe1-gethrtime
	.local	hrtime_rtsc_zero
	.comm	hrtime_rtsc_zero,8,8
	.local	hrtime_rtsc_last
	.comm	hrtime_rtsc_last,8,8
	.local	hrtime_timeval_zero
	.comm	hrtime_timeval_zero,8,4
	.align 16
.globl own_gethrtime_init
	.type	own_gethrtime_init,@function
own_gethrtime_init:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
#APP
	rdtsc
#NO_APP
	movl	hrtime_rtsc_zero+4, %ecx
	movl	%eax, hrtime_rtsc_zero
	movl	hrtime_rtsc_zero, %eax
	pushl	$0
	pushl	$hrtime_timeval_zero
	movl	%edx, hrtime_rtsc_zero+4
	movl	%eax, hrtime_rtsc_last
	movl	%ecx, hrtime_rtsc_last+4
	call	gettimeofday
	addl	$16, %esp
	movl	%ebp, %esp
	popl	%ebp
	ret
.Lfe2:
	.size	own_gethrtime_init,.Lfe2-own_gethrtime_init
	.align 16
.globl own_gethrtime_update
	.type	own_gethrtime_update,@function
own_gethrtime_update:
	pushl	%ebp
	movl	%esp, %ebp
	pushl	%edi
	pushl	%esi
	pushl	%ebx
	subl	$36, %esp
#APP
	rdtsc
#NO_APP
	movl	$1000000000, %edi
	movl	%eax, -24(%ebp)
	movl	8(%ebp), %eax
	pushl	$0
	movl	%edx, -20(%ebp)
	pushl	%eax
	call	gettimeofday
	movl	8(%ebp), %ecx
	movl	(%ecx), %eax
	movl	%eax, %esi
	movl	%eax, %ebx
	movl	hrtime_timeval_zero, %eax
	sarl	$31, %ebx
	cltd
	subl	%eax, %esi
	movl	%esi, %eax
	sbbl	%edx, %ebx
	mull	%edi
	movl	%edx, %edi
	movl	%ebx, %edx
	movl	8(%ebp), %ebx
	movl	%eax, %esi
	imull	$1000000000, %edx, %edx
	movl	4(%ebx), %eax
	addl	%edx, %edi
	movl	%eax, %ecx
	movl	%eax, %ebx
	movl	hrtime_timeval_zero+4, %eax
	sarl	$31, %ebx
	cltd
	subl	%eax, %ecx
	movl	%ecx, %eax
	sbbl	%edx, %ebx
	movl	%ebx, %edx
	shldl	$2, %eax, %edx
	sall	$2, %eax
	addl	%ecx, %eax
	adcl	%ebx, %edx
	movl	%eax, %ecx
	movl	%edx, %ebx
	shldl	$2, %ecx, %ebx
	sall	$2, %ecx
	addl	%eax, %ecx
	adcl	%edx, %ebx
	movl	%ecx, %eax
	movl	%ebx, %edx
	shldl	$2, %eax, %edx
	sall	$2, %eax
	movl	%eax, -32(%ebp)
	addl	%ecx, -32(%ebp)
	movl	%edx, -28(%ebp)
	movl	-20(%ebp), %edx
	movl	-32(%ebp), %ecx
	adcl	%ebx, -28(%ebp)
	movl	%edx, hrtime_rtsc_last+4
	movl	-28(%ebp), %eax
	shldl	$3, %ecx, %eax
	sall	$3, %ecx
	movl	%ecx, -32(%ebp)
	movl	%eax, -28(%ebp)
	movl	-24(%ebp), %eax
	addl	%esi, -32(%ebp)
	movl	%eax, hrtime_rtsc_last
	adcl	%edi, -28(%ebp)
	subl	hrtime_rtsc_zero, %eax
	movl	%eax, %ebx
	sbbl	hrtime_rtsc_zero+4, %edx
	addl	$16, %esp
	orl	%edx, %ebx
	je	.L19
	pushl	%edx
	pushl	%eax
	fildll	(%esp)
	addl	$8, %esp
	fildll	-32(%ebp)
	fdivp	%st, %st(1)
	fstpt	hrtime_conv
.L19:
	leal	-12(%ebp), %esp
	popl	%ebx
	popl	%esi
	popl	%edi
	popl	%ebp
	ret
.Lfe3:
	.size	own_gethrtime_update,.Lfe3-own_gethrtime_update
	.align 16
.globl main
	.type	main,@function
main:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
#APP
	rdtsc
#NO_APP
	movl	hrtime_rtsc_zero+4, %ecx
	movl	%eax, hrtime_rtsc_zero
	movl	hrtime_rtsc_zero, %eax
	movl	%edx, hrtime_rtsc_zero+4
	movl	%ecx, hrtime_rtsc_last+4
	movl	%eax, hrtime_rtsc_last
	pushl	$0
	pushl	$hrtime_timeval_zero
	call	gettimeofday
	movl	$10, (%esp)
	call	usleep
	addl	$16, %esp
	movl	%ebp, %esp
	popl	%ebp
	jmp	gethrtime
.Lfe4:
	.size	main,.Lfe4-main
	.ident	"GCC: (GNU) 2.97 20010108 (experimental)"

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]