home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 13 / AACD13.ISO / AACD / Sound / LAME / src / portableio.c < prev    next >
C/C++ Source or Header  |  2000-06-07  |  7KB  |  360 lines

  1. /* Copyright (C) 1988-1991 Apple Computer, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * Warranty Information
  5.  * Even though Apple has reviewed this software, Apple makes no warranty
  6.  * or representation, either express or implied, with respect to this
  7.  * software, its quality, accuracy, merchantability, or fitness for a
  8.  * particular purpose.  As a result, this software is provided "as is,"
  9.  * and you, its user, are assuming the entire risk as to its quality
  10.  * and accuracy.
  11.  *
  12.  * This code may be used and freely distributed as long as it includes
  13.  * this copyright notice and the warranty information.
  14.  *
  15.  *
  16.  * Motorola processors (Macintosh, Sun, Sparc, MIPS, etc)
  17.  * pack bytes from high to low (they are big-endian).
  18.  * Use the HighLow routines to match the native format
  19.  * of these machines.
  20.  *
  21.  * Intel-like machines (PCs, Sequent)
  22.  * pack bytes from low to high (the are little-endian).
  23.  * Use the LowHigh routines to match the native format
  24.  * of these machines.
  25.  *
  26.  * These routines have been tested on the following machines:
  27.  *    Apple Macintosh, MPW 3.1 C compiler
  28.  *    Apple Macintosh, THINK C compiler
  29.  *    Silicon Graphics IRIS, MIPS compiler
  30.  *    Cray X/MP and Y/MP
  31.  *    Digital Equipment VAX
  32.  *
  33.  *
  34.  * Implemented by Malcolm Slaney and Ken Turkowski.
  35.  *
  36.  * Malcolm Slaney contributions during 1988-1990 include big- and little-
  37.  * endian file I/O, conversion to and from Motorola's extended 80-bit
  38.  * floating-point format, and conversions to and from IEEE single-
  39.  * precision floating-point format.
  40.  *
  41.  * In 1991, Ken Turkowski implemented the conversions to and from
  42.  * IEEE double-precision format, added more precision to the extended
  43.  * conversions, and accommodated conversions involving +/- infinity,
  44.  * NaN's, and denormalized numbers.
  45.  *
  46.  * $Id: portableio.c,v 1.2 2000/06/07 22:56:02 sbellon Exp $
  47.  *
  48.  * $Log: portableio.c,v $
  49.  * Revision 1.2  2000/06/07 22:56:02  sbellon
  50.  * added support for FPA10 hardware (RISC OS only)
  51.  *
  52.  * Revision 1.1.1.1  1999/11/24 08:43:35  markt
  53.  * initial checkin of LAME
  54.  * Starting with LAME 3.57beta with some modifications
  55.  *
  56.  * Revision 2.6  91/04/30  17:06:02  malcolm
  57.  */
  58.  
  59. #include    <stdio.h>
  60. #if defined(__riscos__) && defined(FPA10)
  61. #include    "ymath.h"
  62. #else
  63. #include    <math.h>
  64. #endif
  65. #include    "portableio.h"
  66.  
  67. /****************************************************************
  68.  * Big/little-endian independent I/O routines.
  69.  ****************************************************************/
  70.  
  71.  
  72. int
  73. ReadByte(FILE *fp)
  74. {
  75.     int    result;
  76.  
  77.     result = getc(fp) & 0xff;
  78.     if (result & 0x80)
  79.         result = result - 0x100;
  80.     return result;
  81. }
  82.  
  83.  
  84. int
  85. Read16BitsLowHigh(FILE *fp)
  86. {
  87.     int    first, second, result;
  88.  
  89.     first = 0xff & getc(fp);
  90.     second = 0xff & getc(fp);
  91.  
  92.     result = (second << 8) + first;
  93. #ifndef    THINK_C42
  94.     if (result & 0x8000)
  95.         result = result - 0x10000;
  96. #endif    /* THINK_C */
  97.     return(result);
  98. }
  99.  
  100.  
  101. int
  102. Read16BitsHighLow(FILE *fp)
  103. {
  104.     int    first, second, result;
  105.  
  106.     first = 0xff & getc(fp);
  107.     second = 0xff & getc(fp);
  108.  
  109.     result = (first << 8) + second;
  110. #ifndef    THINK_C42
  111.     if (result & 0x8000)
  112.         result = result - 0x10000;
  113. #endif    /* THINK_C */
  114.     return(result);
  115. }
  116.  
  117.  
  118. void
  119. Write8Bits(FILE *fp, int i)
  120. {
  121.     putc(i&0xff,fp);
  122. }
  123.  
  124.  
  125. void
  126. Write16BitsLowHigh(FILE *fp, int i)
  127. {
  128.     putc(i&0xff,fp);
  129.     putc((i>>8)&0xff,fp);
  130. }
  131.  
  132.  
  133. void
  134. Write16BitsHighLow(FILE *fp, int i)
  135. {
  136.     putc((i>>8)&0xff,fp);
  137.     putc(i&0xff,fp);
  138. }
  139.  
  140.  
  141. int
  142. Read24BitsHighLow(FILE *fp)
  143. {
  144.     int    first, second, third;
  145.     int    result;
  146.  
  147.     first = 0xff & getc(fp);
  148.     second = 0xff & getc(fp);
  149.     third = 0xff & getc(fp);
  150.  
  151.     result = (first << 16) + (second << 8) + third;
  152.     if (result & 0x800000)
  153.         result = result - 0x1000000;
  154.     return(result);
  155. }
  156.  
  157. #define    Read32BitsLowHigh(f)    Read32Bits(f)
  158.  
  159.  
  160. int
  161. Read32Bits(FILE *fp)
  162. {
  163.     int    first, second, result;
  164.  
  165.     first = 0xffff & Read16BitsLowHigh(fp);
  166.     second = 0xffff & Read16BitsLowHigh(fp);
  167.  
  168.     result = (second << 16) + first;
  169. #ifdef    CRAY
  170.     if (result & 0x80000000)
  171.         result = result - 0x100000000;
  172. #endif    /* CRAY */
  173.     return(result);
  174. }
  175.  
  176.  
  177. int
  178. Read32BitsHighLow(FILE *fp)
  179. {
  180.     int    first, second, result;
  181.  
  182.     first = 0xffff & Read16BitsHighLow(fp);
  183.     second = 0xffff & Read16BitsHighLow(fp);
  184.  
  185.     result = (first << 16) + second;
  186. #ifdef    CRAY
  187.     if (result & 0x80000000)
  188.         result = result - 0x100000000;
  189. #endif
  190.     return(result);
  191. }
  192.  
  193.  
  194. void
  195. Write32Bits(FILE *fp, int i)
  196. {
  197.     Write16BitsLowHigh(fp,(int)(i&0xffffL));
  198.     Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  199. }
  200.  
  201.  
  202. void
  203. Write32BitsLowHigh(FILE *fp, int i)
  204. {
  205.     Write16BitsLowHigh(fp,(int)(i&0xffffL));
  206.     Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  207. }
  208.  
  209.  
  210. void
  211. Write32BitsHighLow(FILE *fp, int i)
  212. {
  213.     Write16BitsHighLow(fp,(int)((i>>16)&0xffffL));
  214.     Write16BitsHighLow(fp,(int)(i&0xffffL));
  215. }
  216.  
  217. void ReadBytes(FILE    *fp, char *p, int n)
  218. {
  219.     while (!feof(fp) & (n-- > 0))
  220.         *p++ = getc(fp);
  221. }
  222.  
  223. void ReadBytesSwapped(FILE *fp, char *p, int n)
  224. {
  225.     register char    *q = p;
  226.  
  227.     while (!feof(fp) & (n-- > 0))
  228.         *q++ = getc(fp);
  229.  
  230.     for (q--; p < q; p++, q--){
  231.         n = *p;
  232.         *p = *q;
  233.         *q = n;
  234.     }
  235. }
  236.  
  237. void WriteBytes(FILE *fp, char *p, int n)
  238. {
  239.     while (n-- > 0)
  240.         putc(*p++, fp);
  241. }
  242.  
  243. void WriteBytesSwapped(FILE *fp, char *p, int n)
  244. {
  245.     p += n-1;
  246.     while (n-- > 0)
  247.         putc(*p--, fp);
  248. }
  249.  
  250. defdouble
  251. ReadIeeeFloatHighLow(FILE *fp)
  252. {
  253.     char    bits[kFloatLength];
  254.  
  255.     ReadBytes(fp, bits, kFloatLength);
  256.     return ConvertFromIeeeSingle(bits);
  257. }
  258.  
  259. defdouble
  260. ReadIeeeFloatLowHigh(FILE *fp)
  261. {
  262.     char    bits[kFloatLength];
  263.  
  264.     ReadBytesSwapped(fp, bits, kFloatLength);
  265.     return ConvertFromIeeeSingle(bits);
  266. }
  267.  
  268. defdouble
  269. ReadIeeeDoubleHighLow(FILE *fp)
  270. {
  271.     char    bits[kDoubleLength];
  272.  
  273.     ReadBytes(fp, bits, kDoubleLength);
  274.     return ConvertFromIeeeDouble(bits);
  275. }
  276.  
  277. defdouble
  278. ReadIeeeDoubleLowHigh(FILE *fp)
  279. {
  280.     char    bits[kDoubleLength];
  281.  
  282.     ReadBytesSwapped(fp, bits, kDoubleLength);
  283.     return ConvertFromIeeeDouble(bits);
  284. }
  285.  
  286. defdouble
  287. ReadIeeeExtendedHighLow(FILE *fp)
  288. {
  289.     char    bits[kExtendedLength];
  290.  
  291.     ReadBytes(fp, bits, kExtendedLength);
  292.     return ConvertFromIeeeExtended(bits);
  293. }
  294.  
  295. defdouble
  296. ReadIeeeExtendedLowHigh(FILE *fp)
  297. {
  298.     char    bits[kExtendedLength];
  299.  
  300.     ReadBytesSwapped(fp, bits, kExtendedLength);
  301.     return ConvertFromIeeeExtended(bits);
  302. }
  303.  
  304. void
  305. WriteIeeeFloatLowHigh(FILE *fp, defdouble num)
  306. {
  307.     char    bits[kFloatLength];
  308.  
  309.     ConvertToIeeeSingle(num,bits);
  310.     WriteBytesSwapped(fp,bits,kFloatLength);
  311. }
  312.  
  313. void
  314. WriteIeeeFloatHighLow(FILE *fp, defdouble num)
  315. {
  316.     char    bits[kFloatLength];
  317.  
  318.     ConvertToIeeeSingle(num,bits);
  319.     WriteBytes(fp,bits,kFloatLength);
  320. }
  321.  
  322. void
  323. WriteIeeeDoubleLowHigh(FILE *fp, defdouble num)
  324. {
  325.     char    bits[kDoubleLength];
  326.  
  327.     ConvertToIeeeDouble(num,bits);
  328.     WriteBytesSwapped(fp,bits,kDoubleLength);
  329. }
  330.  
  331. void
  332. WriteIeeeDoubleHighLow(FILE *fp, defdouble num)
  333. {
  334.     char    bits[kDoubleLength];
  335.  
  336.     ConvertToIeeeDouble(num,bits);
  337.     WriteBytes(fp,bits,kDoubleLength);
  338. }
  339.  
  340. void
  341. WriteIeeeExtendedLowHigh(FILE *fp, defdouble num)
  342. {
  343.     char    bits[kExtendedLength];
  344.  
  345.     ConvertToIeeeExtended(num,bits);
  346.     WriteBytesSwapped(fp,bits,kExtendedLength);
  347. }
  348.  
  349.  
  350. void
  351. WriteIeeeExtendedHighLow(FILE *fp, defdouble num)
  352. {
  353.     char    bits[kExtendedLength];
  354.  
  355.     ConvertToIeeeExtended(num,bits);
  356.     WriteBytes(fp,bits,kExtendedLength);
  357. }
  358.  
  359.  
  360.