[PATCH 6/6] Replace portableio code. Only portableio.h is required and all functions in the header are inline.
Andres Mejia
mcitadel at gmail.com
Mon Aug 1 17:55:30 UTC 2011
---
frontend/Makefile.am | 1 -
frontend/portableio.c | 490 -------------------------------------------------
frontend/portableio.h | 251 +++++++++++++++++--------
3 files changed, 170 insertions(+), 572 deletions(-)
delete mode 100644 frontend/portableio.c
diff --git a/frontend/Makefile.am b/frontend/Makefile.am
index 0ce4a64..aafca45 100644
--- a/frontend/Makefile.am
+++ b/frontend/Makefile.am
@@ -17,7 +17,6 @@ common_sources = \
get_audio.c \
lametime.c \
parse.c \
- portableio.c \
timestatus.c
noinst_HEADERS = \
diff --git a/frontend/portableio.c b/frontend/portableio.c
deleted file mode 100644
index c890e16..0000000
--- a/frontend/portableio.c
+++ /dev/null
@@ -1,490 +0,0 @@
-/* Copyright (C) 1988-1991 Apple Computer, Inc.
- * All Rights Reserved.
- *
- * Warranty Information
- * Even though Apple has reviewed this software, Apple makes no warranty
- * or representation, either express or implied, with respect to this
- * software, its quality, accuracy, merchantability, or fitness for a
- * particular purpose. As a result, this software is provided "as is,"
- * and you, its user, are assuming the entire risk as to its quality
- * and accuracy.
- *
- * This code may be used and freely distributed as long as it includes
- * this copyright notice and the warranty information.
- *
- *
- * Motorola processors (Macintosh, Sun, Sparc, MIPS, etc)
- * pack bytes from high to low (they are big-endian).
- * Use the HighLow routines to match the native format
- * of these machines.
- *
- * Intel-like machines (PCs, Sequent)
- * pack bytes from low to high (the are little-endian).
- * Use the LowHigh routines to match the native format
- * of these machines.
- *
- * These routines have been tested on the following machines:
- * Apple Macintosh, MPW 3.1 C compiler
- * Apple Macintosh, THINK C compiler
- * Silicon Graphics IRIS, MIPS compiler
- * Cray X/MP and Y/MP
- * Digital Equipment VAX
- *
- *
- * Implemented by Malcolm Slaney and Ken Turkowski.
- *
- * Malcolm Slaney contributions during 1988-1990 include big- and little-
- * endian file I/O, conversion to and from Motorola's extended 80-bit
- * floating-point format, and conversions to and from IEEE single-
- * precision floating-point format.
- *
- * In 1991, Ken Turkowski implemented the conversions to and from
- * IEEE double-precision format, added more precision to the extended
- * conversions, and accommodated conversions involving +/- infinity,
- * NaN's, and denormalized numbers.
- *
- * $Id$
- */
-
-#ifdef HAVE_CONFIG_H
-# include <config.h>
-#endif
-
-#include <stdio.h>
-#if defined(__riscos__) && defined(FPA10)
-#include "ymath.h"
-#else
-#include <math.h>
-#endif
-#include "portableio.h"
-
-#ifdef WITH_DMALLOC
-#include <dmalloc.h>
-#endif
-
-/****************************************************************
- * Big/little-endian independent I/O routines.
- ****************************************************************/
-
-/*
- * It is a hoax to call this code portable-IO:
- *
- * - It doesn't work on machines with CHAR_BIT != 8
- * - it also don't test this error condition
- * - otherwise it tries to handle CHAR_BIT != 8 by things like
- * masking 'putc(i&0xff,fp)'
- * - It doesn't handle EOF in any way
- * - it only works with ints with 32 or more bits
- * - It is a collection of initial buggy code with patching the known errors
- * instead of CORRECTING them!
- * For that see comments on the old Read16BitsHighLow()
- */
-
-#ifdef KLEMM_36
-
-signed int
-ReadByte(FILE * fp)
-{
- int result = getc(fp);
- return result == EOF ? 0 : (signed char) (result & 0xFF);
-}
-
-unsigned int
-ReadByteUnsigned(FILE * fp)
-{
- int result = getc(fp);
- return result == EOF ? 0 : (unsigned char) (result & 0xFF);
-}
-
-#else
-
-int
-ReadByte(FILE * fp)
-{
- int result;
-
- result = getc(fp) & 0xff;
- if (result & 0x80)
- result = result - 0x100;
- return result;
-}
-
-#endif
-
-#ifdef KLEMM_36
-
-int
-Read16BitsLowHigh(FILE * fp)
-{
- int low = ReadByteUnsigned(fp);
- int high = ReadByte(fp);
-
- return (high << 8) | low;
-}
-
-#else
-int
-Read16BitsLowHigh(FILE * fp)
-{
- int first, second, result;
-
- first = 0xff & getc(fp);
- second = 0xff & getc(fp);
-
- result = (second << 8) + first;
-#ifndef THINK_C42
- if (result & 0x8000)
- result = result - 0x10000;
-#endif /* THINK_C */
- return (result);
-}
-#endif
-
-
-#ifdef KLEMM_36
-
-int
-Read16BitsHighLow(FILE * fp)
-{
- int high = ReadByte(fp);
- int low = ReadByteUnsigned(fp);
-
- return (high << 8) | low;
-}
-
-#else
-int
-Read16BitsHighLow(FILE * fp)
-{
- int first, second, result;
-
- /* Reads the High bits, the value is -128...127
- * (which gave after upscaling the -32768...+32512
- * Why this value is not converted to signed char?
- */
- first = 0xff & getc(fp);
- /* Reads the Lows bits, the value is 0...255
- * This is correct. This value gives an additional offset
- * for the High bits
- */
- second = 0xff & getc(fp);
-
- /* This is right */
- result = (first << 8) + second;
-
- /* Now we are starting to correct the nasty bug of the first instruction
- * The value of the high bits is wrong. Always. So we must correct this
- * value. This seems to be not necessary for THINK_C42. This is either
- * a 16 bit compiler with 16 bit ints (where this bug is hidden and 0x10000
- * is not in the scope of an int) or it is not a C compiler, but only a
- * C like compiler. In the first case the '#ifndef THINK_C42' is wrong
- * because it's not a property of the THINK_C42 compiler, but of all compilers
- * with sizeof(int)*CHAR_BIT < 18.
- * Another nasty thing is that the rest of the code doesn't work for 16 bit ints,
- * so this patch don't solve the 16 bit problem.
- */
-#ifndef THINK_C42
- if (result & 0x8000)
- result = result - 0x10000;
-#endif /* THINK_C */
- return (result);
-}
-#endif
-
-void
-Write8Bits(FILE * fp, int i)
-{
- putc(i & 0xff, fp);
-}
-
-
-void
-Write16BitsLowHigh(FILE * fp, int i)
-{
- putc(i & 0xff, fp);
- putc((i >> 8) & 0xff, fp);
-}
-
-
-void
-Write16BitsHighLow(FILE * fp, int i)
-{
- putc((i >> 8) & 0xff, fp);
- putc(i & 0xff, fp);
-}
-
-#ifdef KLEMM_36
-
-int
-Read24BitsHighLow(FILE * fp)
-{
- int high = ReadByte(fp);
- int med = ReadByteUnsigned(fp);
- int low = ReadByteUnsigned(fp);
-
- return (high << 16) | (med << 8) | low;
-}
-
-#else
-int
-Read24BitsHighLow(FILE * fp)
-{
- int first, second, third;
- int result;
-
- first = 0xff & getc(fp);
- second = 0xff & getc(fp);
- third = 0xff & getc(fp);
-
- result = (first << 16) + (second << 8) + third;
- if (result & 0x800000)
- result = result - 0x1000000;
- return (result);
-}
-#endif
-
-#define Read32BitsLowHigh(f) Read32Bits(f)
-
-#ifdef KLEMM_36
-
-int
-Read32Bits(FILE * fp)
-{
- int low = ReadByteUnsigned(fp);
- int medl = ReadByteUnsigned(fp);
- int medh = ReadByteUnsigned(fp);
- int high = ReadByte(fp);
-
- return (high << 24) | (medh << 16) | (medl << 8) | low;
-}
-
-#else
-
-int
-Read32Bits(FILE * fp)
-{
- int first, second, result;
-
- first = 0xffff & Read16BitsLowHigh(fp);
- second = 0xffff & Read16BitsLowHigh(fp);
-
- result = (second << 16) + first;
-#ifdef CRAY
- if (result & 0x80000000)
- result = result - 0x100000000;
-#endif /* CRAY */
- return (result);
-}
-#endif
-
-
-#ifdef KLEMM_36
-
-int
-Read32BitsHighLow(FILE * fp)
-{
- int high = ReadByte(fp);
- int medh = ReadByteUnsigned(fp);
- int medl = ReadByteUnsigned(fp);
- int low = ReadByteUnsigned(fp);
-
- return (high << 24) | (medh << 16) | (medl << 8) | low;
-}
-
-#else
-
-int
-Read32BitsHighLow(FILE * fp)
-{
- int first, second, result;
-
- first = 0xffff & Read16BitsHighLow(fp);
- second = 0xffff & Read16BitsHighLow(fp);
-
- result = (first << 16) + second;
-#ifdef CRAY
- if (result & 0x80000000)
- result = result - 0x100000000;
-#endif
- return (result);
-}
-
-#endif
-
-void
-Write32Bits(FILE * fp, int i)
-{
- Write16BitsLowHigh(fp, (int) (i & 0xffffL));
- Write16BitsLowHigh(fp, (int) ((i >> 16) & 0xffffL));
-}
-
-
-void
-Write32BitsLowHigh(FILE * fp, int i)
-{
- Write16BitsLowHigh(fp, (int) (i & 0xffffL));
- Write16BitsLowHigh(fp, (int) ((i >> 16) & 0xffffL));
-}
-
-
-void
-Write32BitsHighLow(FILE * fp, int i)
-{
- Write16BitsHighLow(fp, (int) ((i >> 16) & 0xffffL));
- Write16BitsHighLow(fp, (int) (i & 0xffffL));
-}
-
-#ifdef KLEMM_36
-void
-ReadBytes(FILE * fp, char *p, int n)
-{
- memset(p, 0, n);
- fread(p, 1, n, fp);
-}
-#else
-void
-ReadBytes(FILE * fp, char *p, int n)
-{
- /* What about fread? */
-
- while (!feof(fp) & (n-- > 0))
- *p++ = (char) getc(fp);
-}
-#endif
-
-void
-ReadBytesSwapped(FILE * fp, char *p, int n)
-{
- register char *q = p;
-
- /* What about fread? */
-
- while (!feof(fp) & (n-- > 0))
- *q++ = (char) getc(fp);
-
- /* If not all bytes could be read, the resorting is different
- * from the normal resorting. Is this intention or another bug?
- */
- for (q--; p < q; p++, q--) {
- n = *p;
- *p = *q;
- *q = (char) n;
- }
-}
-
-#ifdef KLEMM_36
-void
-WriteBytes(FILE * fp, char *p, int n)
-{
- /* return n == */
- fwrite(p, 1, n, fp);
-}
-#else
-void
-WriteBytes(FILE * fp, char *p, int n)
-{
- /* No error condition checking */
- while (n-- > 0)
- putc(*p++, fp);
-}
-#endif
-#ifdef KLEMM_36
-void
-WriteBytesSwapped(FILE * fp, char *p, int n)
-{
- p += n;
- while (n-- > 0)
- putc(*--p, fp);
-}
-#else
-void
-WriteBytesSwapped(FILE * fp, char *p, int n)
-{
- p += n - 1;
- while (n-- > 0)
- putc(*p--, fp);
-}
-#endif
-
-
-
-/****************************************************************
- * The following two routines make up for deficiencies in many
- * compilers to convert properly between unsigned integers and
- * floating-point. Some compilers which have this bug are the
- * THINK_C compiler for the Macintosh and the C compiler for the
- * Silicon Graphics MIPS-based Iris.
- ****************************************************************/
-
-#ifdef applec /* The Apple C compiler works */
-# define FloatToUnsigned(f) ((unsigned long)(f))
-# define UnsignedToFloat(u) ((double)(u))
-#else /* applec */
-# define FloatToUnsigned(f) ((unsigned long)(((long)((f) - 2147483648.0)) + 2147483647L + 1))
-# define UnsignedToFloat(u) (((double)((long)((u) - 2147483647L - 1))) + 2147483648.0)
-#endif /* applec */
-/****************************************************************
- * Extended precision IEEE floating-point conversion routines
- ****************************************************************/
-
-static double
-ConvertFromIeeeExtended(char *bytes)
-{
- double f;
- long expon;
- unsigned long hiMant, loMant;
-
-#ifdef TEST
- printf("ConvertFromIEEEExtended(%lx,%lx,%lx,%lx,%lx,%lx,%lx,%lx,%lx,%lx\r",
- (long) bytes[0], (long) bytes[1], (long) bytes[2], (long) bytes[3],
- (long) bytes[4], (long) bytes[5], (long) bytes[6],
- (long) bytes[7], (long) bytes[8], (long) bytes[9]);
-#endif
-
- expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
- hiMant = ((unsigned long) (bytes[2] & 0xFF) << 24)
- | ((unsigned long) (bytes[3] & 0xFF) << 16)
- | ((unsigned long) (bytes[4] & 0xFF) << 8)
- | ((unsigned long) (bytes[5] & 0xFF));
- loMant = ((unsigned long) (bytes[6] & 0xFF) << 24)
- | ((unsigned long) (bytes[7] & 0xFF) << 16)
- | ((unsigned long) (bytes[8] & 0xFF) << 8)
- | ((unsigned long) (bytes[9] & 0xFF));
-
- /* This case should also be called if the number is below the smallest
- * positive double variable */
- if (expon == 0 && hiMant == 0 && loMant == 0) {
- f = 0;
- }
- else {
- /* This case should also be called if the number is too large to fit into
- * a double variable */
-
- if (expon == 0x7FFF) { /* Infinity or NaN */
- f = HUGE_VAL;
- }
- else {
- expon -= 16383;
- f = ldexp(UnsignedToFloat(hiMant), (int) (expon -= 31));
- f += ldexp(UnsignedToFloat(loMant), (int) (expon -= 32));
- }
- }
-
- if (bytes[0] & 0x80)
- return -f;
- else
- return f;
-}
-
-
-
-
-
-double
-ReadIeeeExtendedHighLow(FILE * fp)
-{
- char bytes[10];
-
- ReadBytes(fp, bytes, 10);
- return ConvertFromIeeeExtended(bytes);
-}
diff --git a/frontend/portableio.h b/frontend/portableio.h
index 98cc048..b219ffe 100644
--- a/frontend/portableio.h
+++ b/frontend/portableio.h
@@ -1,94 +1,183 @@
-#ifndef LAME_PORTABLEIO_H
-#define LAME_PORTABLEIO_H
-/* Copyright (C) 1988-1991 Apple Computer, Inc.
- * All Rights Reserved.
- *
- * Warranty Information
- * Even though Apple has reviewed this software, Apple makes no warranty
- * or representation, either express or implied, with respect to this
- * software, its quality, accuracy, merchantability, or fitness for a
- * particular purpose. As a result, this software is provided "as is,"
- * and you, its user, are assuming the entire risk as to its quality
- * and accuracy.
- *
- * This code may be used and freely distributed as long as it includes
- * this copyright notice and the warranty information.
- *
- * Machine-independent I/O routines for 8-, 16-, 24-, and 32-bit integers.
- *
- * Motorola processors (Macintosh, Sun, Sparc, MIPS, etc)
- * pack bytes from high to low (they are big-endian).
- * Use the HighLow routines to match the native format
- * of these machines.
+/* Simple I/O Function header file
*
- * Intel-like machines (PCs, Sequent)
- * pack bytes from low to high (the are little-endian).
- * Use the LowHigh routines to match the native format
- * of these machines.
+ * Copyright (C) 2011 Andres Mejia
*
- * These routines have been tested on the following machines:
- * Apple Macintosh, MPW 3.1 C compiler
- * Apple Macintosh, THINK C compiler
- * Silicon Graphics IRIS, MIPS compiler
- * Cray X/MP and Y/MP
- * Digital Equipment VAX
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
*
+ * This library 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
+ * Library General Public License for more details.
*
- * Implemented by Malcolm Slaney and Ken Turkowski.
- *
- * Malcolm Slaney contributions during 1988-1990 include big- and little-
- * endian file I/O, conversion to and from Motorola's extended 80-bit
- * floating-point format, and conversions to and from IEEE single-
- * precision floating-point format.
- *
- * In 1991, Ken Turkowski implemented the conversions to and from
- * IEEE double-precision format, added more precision to the extended
- * conversions, and accommodated conversions involving +/- infinity,
- * NaN's, and denormalized numbers.
- *
- * $Id$
+ * You should have received a copy of the GNU Library General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#ifndef LAME_PORTABLEIO_H
+#define LAME_PORTABLEIO_H
+
#include <stdio.h>
+#include <stdint.h>
+#include <math.h>
-#if defined(__cplusplus)
-extern "C" {
-#endif
+#include "config.h"
+
+static inline int ReadByte(FILE * fp)
+{
+ int val = (getc(fp)) & 0xFF;
+ return (val & 0x80) ? (val - 0x100) & 0xFF : val;
+}
+
+static inline int Read16BitsLowHigh(FILE * fp)
+{
+ int val = ReadByte(fp);
+ val |= ReadByte(fp) << 8;
+ return (val & 0x8000) ? (val - 0x10000) & 0xFFFF : val;
+}
+
+static inline int Read16BitsHighLow(FILE * fp)
+{
+ int val = (ReadByte(fp) << 8) | ReadByte(fp);
+ return (val & 0x8000) ? (val - 0x10000) & 0xFFFF : val;
+}
+
+static inline void Write8Bits(FILE * fp, int i)
+{
+ putc(i & 0xFF, fp);
+}
-extern int ReadByte(FILE * fp);
-extern int Read16BitsLowHigh(FILE * fp);
-extern int Read16BitsHighLow(FILE * fp);
-extern void Write8Bits(FILE * fp, int i);
-extern void Write16BitsLowHigh(FILE * fp, int i);
-extern void Write16BitsHighLow(FILE * fp, int i);
-extern int Read24BitsHighLow(FILE * fp);
-extern int Read32Bits(FILE * fp);
-extern int Read32BitsHighLow(FILE * fp);
-extern void Write32Bits(FILE * fp, int i);
-extern void Write32BitsLowHigh(FILE * fp, int i);
-extern void Write32BitsHighLow(FILE * fp, int i);
-extern void ReadBytes(FILE * fp, char *p, int n);
-extern void ReadBytesSwapped(FILE * fp, char *p, int n);
-extern void WriteBytes(FILE * fp, char *p, int n);
-extern void WriteBytesSwapped(FILE * fp, char *p, int n);
-extern double ReadIeeeFloatHighLow(FILE * fp);
-extern double ReadIeeeFloatLowHigh(FILE * fp);
-extern double ReadIeeeDoubleHighLow(FILE * fp);
-extern double ReadIeeeDoubleLowHigh(FILE * fp);
-extern double ReadIeeeExtendedHighLow(FILE * fp);
-extern double ReadIeeeExtendedLowHigh(FILE * fp);
-extern void WriteIeeeFloatLowHigh(FILE * fp, double num);
-extern void WriteIeeeFloatHighLow(FILE * fp, double num);
-extern void WriteIeeeDoubleLowHigh(FILE * fp, double num);
-extern void WriteIeeeDoubleHighLow(FILE * fp, double num);
-extern void WriteIeeeExtendedLowHigh(FILE * fp, double num);
-extern void WriteIeeeExtendedHighLow(FILE * fp, double num);
-
-#define Read32BitsLowHigh(f) Read32Bits(f)
-#define WriteString(f,s) fwrite(s,strlen(s),sizeof(char),f)
-
-#if defined(__cplusplus)
+static inline void Write16BitsLowHigh(FILE * fp, int i)
+{
+ Write8Bits(fp, i & 0xFF);
+ Write8Bits(fp, (i >> 8) & 0xFF);
}
+
+static inline void Write16BitsHighLow(FILE * fp, int i)
+{
+ Write8Bits(fp, (i >> 8) & 0xFF);
+ Write8Bits(fp, i & 0xFF);
+}
+
+static inline int Read24BitsHighLow(FILE * fp)
+{
+ int val = (ReadByte(fp) << 16) | (ReadByte(fp) << 8) | ReadByte(fp);
+ return (val & 0x800000) ? (val - 0x1000000) & 0xFFFFFF : val;
+}
+
+static inline int Read32Bits(FILE * fp)
+{
+ int val = Read16BitsLowHigh(fp);
+ val |= Read16BitsLowHigh(fp) << 16;
+ return val;
+}
+
+static inline int Read32BitsHighLow(FILE * fp)
+{
+ return (Read16BitsHighLow(fp) << 16) | Read16BitsHighLow(fp);
+}
+
+static inline void Write32Bits(FILE * fp, int i)
+{
+ Write16BitsLowHigh(fp, i & 0xFFFF);
+ Write16BitsLowHigh(fp, (i >> 16) & 0xFFFF);
+}
+
+static inline void Write32BitsHighLow(FILE * fp, int i)
+{
+ Write16BitsHighLow(fp, (i >> 16) & 0xFFFF);
+ Write16BitsHighLow(fp, i & 0xFFFF);
+}
+
+static inline void ReadBytes(FILE * fp, char *p, int n)
+{
+ while (!feof(fp) && (n-- > 0))
+ *p++ = ReadByte(fp);
+ while (n-- > 0)
+ *p++ = 0;
+}
+
+static inline void ReadBytesSwapped(FILE * fp, char *p, int n)
+{
+ char *tmp = p;
+ while (!feof(fp) && (n-- > 0))
+ *tmp++ = ReadByte(fp);
+ while (n-- > 0)
+ *tmp++ = 0;
+ tmp--;
+ while (p < tmp)
+ {
+ n = *p;
+ *p++ = *tmp;
+ *tmp-- = n;
+ }
+}
+
+static inline void WriteBytes(FILE * fp, char *p, int n)
+{
+ while (n-- > 0)
+ Write8Bits(fp, *p++);
+}
+
+static inline void WriteBytesSwapped(FILE * fp, char *p, int n)
+{
+ p += n;
+ while (n-- > 0)
+ Write8Bits(fp, *p--);
+}
+
+static inline long double BytesToLongDouble(void *d)
+{
+ long double val = *((long double *)d);
+ return val;
+}
+
+static inline double ReadIeeeExtendedHighLow(FILE * fp)
+{
+ char bytes[10];
+#if (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
+#ifdef WORDS_BIGENDIAN
+ ReadBytes(fp, bytes, sizeof(bytes));
+#else
+ ReadBytesSwapped(fp, bytes, sizeof(bytes));
+#endif
+ /* Take advantage of 80-bit precision of long double from GNU C compiler */
+ return BytesToLongDouble(&bytes);
+#else
+ ReadBytes(fp, &bytes, sizeof(bytes));
+ double val;
+ uint32_t mantissa_high, mantissa_low;
+ int16_t exponent;
+ exponent = ((bytes[0] & 0x7F) << 8) | bytes[1];
+ mantissa_high = (bytes[2] << 24) |
+ (bytes[3] << 16) |
+ (bytes[4] << 8) |
+ bytes[5];
+ mantissa_low = (bytes[6] << 24) |
+ (bytes[7] << 16) |
+ (bytes[8] << 8) |
+ bytes[9];
+ if (exponent == 0 && mantissa_high == 0 && mantissa_low == 0)
+ val = 0;
+ else
+ {
+ /* TODO: Perhaps this should also detect NaN */
+ if (exponent == 0x7FFF)
+ val = HUGE_VAL;
+ else
+ {
+ exponent -= 0x3FFF; /* Bias for long double precision */
+ val = ldexp(mantissa_high, (exponent -= 31));
+ val += ldexp(mantissa_low, (exponent -= 32));
+ }
+ }
+ return (bytes[0] & 0x80) ? -val : val;
#endif
+}
+
+#define Read32BitsLowHigh(f) Read32Bits(f)
+#define Write32BitsLowHigh(f, i) Write32Bits(f, i)
#endif
--
1.7.5.4
More information about the pkg-multimedia-maintainers
mailing list