home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 4 / AACD04.ISO / AACD / Sound / LAME / Source / portableio.c < prev    next >
C/C++ Source or Header  |  1999-05-31  |  7KB  |  397 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 2.6 1991/04/30 17:06:02 malcolm Exp $
  47.  *
  48.  * $Log: portableio.c,v $
  49.  * Revision 2.6  91/04/30  17:06:02  malcolm
  50.  */
  51.  
  52. #include    <stdio.h>
  53. #include    <math.h>
  54. #include    "portableio.h"
  55.  
  56. /****************************************************************
  57.  * Big/little-endian independent I/O routines.
  58.  ****************************************************************/
  59.  
  60.  
  61. int
  62. ReadByte(fp)
  63. FILE *fp;
  64. {
  65.     int    result;
  66.  
  67.     result = getc(fp) & 0xff;
  68.     if (result & 0x80)
  69.         result = result - 0x100;
  70.     return result;
  71. }
  72.  
  73.  
  74. int
  75. Read16BitsLowHigh(fp)
  76. FILE *fp;
  77. {
  78.     int    first, second, result;
  79.  
  80.     first = 0xff & getc(fp);
  81.     second = 0xff & getc(fp);
  82.  
  83.     result = (second << 8) + first;
  84. #ifndef    THINK_C42
  85.     if (result & 0x8000)
  86.         result = result - 0x10000;
  87. #endif    /* THINK_C */
  88.     return(result);
  89. }
  90.  
  91.  
  92. int
  93. Read16BitsHighLow(fp)
  94. FILE *fp;
  95. {
  96.     int    first, second, result;
  97.  
  98.     first = 0xff & getc(fp);
  99.     second = 0xff & getc(fp);
  100.  
  101.     result = (first << 8) + second;
  102. #ifndef    THINK_C42
  103.     if (result & 0x8000)
  104.         result = result - 0x10000;
  105. #endif    /* THINK_C */
  106.     return(result);
  107. }
  108.  
  109.  
  110. void
  111. Write8Bits(fp, i)
  112. FILE *fp;
  113. int i;
  114. {
  115.     putc(i&0xff,fp);
  116. }
  117.  
  118.  
  119. void
  120. Write16BitsLowHigh(fp, i)
  121. FILE *fp;
  122. int i;
  123. {
  124.     putc(i&0xff,fp);
  125.     putc((i>>8)&0xff,fp);
  126. }
  127.  
  128.  
  129. void
  130. Write16BitsHighLow(fp, i)
  131. FILE *fp;
  132. int i;
  133. {
  134.     putc((i>>8)&0xff,fp);
  135.     putc(i&0xff,fp);
  136. }
  137.  
  138.  
  139. int
  140. Read24BitsHighLow(fp)
  141. FILE *fp;
  142. {
  143.     int    first, second, third;
  144.     int    result;
  145.  
  146.     first = 0xff & getc(fp);
  147.     second = 0xff & getc(fp);
  148.     third = 0xff & getc(fp);
  149.  
  150.     result = (first << 16) + (second << 8) + third;
  151.     if (result & 0x800000)
  152.         result = result - 0x1000000;
  153.     return(result);
  154. }
  155.  
  156. #define    Read32BitsLowHigh(f)    Read32Bits(f)
  157.  
  158.  
  159. int
  160. Read32Bits(fp)
  161. 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(fp)
  179. FILE *fp;
  180. {
  181.     int    first, second, result;
  182.  
  183.     first = 0xffff & Read16BitsHighLow(fp);
  184.     second = 0xffff & Read16BitsHighLow(fp);
  185.  
  186.     result = (first << 16) + second;
  187. #ifdef    CRAY
  188.     if (result & 0x80000000)
  189.         result = result - 0x100000000;
  190. #endif
  191.     return(result);
  192. }
  193.  
  194.  
  195. void
  196. Write32Bits(fp, i)
  197. FILE *fp;
  198. int i;
  199. {
  200.     Write16BitsLowHigh(fp,(int)(i&0xffffL));
  201.     Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  202. }
  203.  
  204.  
  205. void
  206. Write32BitsLowHigh(fp, i)
  207. FILE *fp;
  208. int i;
  209. {
  210.     Write16BitsLowHigh(fp,(int)(i&0xffffL));
  211.     Write16BitsLowHigh(fp,(int)((i>>16)&0xffffL));
  212. }
  213.  
  214.  
  215. void
  216. Write32BitsHighLow(fp, i)
  217. FILE *fp;
  218. int i;
  219. {
  220.     Write16BitsHighLow(fp,(int)((i>>16)&0xffffL));
  221.     Write16BitsHighLow(fp,(int)(i&0xffffL));
  222. }
  223.  
  224. void ReadBytes(fp, p, n)
  225. FILE    *fp;
  226. char    *p;
  227. int    n;
  228. {
  229.     while (!feof(fp) & (n-- > 0))
  230.         *p++ = getc(fp);
  231. }
  232.  
  233. void ReadBytesSwapped(fp, p, n)
  234. FILE    *fp;
  235. char    *p;
  236. int    n;
  237. {
  238.     register char    *q = p;
  239.  
  240.     while (!feof(fp) & (n-- > 0))
  241.         *q++ = getc(fp);
  242.  
  243.     for (q--; p < q; p++, q--){
  244.         n = *p;
  245.         *p = *q;
  246.         *q = n;
  247.     }
  248. }
  249.  
  250. void WriteBytes(fp, p, n)
  251. FILE    *fp;
  252. char    *p;
  253. int    n;
  254. {
  255.     while (n-- > 0)
  256.         putc(*p++, fp);
  257. }
  258.  
  259. void WriteBytesSwapped(fp, p, n)
  260. FILE    *fp;
  261. char    *p;
  262. int    n;
  263. {
  264.     p += n-1;
  265.     while (n-- > 0)
  266.         putc(*p--, fp);
  267. }
  268.  
  269. defdouble
  270. ReadIeeeFloatHighLow(fp)
  271. FILE *fp;
  272. {
  273.     char    bits[kFloatLength];
  274.  
  275.     ReadBytes(fp, bits, kFloatLength);
  276.     return ConvertFromIeeeSingle(bits);
  277. }
  278.  
  279. defdouble
  280. ReadIeeeFloatLowHigh(fp)
  281. FILE *fp;
  282. {
  283.     char    bits[kFloatLength];
  284.  
  285.     ReadBytesSwapped(fp, bits, kFloatLength);
  286.     return ConvertFromIeeeSingle(bits);
  287. }
  288.  
  289. defdouble
  290. ReadIeeeDoubleHighLow(fp)
  291. FILE *fp;
  292. {
  293.     char    bits[kDoubleLength];
  294.  
  295.     ReadBytes(fp, bits, kDoubleLength);
  296.     return ConvertFromIeeeDouble(bits);
  297. }
  298.  
  299. defdouble
  300. ReadIeeeDoubleLowHigh(fp)
  301. FILE *fp;
  302. {
  303.     char    bits[kDoubleLength];
  304.  
  305.     ReadBytesSwapped(fp, bits, kDoubleLength);
  306.     return ConvertFromIeeeDouble(bits);
  307. }
  308.  
  309. defdouble
  310. ReadIeeeExtendedHighLow(fp)
  311. FILE *fp;
  312. {
  313.     char    bits[kExtendedLength];
  314.  
  315.     ReadBytes(fp, bits, kExtendedLength);
  316.     return ConvertFromIeeeExtended(bits);
  317. }
  318.  
  319. defdouble
  320. ReadIeeeExtendedLowHigh(fp)
  321. FILE *fp;
  322. {
  323.     char    bits[kExtendedLength];
  324.  
  325.     ReadBytesSwapped(fp, bits, kExtendedLength);
  326.     return ConvertFromIeeeExtended(bits);
  327. }
  328.  
  329. void
  330. WriteIeeeFloatLowHigh(fp, num)
  331. FILE *fp;
  332. defdouble num;
  333. {
  334.     char    bits[kFloatLength];
  335.  
  336.     ConvertToIeeeSingle(num,bits);
  337.     WriteBytesSwapped(fp,bits,kFloatLength);
  338. }
  339.  
  340. void
  341. WriteIeeeFloatHighLow(fp, num)
  342. FILE *fp;
  343. defdouble num;
  344. {
  345.     char    bits[kFloatLength];
  346.  
  347.     ConvertToIeeeSingle(num,bits);
  348.     WriteBytes(fp,bits,kFloatLength);
  349. }
  350.  
  351. void
  352. WriteIeeeDoubleLowHigh(fp, num)
  353. FILE *fp;
  354. defdouble num;
  355. {
  356.     char    bits[kDoubleLength];
  357.  
  358.     ConvertToIeeeDouble(num,bits);
  359.     WriteBytesSwapped(fp,bits,kDoubleLength);
  360. }
  361.  
  362. void
  363. WriteIeeeDoubleHighLow(fp, num)
  364. FILE *fp;
  365. defdouble num;
  366. {
  367.     char    bits[kDoubleLength];
  368.  
  369.     ConvertToIeeeDouble(num,bits);
  370.     WriteBytes(fp,bits,kDoubleLength);
  371. }
  372.  
  373. void
  374. WriteIeeeExtendedLowHigh(fp, num)
  375. FILE *fp;
  376. defdouble num;
  377. {
  378.     char    bits[kExtendedLength];
  379.  
  380.     ConvertToIeeeExtended(num,bits);
  381.     WriteBytesSwapped(fp,bits,kExtendedLength);
  382. }
  383.  
  384.  
  385. void
  386. WriteIeeeExtendedHighLow(fp, num)
  387. FILE *fp;
  388. defdouble num;
  389. {
  390.     char    bits[kExtendedLength];
  391.  
  392.     ConvertToIeeeExtended(num,bits);
  393.     WriteBytes(fp,bits,kExtendedLength);
  394. }
  395.  
  396.  
  397.