[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
support for bitwise comparison of floats
From: |
Bruno Haible |
Subject: |
support for bitwise comparison of floats |
Date: |
Sun, 25 Mar 2007 02:04:35 +0100 |
User-agent: |
KMail/1.5.4 |
Hi,
How can one distinguish +0.0 and -0.0. printf() needs to be able to do it.
How can this be done portably? I can see two ways:
a) By knowing the bit position of the sign bit.
b) By doing a bit-for-bit comparison against +0.0.
The second approach appears to be implementable with less portability
problems. But it requires to know the extent of the float. For 'long double'
on x86, sizeof (long double) = 12, but only the first 10 bytes are to be
compared; the remaining two bytes are junk. It is not possible to set this
junk to 0 by statements like
memset (&x, 0, sizeof (long double));
x = y;
because sometimes gcc emits code for 'x = y' that copies 12 bytes, i.e.
including the junk bits from y.
So we must somehow know that a 'long double' is only 10 bytes effectively.
2007-03-24 Bruno Haible <address@hidden>
* lib/float+.h: New file.
* lib/isnan.c: Include float+.h.
(SIZE): New macro.
(FUNC): Compare only SIZE bytes of the value.
* lib/vasnprintf.c: Include float+.h.
(VASNPRINTF): When comparing agains +0.0L or +0.0, compare only
SIZEOF_LDBL or SIZEOF_DBL bytes.
* modules/isnan-nolibm (Files): Add lib/float+.h.
* modules/isnanl-nolibm (Files): Add lib/float+.h.
* modules/isnanl (Files): Add lib/float+.h.
* modules/vasnprintf (Files): Add lib/float+.h.
=========================== lib/float+.h ==================================
/* Supplemental information about the floating-point formats.
Copyright (C) 2007 Free Software Foundation, Inc.
Written by Bruno Haible <address@hidden>, 2007.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#ifndef _FLOATPLUS_H
#define _FLOATPLUS_H
#include <float.h>
#include <limits.h>
/* Number of bits in the mantissa of a floating-point number, including the
"hidden bit". */
#if FLT_RADIX == 2
# define FLT_MANT_BIT FLT_MANT_DIG
# define DBL_MANT_BIT DBL_MANT_DIG
# define LDBL_MANT_BIT LDBL_MANT_DIG
#elif FLT_RADIX == 4
# define FLT_MANT_BIT (FLT_MANT_DIG * 2)
# define DBL_MANT_BIT (DBL_MANT_DIG * 2)
# define LDBL_MANT_BIT (LDBL_MANT_DIG * 2)
#elif FLT_RADIX == 16
# define FLT_MANT_BIT (FLT_MANT_DIG * 4)
# define DBL_MANT_BIT (DBL_MANT_DIG * 4)
# define LDBL_MANT_BIT (LDBL_MANT_DIG * 4)
#endif
/* Bit mask that can be used to mask the exponent, as an unsigned number. */
#define FLT_EXP_MASK ((FLT_MAX_EXP - FLT_MIN_EXP) | 7)
#define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
#define LDBL_EXP_MASK ((LDBL_MAX_EXP - LDBL_MIN_EXP) | 7)
/* Number of bits used for the exponent of a floating-point number, including
the exponent's sign. */
#define FLT_EXP_BIT \
(FLT_EXP_MASK < 0x100 ? 8 : \
FLT_EXP_MASK < 0x200 ? 9 : \
FLT_EXP_MASK < 0x400 ? 10 : \
FLT_EXP_MASK < 0x800 ? 11 : \
FLT_EXP_MASK < 0x1000 ? 12 : \
FLT_EXP_MASK < 0x2000 ? 13 : \
FLT_EXP_MASK < 0x4000 ? 14 : \
FLT_EXP_MASK < 0x8000 ? 15 : \
FLT_EXP_MASK < 0x10000 ? 16 : \
FLT_EXP_MASK < 0x20000 ? 17 : \
FLT_EXP_MASK < 0x40000 ? 18 : \
FLT_EXP_MASK < 0x80000 ? 19 : \
FLT_EXP_MASK < 0x100000 ? 20 : \
FLT_EXP_MASK < 0x200000 ? 21 : \
FLT_EXP_MASK < 0x400000 ? 22 : \
FLT_EXP_MASK < 0x800000 ? 23 : \
FLT_EXP_MASK < 0x1000000 ? 24 : \
FLT_EXP_MASK < 0x2000000 ? 25 : \
FLT_EXP_MASK < 0x4000000 ? 26 : \
FLT_EXP_MASK < 0x8000000 ? 27 : \
FLT_EXP_MASK < 0x10000000 ? 28 : \
FLT_EXP_MASK < 0x20000000 ? 29 : \
FLT_EXP_MASK < 0x40000000 ? 30 : \
FLT_EXP_MASK <= 0x7fffffff ? 31 : \
32)
#define DBL_EXP_BIT \
(DBL_EXP_MASK < 0x100 ? 8 : \
DBL_EXP_MASK < 0x200 ? 9 : \
DBL_EXP_MASK < 0x400 ? 10 : \
DBL_EXP_MASK < 0x800 ? 11 : \
DBL_EXP_MASK < 0x1000 ? 12 : \
DBL_EXP_MASK < 0x2000 ? 13 : \
DBL_EXP_MASK < 0x4000 ? 14 : \
DBL_EXP_MASK < 0x8000 ? 15 : \
DBL_EXP_MASK < 0x10000 ? 16 : \
DBL_EXP_MASK < 0x20000 ? 17 : \
DBL_EXP_MASK < 0x40000 ? 18 : \
DBL_EXP_MASK < 0x80000 ? 19 : \
DBL_EXP_MASK < 0x100000 ? 20 : \
DBL_EXP_MASK < 0x200000 ? 21 : \
DBL_EXP_MASK < 0x400000 ? 22 : \
DBL_EXP_MASK < 0x800000 ? 23 : \
DBL_EXP_MASK < 0x1000000 ? 24 : \
DBL_EXP_MASK < 0x2000000 ? 25 : \
DBL_EXP_MASK < 0x4000000 ? 26 : \
DBL_EXP_MASK < 0x8000000 ? 27 : \
DBL_EXP_MASK < 0x10000000 ? 28 : \
DBL_EXP_MASK < 0x20000000 ? 29 : \
DBL_EXP_MASK < 0x40000000 ? 30 : \
DBL_EXP_MASK <= 0x7fffffff ? 31 : \
32)
#define LDBL_EXP_BIT \
(LDBL_EXP_MASK < 0x100 ? 8 : \
LDBL_EXP_MASK < 0x200 ? 9 : \
LDBL_EXP_MASK < 0x400 ? 10 : \
LDBL_EXP_MASK < 0x800 ? 11 : \
LDBL_EXP_MASK < 0x1000 ? 12 : \
LDBL_EXP_MASK < 0x2000 ? 13 : \
LDBL_EXP_MASK < 0x4000 ? 14 : \
LDBL_EXP_MASK < 0x8000 ? 15 : \
LDBL_EXP_MASK < 0x10000 ? 16 : \
LDBL_EXP_MASK < 0x20000 ? 17 : \
LDBL_EXP_MASK < 0x40000 ? 18 : \
LDBL_EXP_MASK < 0x80000 ? 19 : \
LDBL_EXP_MASK < 0x100000 ? 20 : \
LDBL_EXP_MASK < 0x200000 ? 21 : \
LDBL_EXP_MASK < 0x400000 ? 22 : \
LDBL_EXP_MASK < 0x800000 ? 23 : \
LDBL_EXP_MASK < 0x1000000 ? 24 : \
LDBL_EXP_MASK < 0x2000000 ? 25 : \
LDBL_EXP_MASK < 0x4000000 ? 26 : \
LDBL_EXP_MASK < 0x8000000 ? 27 : \
LDBL_EXP_MASK < 0x10000000 ? 28 : \
LDBL_EXP_MASK < 0x20000000 ? 29 : \
LDBL_EXP_MASK < 0x40000000 ? 30 : \
LDBL_EXP_MASK <= 0x7fffffff ? 31 : \
32)
/* Number of bits used for a floating-point number: the mantissa (not
counting the "hidden bit", since it may or may not be explicit), the
exponent, and the sign. */
#define FLT_TOTAL_BIT ((FLT_MANT_BIT - 1) + FLT_EXP_BIT + 1)
#define DBL_TOTAL_BIT ((DBL_MANT_BIT - 1) + DBL_EXP_BIT + 1)
#define LDBL_TOTAL_BIT ((LDBL_MANT_BIT - 1) + LDBL_EXP_BIT + 1)
/* Number of bytes used for a floating-point number.
This can be smaller than the 'sizeof'. For example, on i386 systems,
'long double' most often have LDBL_MANT_BIT = 64, LDBL_EXP_BIT = 16, hence
LDBL_TOTAL_BIT = 80 bits, i.e. 10 bytes of consecutive memory, but
sizeof (long double) = 12 or = 16. */
#define SIZEOF_FLT ((FLT_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
#define SIZEOF_DBL ((DBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
#define SIZEOF_LDBL ((LDBL_TOTAL_BIT + CHAR_BIT - 1) / CHAR_BIT)
/* Verify that SIZEOF_FLT <= sizeof (float) etc. */
typedef int verify_sizeof_flt[2 * (SIZEOF_FLT <= sizeof (float)) - 1];
typedef int verify_sizeof_dbl[2 * (SIZEOF_DBL <= sizeof (double)) - 1];
#if HAVE_LONG_DOUBLE
typedef int verify_sizeof_ldbl[2 * (SIZEOF_LDBL <= sizeof (long double)) - 1];
#endif
#endif /* _FLOATPLUS_H */
===========================================================================
*** lib/isnan.c 11 Mar 2007 22:40:52 -0000 1.3
--- lib/isnan.c 25 Mar 2007 00:45:01 -0000
***************
*** 22,27 ****
--- 22,29 ----
#include <float.h>
#include <string.h>
+ #include "float+.h"
+
#ifdef USE_LONG_DOUBLE
# define FUNC rpl_isnanl
# define DOUBLE long double
***************
*** 32,37 ****
--- 34,40 ----
# define EXPBIT0_WORD LDBL_EXPBIT0_WORD
# define EXPBIT0_BIT LDBL_EXPBIT0_BIT
# endif
+ # define SIZE SIZEOF_LDBL
# define L_(literal) literal##L
#else
# define FUNC rpl_isnan
***************
*** 43,48 ****
--- 46,52 ----
# define EXPBIT0_WORD DBL_EXPBIT0_WORD
# define EXPBIT0_BIT DBL_EXPBIT0_BIT
# endif
+ # define SIZE SIZEOF_DBL
# define L_(literal) literal
#endif
***************
*** 79,86 ****
if (((m.word[EXPBIT0_WORD] ^ nan.word[EXPBIT0_WORD])
& (EXP_MASK << EXPBIT0_BIT))
== 0)
! return (memcmp (&m.value, &plus_inf, sizeof (DOUBLE)) != 0
! && memcmp (&m.value, &minus_inf, sizeof (DOUBLE)) != 0);
else
return 0;
}
--- 83,90 ----
if (((m.word[EXPBIT0_WORD] ^ nan.word[EXPBIT0_WORD])
& (EXP_MASK << EXPBIT0_BIT))
== 0)
! return (memcmp (&m.value, &plus_inf, SIZE) != 0
! && memcmp (&m.value, &minus_inf, SIZE) != 0);
else
return 0;
}
*** lib/vasnprintf.c 25 Mar 2007 00:49:34 -0000 1.33
--- lib/vasnprintf.c 25 Mar 2007 00:50:13 -0000
***************
*** 51,56 ****
--- 54,60 ----
#include "xsize.h"
#if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
+ # include "float+.h"
# include "isnan.h"
# include "printf-frexp.h"
# if HAVE_LONG_DOUBLE
***************
*** 424,433 ****
{
/* Distinguish 0.0L and -0.0L. */
static long double plus_zero = 0.0L;
! long double arg_mem;
! memset (&arg_mem, 0, sizeof (long double));
! arg_mem = arg;
! if (memcmp (&plus_zero, &arg_mem, sizeof (long
double)) != 0)
{
sign = -1;
arg = -arg;
--- 432,439 ----
{
/* Distinguish 0.0L and -0.0L. */
static long double plus_zero = 0.0L;
! long double arg_mem = arg;
! if (memcmp (&plus_zero, &arg_mem, SIZEOF_LDBL) != 0)
{
sign = -1;
arg = -arg;
***************
*** 574,583 ****
{
/* Distinguish 0.0 and -0.0. */
static double plus_zero = 0.0;
! double arg_mem;
! memset (&arg_mem, 0, sizeof (double));
! arg_mem = arg;
! if (memcmp (&plus_zero, &arg_mem, sizeof (double))
!= 0)
{
sign = -1;
arg = -arg;
--- 589,596 ----
{
/* Distinguish 0.0 and -0.0. */
static double plus_zero = 0.0;
! double arg_mem = arg;
! if (memcmp (&plus_zero, &arg_mem, SIZEOF_DBL) != 0)
{
sign = -1;
arg = -arg;
*** modules/isnan-nolibm 24 Feb 2007 19:08:56 -0000 1.1
--- modules/isnan-nolibm 25 Mar 2007 00:45:01 -0000
***************
*** 4,9 ****
--- 4,10 ----
Files:
lib/isnan.h
lib/isnan.c
+ lib/float+.h
m4/isnan.m4
Depends-on:
*** modules/isnanl 22 Mar 2007 02:18:35 -0000 1.1
--- modules/isnanl 25 Mar 2007 00:45:01 -0000
***************
*** 5,10 ****
--- 5,11 ----
lib/isnanl.h
lib/isnanl.c
lib/isnan.c
+ lib/float+.h
m4/isnanl.m4
m4/longdouble.m4
*** modules/isnanl-nolibm 22 Mar 2007 02:03:21 -0000 1.3
--- modules/isnanl-nolibm 25 Mar 2007 00:45:01 -0000
***************
*** 5,10 ****
--- 5,11 ----
lib/isnanl-nolibm.h
lib/isnanl.c
lib/isnan.c
+ lib/float+.h
m4/isnanl.m4
m4/longdouble.m4
*** modules/vasnprintf 27 Oct 2006 17:11:53 -0000 1.11
--- modules/vasnprintf 25 Mar 2007 00:45:01 -0000
***************
*** 6,11 ****
--- 6,12 ----
lib/printf-args.c
lib/printf-parse.h
lib/printf-parse.c
+ lib/float+.h
lib/vasnprintf.h
lib/vasnprintf.c
lib/asnprintf.c
- support for bitwise comparison of floats,
Bruno Haible <=
- Re: support for bitwise comparison of floats, Eric Blake, 2007/03/24
- Re: support for bitwise comparison of floats, Bruno Haible, 2007/03/25
- Re: support for bitwise comparison of floats, Eric Blake, 2007/03/25
- Re: support for bitwise comparison of floats, Bruno Haible, 2007/03/25
- Re: support for bitwise comparison of floats, Eric Blake, 2007/03/25
- Re: support for bitwise comparison of floats, Ben Pfaff, 2007/03/25
Re: support for bitwise comparison of floats, Eric Blake, 2007/03/25