From owner-freebsd-current Fri Dec 20 19:57:35 2002 Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 4AC6137B401 for ; Fri, 20 Dec 2002 19:57:33 -0800 (PST) Received: from smtp04.iprimus.com.au (smtp04.iprimus.com.au [210.50.76.52]) by mx1.FreeBSD.org (Postfix) with ESMTP id F167B43EDA for ; Fri, 20 Dec 2002 19:57:31 -0800 (PST) (envelope-from tim@robbins.dropbear.id.au) Received: from smtp02.iprimus.net.au (210.50.76.70) by smtp04.iprimus.com.au (6.7.010) id 3DF583C30016D123 for current@FreeBSD.ORG; Sat, 21 Dec 2002 14:57:25 +1100 Received: from dilbert.robbins.dropbear.id.au ([210.50.81.103]) by smtp02.iprimus.net.au with Microsoft SMTPSVC(5.0.2195.5600); Sat, 21 Dec 2002 14:57:23 +1100 Received: from dilbert.robbins.dropbear.id.au (5o52yzyngjmtqj1g@localhost [127.0.0.1]) by dilbert.robbins.dropbear.id.au (8.12.6/8.12.6) with ESMTP id gBL3vmCv014790; Sat, 21 Dec 2002 14:57:49 +1100 (EST) (envelope-from tim@dilbert.robbins.dropbear.id.au) Received: (from tim@localhost) by dilbert.robbins.dropbear.id.au (8.12.6/8.12.6/Submit) id gBL3vf7N014789; Sat, 21 Dec 2002 14:57:41 +1100 (EST) (envelope-from tim) Date: Sat, 21 Dec 2002 14:57:41 +1100 From: Tim Robbins To: Joe Marcus Clarke Cc: current@FreeBSD.ORG Subject: Re: WEIRD! div() broken on -CURRENT? Message-ID: <20021221145741.A14018@dilbert.robbins.dropbear.id.au> References: <1040437478.29101.23.camel@shumai.marcuscom.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.5.1i In-Reply-To: <1040437478.29101.23.camel@shumai.marcuscom.com>; from marcus@marcuscom.com on Fri, Dec 20, 2002 at 09:24:39PM -0500 X-OriginalArrivalTime: 21 Dec 2002 03:57:24.0638 (UTC) FILETIME=[121743E0:01C2A8A5] Sender: owner-freebsd-current@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG On Fri, Dec 20, 2002 at 09:24:39PM -0500, Joe Marcus Clarke wrote: > Okay, I must be losing my mind. Does anyone know why the following > program compiled with stock gcc-3.2.1, stock CFLAGS, and no CPUTYPE > produces: > > ddy.quot = 1 > ddy.rem = -1077937744 > > on -CURRENT, and: > > ddy.quot = 8 > ddy.rem = 0 > > On -stable? > > #include > #include > > main(void) { > div_t ddy; > int dy, dy_frac; > > ddy = div (768, 96); > dy = ddy.quot; > dy_frac = ddy.rem; > > printf("ddy.quot = %d\n", dy); > printf("ddy.rem = %d\n", dy_frac); > > return 0; > } > > > cc -O -pipe -o xxx xxx.c > > I'm doing something wrong, right? I mean, this can't be right. I've > verified this now on a P4 running: [...] I can reproduce it here. It looks like gcc is using a strange calling convention that the i386 div.S does not understand (MI div.c seems to, though). It's generating the following code: 0x8048541 : mov $0x0,%eax 0x8048546 : sub %eax,%esp 0x8048548 : lea 0xfffffff8(%ebp),%eax 0x804854b : sub $0x4,%esp 0x804854e : push $0x60 0x8048550 : push $0x300 0x8048555 : push %eax 0x8048556 : call 0x80483ec
0x804855b : add $0xc,%esp At the time of the call to div(): (gdb) x/4w $esp 0xbfbffbbc: 0x0804855b 0xbfbffbe8 0x00000300 0x00000060 It looks like gcc might be pushing the address of "ddy" and expecting div() to put the result in there. Microsoft C 12 generates this code: sub esp, 8 push 96 ; 00000060H push 768 ; 00000300H call _div add esp, 8 mov DWORD PTR [ebp], eax mov DWORD PTR [ebp+4], edx Tim To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-current" in the body of the message