[Xorp-hackers] [PATCH] Made fp64 serialization compatible with pre-C99.

ss at comp.lancs.ac.uk ss at comp.lancs.ac.uk
Tue Sep 13 14:50:18 PDT 2011


From: Steven Simpson <ss at comp.lancs.ac.uk>

* Copyright blocks use /* */, not //.

* Macros to build type-specific function names and macros don't
  add any suffix.

* Feature macros tested for presence of fpclassify.  Basic
  implementation used when not present.

* Feature macros tested for presence of signbit.  Basic
  implementation used when not present.

* Optional INFINITY detected; HUGE_VAL used instead.

* Lack of NAN detected; HUGE_VAL used instead.

Signed-off-by: Steven Simpson <ss at comp.lancs.ac.uk>
---
 xorp/libxipc/fp64.h       |   55 ++++++++++++++++---------
 xorp/libxipc/fp64serial.c |   95 +++++++++++++++++++++++++++++++++++----------
 xorp/libxipc/fp64serial.h |   40 ++++++++++---------
 3 files changed, 130 insertions(+), 60 deletions(-)

diff --git a/xorp/libxipc/fp64.h b/xorp/libxipc/fp64.h
index 8d44239..335c73a 100644
--- a/xorp/libxipc/fp64.h
+++ b/xorp/libxipc/fp64.h
@@ -1,23 +1,25 @@
-// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
-// vim:set sts=4 ts=8:
-
-// Copyright (c) 2001-2011 XORP, Inc and Others
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License, Version
-// 2.1, June 1999 as published by the Free Software Foundation.
-// Redistribution and/or modification of this program under the terms of
-// any other version of the GNU Lesser General Public License is not
-// permitted.
-//
-// 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. For more details,
-// see the GNU Lesser General Public License, Version 2.1, a copy of
-// which can be found in the XORP LICENSE.lgpl file.
-//
-// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
-// http://xorp.net
+/* -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
+ * vim:set sts=4 ts=8: */
+
+/*
+ * Copyright (c) 2001-2011 XORP, Inc and Others
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License, Version
+ * 2.1, June 1999 as published by the Free Software Foundation.
+ * Redistribution and/or modification of this program under the terms of
+ * any other version of the GNU Lesser General Public License is not
+ * permitted.
+ *
+ * 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. For more details,
+ * see the GNU Lesser General Public License, Version 2.1, a copy of
+ * which can be found in the XORP LICENSE.lgpl file.
+ *
+ * XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
+ * http://xorp.net
+ */
 
 #ifndef __LIBXIPC_FP64_H__
 #define __LIBXIPC_FP64_H__
@@ -128,7 +130,13 @@ typedef float fp64_t;
 #define XORP_SCNeFP64 "e"
 #define XORP_SCNfFP64 "f"
 #define XORP_SCNgFP64 "g"
+#if __STDC_VERSION__ >= 199901L
 #define XORP_FP64(F) F ## f
+#define XORP_FP64UC(FN) FN ## F
+#else
+#define XORP_FP64(F) F
+#define XORP_FP64UC(FN) FN
+#endif
 #define XORP_FP64_DIG FLT_FP64_DIG
 #define XORP_FP64_EPSILON FLT_FP64_EPSILON
 #define XORP_FP64_MANT_DIG FLT_FP64_MANT_DIG
@@ -155,6 +163,7 @@ typedef double fp64_t;
 #define XORP_SCNfFP64 "lf"
 #define XORP_SCNgFP64 "lg"
 #define XORP_FP64(F) F
+#define XORP_FP64UC(F) F
 #define XORP_FP64_DIG DBL_FP64_DIG
 #define XORP_FP64_EPSILON DBL_FP64_EPSILON
 #define XORP_FP64_MANT_DIG DBL_FP64_MANT_DIG
@@ -180,7 +189,13 @@ typedef long double fp64_t;
 #define XORP_SCNeFP64 "Le"
 #define XORP_SCNfFP64 "Lf"
 #define XORP_SCNgFP64 "Lg"
+#if __STDC_VERSION__ >= 199901L
 #define XORP_FP64(F) F ## l
+#define XORP_FP64UC(F) F ## L
+#else
+#define XORP_FP64(F) F
+#define XORP_FP64UC(F) F
+#endif
 #define XORP_FP64_DIG LDBL_FP64_DIG
 #define XORP_FP64_EPSILON LDBL_FP64_EPSILON
 #define XORP_FP64_MANT_DIG LDBL_FP64_MANT_DIG
