Subject: port-arm32/9925: __nanf not correctly aligned
To: None <gnats-bugs@gnats.netbsd.org>
From: Richard Earnshaw <rearnsha@cambridge.arm.com>
List: netbsd-bugs
Date: 04/18/2000 09:46:13
>Number:         9925
>Category:       port-arm32
>Synopsis:       __nanf not correctly aligned
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    port-arm32-maintainer
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Apr 18 09:47:00 PDT 2000
>Closed-Date:
>Last-Modified:
>Originator:     Richard Earnshaw
>Release:        All<NetBSD-current source date>
>Organization:
ARM
-- 
>Environment:
	
System: NetBSD shark1 1.4X NetBSD 1.4X (SHARK) #52: Mon Mar 27 17:55:21 BST 2000 rearnsha@shark1:/usr/src/sys/arch/arm32/compile/SHARK arm32


>Description:
	The program below demonstrates that __nanf (the float NaN) is not
	correctly aligned.  So that attempts to read it as a float give a
	garbage answer.  A similar problem potentially exists for Infinity,
	though by luck it currently doesn't trigger.
	
>How-To-Repeat:
	Compile and run the following.

#include <math.h>
#include <stdio.h>

union d {
  double d;
  struct {unsigned a, b;} bits;
};

union f {
  float f;
  struct {unsigned a;} bits;
};

int main()
{
  union d x;
  union f y;
  x.d = NAN;
  y.f = NAN;
  printf("bits: %08x %08x\n", x.bits.a, x.bits.b);
  printf("bits: %08x\n", y.bits.a);
  printf("+/-inifinity, EARLY:\n");
  printf(" HUGE_VAL: % g\n", HUGE_VAL);
  printf("-HUGE_VAL: % g\n",-HUGE_VAL);

  printf("invalid:\n");
#ifdef NAN
  printf("NAN: %g, isnan(NAN): %i\n",NAN,isnan(NAN));
#else
  printf("2min: %g\n",DBL_MIN+DBL_MIN);
#endif

  return 0;
}


	
>Fix:

	__nanf must be aligned.  There are two ways to make it so:

	const char __nanf[] __attribute__((aligned(2))) = { 0, 0, (char)0xc0, 0x7f };

	or

	const unsigned long __nanf[] = {0x7fc00000};

	depending on your preference for gcc extensions.

	A similar patch is needed for __infinity[].

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