Subject: Float / Double issues

From: Tim Hockin <[email-masked]>
Date: Mon, 12 Jul 1999 20:09:41 -0700

Hey gang - I have what seems to be two seperate issues on Mips/Linux
(cobalt boxes).

1) Programs using doubles with pthreads get corrupted data in the
doubles.
2) Floats and va_arg crash with an FPE

below are two snippets that show these, at least on our boxes.  If
someone could confirm or deny their existence on any other platform?

Tim

#include <stdio.h>
#include <pthread.h>
#include <stdarg.h>

void doit(int, double, unsigned int);
void vait(unsigned int, ...);
void *ptfn(void *);
void doit2(int, int);
void vait2(int, ...);
void *ptfn2(void *);

int main() 
{
	pthread_t pt1;
	double d1 = 1.234; 

	printf("Without threads: \n");
	ptfn((void *)&d1);
	printf("\n");
	fflush(stdout);

	printf("With threads: \n");
	pthread_create(&pt1, NULL, ptfn, &d1);
	printf("\n");
	fflush(stdout);

	pthread_exit(0);
	return 0;
}

void *ptfn(void *x)
{
	char ar[16];
	unsigned int decimals = 5;
	double d = *(double *)x;

	doit(1, d, decimals);
	vait(decimals, d);
}

void doit(int x, double d, unsigned int ui)
{
	char buff[331];

	sprintf(buff,"double: %.*f",ui,d);
	printf("%s\n", buff);
}

void vait(unsigned int ui, ...)
{
	double d=-10.0;
	va_list va;

	va_start(va, ui);

	printf("double before va_arg: %f\n", d);
	d = va_arg(va, double);
	printf("double after va_arg: %f\n", d);

	va_end(va);

	doit(1, d, ui);
}
#include <stdio.h>
#include <stdarg.h>

void doit(int, float, unsigned int);
void vait(unsigned int, ...);
void *ptfn(void *);

int main() 
{
	float d1 = 1.234; 

	printf("Without threads: \n");
	ptfn((void *)&d1);
	printf("\n");
	fflush(stdout);

	return 0;
}

void *ptfn(void *x)
{
	char ar[16];
	unsigned int decimals = 5;
	float d = *(float *)x;

	doit(1, d, decimals);
	vait(decimals, d);
}

void doit(int x, float d, unsigned int ui)
{
	char buff[331];

	sprintf(buff,"float: %.*f",ui,d);
	printf("%s\n", buff);
}

void vait(unsigned int ui, ...)
{
	float d=-10.0;
	va_list va;

	va_start(va, ui);

	printf("float before va_arg: %f\n", d);
	d = va_arg(va, float);
	printf("float after va_arg: %f\n", d);

	va_end(va);

	doit(1, d, ui);
}

---

From: Ralf Baechle <[email-masked]>
Date: Wed, 14 Jul 1999 22:52:22 +0200

> someone could confirm or deny their existence on any other platform?

The float / va_arg bug is a pilot error.  See ANSI C draft chapter 6.5.2.2,
paragraph 6 and7.  In short the problem is that float parameters will be
promoted to double.  You therefore have to to change the call of va_arg
in your float.c to va_arg(va, float).

Obvious, heh ;-)

Btw, SGI's compiler also doesn't attempt to get va_arg(..., float) right.

  Ralf

---

From: Ralf Baechle <[email-masked]>
Date: Wed, 14 Jul 1999 23:53:46 +0200

> 1) Programs using doubles with pthreads get corrupted data in the

That one is funny.  It was/is a longstanding libc bug originally reported
by Dong Liu.  I actually thought it'd be fixed but now thanks to your
report I see it's still broken.  So the multithreaded variant of your
double.c doesn't work at all on our libc.

The bug is in glibc/sysdeps/unix/sysv/linux/mips/clone.S; I suppose
Cobalt's libc which I last worked on late October is still using my
old clone.S from that time or somebody else there came up with a funky
new bug completly on it's own - which might explain why double.c cannot
even successfully create the threads.

More later ...

  Ralf

---

From: Ralf Baechle <[email-masked]>
Date: Thu, 15 Jul 1999 02:38:57 +0200

> even successfully create the threads.

It is later :-)

Attached are fixed versions of clone.S.  I also attach Dong's old test
program.  While now threads are working dong.c still fails for a large
number of threads, that is > 1000 or so with a SIGSEGV.  This of course
doesn't solve your problem yet, Tim - but now we've got the same problem ...

  Ralf

---

From: Tim Hockin <[email-masked]>
Date: Wed, 14 Jul 1999 18:18:29 -0700

> even successfully create the threads.

We have the fix, I think.  Not clone.S, but in
glibc-2.0.7/linuxthreads/internals.h line 86 we need to tell gcc that this
struct (_pthread_descr_struct - which defines the offset of a thread's stack
from a well-aligned, malloc() returned addr) be padded to be aligned
correctly on an 8byte boundary:

-};
+} __attribute__ ((aligned(__alignof__(double))));

It's working for us now.  Please confirm if it works for you folks, too.  A
bug report has been filed for glibc, though I doubt if anything official
goes on for 2.0.x, now. Hopefully 2.1.x will fold this in.  A thread's stack
really should always be well aligned :)  Funny, the doubles were actually
calculated right, but stdio functions use va_arg - which expected double's
to be aligned, and weren't.  The work got done, but it couldn't tell us!

As for the va_arg "bug" - I realized it was pilot error shortly after
sending it out.  I just needed to look at something that was NOT this double
bug for a while. :)  So the new question is who wants to do thread support
in gdb !!

Tim

---

From: Ralf Baechle <[email-masked]>
Date: Fri, 16 Jul 1999 02:57:32 +0200

On Wed, Jul 14, 1999 at 06:18:29PM -0700, Tim Hockin wrote:

> calculated right,

It's dangerous though.  If the code would have attempted to use a MIPS II
ldc1 / sdc1 instruction to access the floating point number, the code
would have been killed by arch/mips/kernel/unaligned.c.

> but stdio functions use va_arg - which expected double's

The MIPS ABI wants a 8 byte alignment for the stack and even 16 byte for
ABI64.  Now we know what can happen if not :-)

Thanks for your fix,

  Ralf