diff --git a/xorp/libxipc/fp64serial.c b/xorp/libxipc/fp64serial.c
index c943ef3..fde5149 100644
--- a/xorp/libxipc/fp64serial.c
+++ b/xorp/libxipc/fp64serial.c
@@ -1,23 +1,25 @@
-// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
-// vim:set sts=4 ts=8:
-
-// Copyright (c) 2001-2011 XORP, Inc and Others
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License, Version
-// 2.1, June 1999 as published by the Free Software Foundation.
-// Redistribution and/or modification of this program under the terms of
-// any other version of the GNU Lesser General Public License is not
-// permitted.
-//
-// 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. For more details,
-// see the GNU Lesser General Public License, Version 2.1, a copy of
-// which can be found in the XORP LICENSE.lgpl file.
-//
-// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
-// http://xorp.net
+/* -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
+ * vim:set sts=4 ts=8: */
+
+/*
+ * Copyright (c) 2001-2011 XORP, Inc and Others
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License, Version
+ * 2.1, June 1999 as published by the Free Software Foundation.
+ * Redistribution and/or modification of this program under the terms of
+ * any other version of the GNU Lesser General Public License is not
+ * permitted.
+ *
+ * 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. For more details,
+ * see the GNU Lesser General Public License, Version 2.1, a copy of
+ * which can be found in the XORP LICENSE.lgpl file.
+ *
+ * XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
+ * http://xorp.net
+ */
 
 #include <stdlib.h>
 #include <stdbool.h>
@@ -44,6 +46,48 @@
 /* How much is the exponent biased? */
 #define EXPONENT_BIAS ((EXPONENT_MASK >> 1u) - 1u)
 
+#if __STDC_VERSION__ >= 199901L || \
+    _XOPEN_SOURCE >= 600 || \
+    _ISOC99_SOURCE || \
+    _POSIX_C_SOURCE >= 200112L
+
+/* fpclassify is available. */
+#else
+/* We can't guarantee that fpclassify exists, so define a simple
+   implementation that at least picks out zero. */
+#undef FP_ZERO
+#undef FP_INFINITE
+#undef FP_NAN
+#undef FP_NORMAL
+#undef FP_SUBNORMAL
+#undef fpclassify
+
+#define FP_ZERO      0
+#define FP_INFINITE  1
+#define FP_NAN       2
+#define FP_NORMAL    3
+#define FP_SUBNORMAL 4
+
+#define fpclassify(X) \
+    ((X) == 0.0 ? FP_ZERO :			\
+	(X) >= +XORP_FP64UC(HUGE_VAL) ? FP_INFINITE : \
+	(X) <= -XORP_FP64UC(HUGE_VAL) ? FP_INFINITE : \
+	(X) != (X) ? FP_NAN : \
+     FP_NORMAL)
+#endif
+
+
+#if __STDC_VERSION__ >= 199901L || \
+    _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE || _POSIX_C_SOURCE >= 200112L
+/* signbit is available. */
+#else
+/* We can't guarantee that signbit exists.  Define a simple
+   implementation that probably won't do worse than fail to detect -ve
+   zero. */
+#undef signbit
+#define signbit(X) ((X) < 0)
+
+#endif
 
 uint_fast64_t fp64enc(fp64_t input)
 {
@@ -146,9 +190,18 @@ fp64_t fp64dec(uint_fast64_t bytes)
     neg = (bytes >> SIGN_SHIFT) & SIGN_MASK;
 
     if (u_exp == EXPONENT_MASK) {
-	if (u_mant == 0)
+	if (u_mant == 0) {
+#ifdef INFINITY
 	    return neg ? -INFINITY : +INFINITY;
+#else
+	    return neg ? -XORP_FP64UC(HUGE_VAL) : +XORP_FP64UC(HUGE_VAL);
+#endif
+	}
+#ifdef NAN
 	return NAN;
+#else
+	return HUGE_VAL;
+#endif
     }
 
 
diff --git a/xorp/libxipc/fp64serial.h b/xorp/libxipc/fp64serial.h
index 03d1daa..ccbd299 100644
--- a/xorp/libxipc/fp64serial.h
+++ b/xorp/libxipc/fp64serial.h
@@ -1,23 +1,25 @@
-// -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
-// vim:set sts=4 ts=8:
+/* -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
+ * vim:set sts=4 ts=8: */
 
-// Copyright (c) 2001-2011 XORP, Inc and Others
-//
-// This program is free software; you can redistribute it and/or modify
-// it under the terms of the GNU Lesser General Public License, Version
-// 2.1, June 1999 as published by the Free Software Foundation.
-// Redistribution and/or modification of this program under the terms of
-// any other version of the GNU Lesser General Public License is not
-// permitted.
-//
-// 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. For more details,
-// see the GNU Lesser General Public License, Version 2.1, a copy of
-// which can be found in the XORP LICENSE.lgpl file.
-//
-// XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
-// http://xorp.net
+/*
+ * Copyright (c) 2001-2011 XORP, Inc and Others
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License, Version
+ * 2.1, June 1999 as published by the Free Software Foundation.
+ * Redistribution and/or modification of this program under the terms of
+ * any other version of the GNU Lesser General Public License is not
+ * permitted.
+ *
+ * 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. For more details,
+ * see the GNU Lesser General Public License, Version 2.1, a copy of
+ * which can be found in the XORP LICENSE.lgpl file.
+ *
+ * XORP, Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
+ * http://xorp.net
+ */
 
 #ifndef __LIBXIPC_FP64SERIAL_H__
 #define __LIBXIPC_FP64SERIAL_H__
-- 
1.7.4.1



More information about the Xorp-hackers mailing list