This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re: undefined behavior if sizeof(mp_limb_t)>sizeof(long)
- From: Alexandre Oliva <aoliva at redhat dot com>
- To: Roland McGrath <roland at redhat dot com>
- Cc: libc-alpha at sources dot redhat dot com
- Date: 13 Mar 2003 21:52:00 -0300
- Subject: Re: undefined behavior if sizeof(mp_limb_t)>sizeof(long)
- Organization: GCC Team, Red Hat
- References: <200303101931.h2AJVFC12055@magilla.sf.frob.com>
On Mar 10, 2003, Roland McGrath <roland at redhat dot com> wrote:
>> This is one I had missed in the mips64-linux port I posted two months
>> ago, still awaiting review. Is there any interest in my extracting
>> the similar bits from that patch and submitting them separately?
> Yes, please do.
Here they are. Ok to install?
Index: ChangeLog
from Alexandre Oliva <aoliva at redhat dot com>
* sysdeps/ieee754/dbl-64/dbl2mpn.c (__mpn_extract_double):
Cast shifted values that may be too narrow to mp_limb_t.
* sysdeps/ieee754/dbl-64/mpn2dbl.c (__mpn_construct_double):
Likewise.
* sysdeps/ieee754/flt-32/mpn2flt.c (__mpn_construct_float):
Likewise.
* sysdeps/ieee754/ldbl-128/ldbl2mpn.c
(__mpn_extract_long_double): Likewise.
* sysdeps/ieee754/ldbl-128/mpn2ldbl.c
(__mpn_construct_long_double): Likewise.
* sysdeps/ieee754/ldbl-96/ldbl2mpn.c
(__mpn_extract_long_double): Likewise.
* sysdeps/ieee754/ldbl-96/mpn2ldbl.c
(__mpn_construct_long_double): Likewise.
Index: sysdeps/ieee754/dbl-64/dbl2mpn.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/ieee754/dbl-64/dbl2mpn.c,v
retrieving revision 1.2
diff -u -p -r1.2 dbl2mpn.c
--- sysdeps/ieee754/dbl-64/dbl2mpn.c 6 Jul 2001 04:55:54 -0000 1.2
+++ sysdeps/ieee754/dbl-64/dbl2mpn.c 14 Mar 2003 00:50:17 -0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
+/* Copyright 1993, 1994, 1995, 1996, 1997, 2002, 2003
+ Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -45,7 +46,7 @@ __mpn_extract_double (mp_ptr res_ptr, mp
#elif BITS_PER_MP_LIMB == 64
/* Hopefully the compiler will combine the two bitfield extracts
and this composition into just the original quadword extract. */
- res_ptr[0] = ((unsigned long int) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
+ res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
#define N 1
#else
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
@@ -101,7 +102,8 @@ __mpn_extract_double (mp_ptr res_ptr, mp
}
else
/* Add the implicit leading one bit for a normalized number. */
- res_ptr[N - 1] |= 1L << (DBL_MANT_DIG - 1 - ((N - 1) * BITS_PER_MP_LIMB));
+ res_ptr[N - 1] |= (mp_limb_t) 1 << (DBL_MANT_DIG - 1
+ - ((N - 1) * BITS_PER_MP_LIMB));
return N;
}
Index: sysdeps/ieee754/dbl-64/mpn2dbl.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/ieee754/dbl-64/mpn2dbl.c,v
retrieving revision 1.2
diff -u -p -r1.2 mpn2dbl.c
--- sysdeps/ieee754/dbl-64/mpn2dbl.c 6 Jul 2001 04:55:55 -0000 1.2
+++ sysdeps/ieee754/dbl-64/mpn2dbl.c 14 Mar 2003 00:50:18 -0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
+/* Copyright 1995, 1996, 1997, 2002, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -34,10 +34,12 @@ __mpn_construct_double (mp_srcptr frac_p
u.ieee.exponent = expt + IEEE754_DOUBLE_BIAS;
#if BITS_PER_MP_LIMB == 32
u.ieee.mantissa1 = frac_ptr[0];
- u.ieee.mantissa0 = frac_ptr[1] & ((1 << (DBL_MANT_DIG - 32)) - 1);
+ u.ieee.mantissa0 = frac_ptr[1] & (((mp_limb_t) 1
+ << (DBL_MANT_DIG - 32)) - 1);
#elif BITS_PER_MP_LIMB == 64
- u.ieee.mantissa1 = frac_ptr[0] & ((1L << 32) - 1);
- u.ieee.mantissa0 = (frac_ptr[0] >> 32) & ((1 << (DBL_MANT_DIG - 32)) - 1);
+ u.ieee.mantissa1 = frac_ptr[0] & (((mp_limb_t) 1 << 32) - 1);
+ u.ieee.mantissa0 = (frac_ptr[0] >> 32) & (((mp_limb_t) 1
+ << (DBL_MANT_DIG - 32)) - 1);
#else
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
#endif
Index: sysdeps/ieee754/flt-32/mpn2flt.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/ieee754/flt-32/mpn2flt.c,v
retrieving revision 1.2
diff -u -p -r1.2 mpn2flt.c
--- sysdeps/ieee754/flt-32/mpn2flt.c 6 Jul 2001 04:55:55 -0000 1.2
+++ sysdeps/ieee754/flt-32/mpn2flt.c 14 Mar 2003 00:50:18 -0000
@@ -1,4 +1,4 @@
-/* Copyright (C) 1995, 1997 Free Software Foundation, Inc.
+/* Copyright 1995, 1997, 2002, 2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -33,7 +33,7 @@ __mpn_construct_float (mp_srcptr frac_pt
u.ieee.negative = sign;
u.ieee.exponent = expt + IEEE754_FLOAT_BIAS;
#if BITS_PER_MP_LIMB > FLT_MANT_DIG
- u.ieee.mantissa = frac_ptr[0] & ((1 << FLT_MANT_DIG) - 1);
+ u.ieee.mantissa = frac_ptr[0] & (((mp_limb_t) 1 << FLT_MANT_DIG) - 1);
#else
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
#endif
Index: sysdeps/ieee754/ldbl-128/ldbl2mpn.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/ieee754/ldbl-128/ldbl2mpn.c,v
retrieving revision 1.3
diff -u -p -r1.3 ldbl2mpn.c
--- sysdeps/ieee754/ldbl-128/ldbl2mpn.c 11 Jul 2002 03:10:27 -0000 1.3
+++ sysdeps/ieee754/ldbl-128/ldbl2mpn.c 14 Mar 2003 00:50:19 -0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+/* Copyright 1995, 1996, 1997, 1998, 1999, 2002, 2003
+ Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -49,8 +50,8 @@ __mpn_extract_long_double (mp_ptr res_pt
#elif BITS_PER_MP_LIMB == 64
/* Hopefully the compiler will combine the two bitfield extracts
and this composition into just the original quadword extract. */
- res_ptr[0] = ((unsigned long int) u.ieee.mantissa2 << 32) | u.ieee.mantissa3;
- res_ptr[1] = ((unsigned long int) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
+ res_ptr[0] = ((mp_limb_t) u.ieee.mantissa2 << 32) | u.ieee.mantissa3;
+ res_ptr[1] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
#define N 2
#else
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
@@ -133,7 +134,8 @@ __mpn_extract_long_double (mp_ptr res_pt
}
else
/* Add the implicit leading one bit for a normalized number. */
- res_ptr[N - 1] |= 1L << (LDBL_MANT_DIG - 1 - ((N - 1) * BITS_PER_MP_LIMB));
+ res_ptr[N - 1] |= (mp_limb_t) 1 << (LDBL_MANT_DIG - 1
+ - ((N - 1) * BITS_PER_MP_LIMB));
return N;
}
Index: sysdeps/ieee754/ldbl-128/mpn2ldbl.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/ieee754/ldbl-128/mpn2ldbl.c,v
retrieving revision 1.2
diff -u -p -r1.2 mpn2ldbl.c
--- sysdeps/ieee754/ldbl-128/mpn2ldbl.c 6 Jul 2001 04:55:55 -0000 1.2
+++ sysdeps/ieee754/ldbl-128/mpn2ldbl.c 14 Mar 2003 00:50:19 -0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995, 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
+/* Copyright 1995, 1996, 1997, 1998, 1999, 2002, 2003
+ Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -37,12 +38,14 @@ __mpn_construct_long_double (mp_srcptr f
u.ieee.mantissa3 = frac_ptr[0];
u.ieee.mantissa2 = frac_ptr[1];
u.ieee.mantissa1 = frac_ptr[2];
- u.ieee.mantissa0 = frac_ptr[3] & ((1 << (LDBL_MANT_DIG - 96)) - 1);
+ u.ieee.mantissa0 = frac_ptr[3] & (((mp_limb_t) 1
+ << (LDBL_MANT_DIG - 96)) - 1);
#elif BITS_PER_MP_LIMB == 64
- u.ieee.mantissa3 = frac_ptr[0] & ((1L << 32) - 1);
+ u.ieee.mantissa3 = frac_ptr[0] & (((mp_limb_t) 1 << 32) - 1);
u.ieee.mantissa2 = frac_ptr[0] >> 32;
- u.ieee.mantissa1 = frac_ptr[1] & ((1L << 32) - 1);
- u.ieee.mantissa0 = (frac_ptr[1] >> 32) & ((1 << (LDBL_MANT_DIG - 96)) - 1);
+ u.ieee.mantissa1 = frac_ptr[1] & (((mp_limb_t) 1 << 32) - 1);
+ u.ieee.mantissa0 = (frac_ptr[1] >> 32) & (((mp_limb_t) 1
+ << (LDBL_MANT_DIG - 96)) - 1);
#else
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
#endif
Index: sysdeps/ieee754/ldbl-96/ldbl2mpn.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/ieee754/ldbl-96/ldbl2mpn.c,v
retrieving revision 1.2
diff -u -p -r1.2 ldbl2mpn.c
--- sysdeps/ieee754/ldbl-96/ldbl2mpn.c 6 Jul 2001 04:55:55 -0000 1.2
+++ sysdeps/ieee754/ldbl-96/ldbl2mpn.c 14 Mar 2003 00:50:19 -0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+/* Copyright 1995, 1996, 1997, 1998, 2002, 2003
+ Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -47,7 +48,7 @@ __mpn_extract_long_double (mp_ptr res_pt
#elif BITS_PER_MP_LIMB == 64
/* Hopefully the compiler will combine the two bitfield extracts
and this composition into just the original quadword extract. */
- res_ptr[0] = ((unsigned long int) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
+ res_ptr[0] = ((mp_limb_t) u.ieee.mantissa0 << 32) | u.ieee.mantissa1;
#define N 1
#else
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
Index: sysdeps/ieee754/ldbl-96/mpn2ldbl.c
===================================================================
RCS file: /cvs/glibc/libc/sysdeps/ieee754/ldbl-96/mpn2ldbl.c,v
retrieving revision 1.2
diff -u -p -r1.2 mpn2ldbl.c
--- sysdeps/ieee754/ldbl-96/mpn2ldbl.c 6 Jul 2001 04:55:55 -0000 1.2
+++ sysdeps/ieee754/ldbl-96/mpn2ldbl.c 14 Mar 2003 00:50:19 -0000
@@ -1,4 +1,5 @@
-/* Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
+/* Copyright 1995, 1996, 1997, 1998, 2002, 2003
+ Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -37,7 +38,7 @@ __mpn_construct_long_double (mp_srcptr f
u.ieee.mantissa1 = frac_ptr[0];
u.ieee.mantissa0 = frac_ptr[1];
#elif BITS_PER_MP_LIMB == 64
- u.ieee.mantissa1 = frac_ptr[0] & ((1L << 32) - 1);
+ u.ieee.mantissa1 = frac_ptr[0] & (((mp_limb_t) 1 << 32) - 1);
u.ieee.mantissa0 = frac_ptr[0] >> 32;
#else
#error "mp_limb size " BITS_PER_MP_LIMB "not accounted for"
--
Alexandre Oliva Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer aoliva at {redhat dot com, gcc.gnu.org}
CS PhD student at IC-Unicamp oliva at {lsd dot ic dot unicamp dot br, gnu.org}
Free Software Evangelist Professional serial bug killer