r123: Merged HEAD and TEST. New stuff shall be committed to HEAD from now on.
[cinelerra_cv/mob.git] / plugins / toolame / ieeefloat.c
blobcebb117f78c2ebad77bfa1b3eefff28ba6eebc21
1 /* Copyright (C) 1988-1991 Apple Computer, Inc.
2 * All Rights Reserved.
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.
12 * This code may be used and freely distributed as long as it includes
13 * this copyright notice and the warranty information.
15 * Machine-independent I/O routines for IEEE floating-point numbers.
17 * NaN's and infinities are converted to HUGE_VAL or HUGE, which
18 * happens to be infinity on IEEE machines. Unfortunately, it is
19 * impossible to preserve NaN's in a machine-independent way.
20 * Infinities are, however, preserved on IEEE machines.
22 * These routines have been tested on the following machines:
23 * Apple Macintosh, MPW 3.1 C compiler
24 * Apple Macintosh, THINK C compiler
25 * Silicon Graphics IRIS, MIPS compiler
26 * Cray X/MP and Y/MP
27 * Digital Equipment VAX
28 * Sequent Balance (Multiprocesor 386)
29 * NeXT
32 * Implemented by Malcolm Slaney and Ken Turkowski.
34 * Malcolm Slaney contributions during 1988-1990 include big- and little-
35 * endian file I/O, conversion to and from Motorola's extended 80-bit
36 * floating-point format, and conversions to and from IEEE single-
37 * precision floating-point format.
39 * In 1991, Ken Turkowski implemented the conversions to and from
40 * IEEE double-precision format, added more precision to the extended
41 * conversions, and accommodated conversions involving +/- infinity,
42 * NaN's, and denormalized numbers.
44 * $Id: ieeefloat.c,v 1.1 2003/06/16 20:00:50 herman Exp $
46 * $Log: ieeefloat.c,v $
47 * Revision 1.1 2003/06/16 20:00:50 herman
48 * Initial revision
50 * Revision 1.1.1.1 2002/06/21 12:35:26 myrina
53 * Revision 1.1.1.1 2001/10/01 03:14:55 heroine
56 * Revision 1.1.1.1 2001/10/01 02:51:42 root
59 * Revision 1.3 2001/07/15 12:16:31 mikecheng
60 * Removed a whooooole heap of unused functions
62 * Revision 1.2 2001/07/04 09:51:36 uid43892
63 * everything passed through 'indent *.c *.h'
65 * Revision 1.1.1.1 2001/07/01 06:54:10 mikecheng
66 * This is v0.30 of toolame. A fresh start consisting of the dist10 code with
67 * all layerI and layerIII removed.
69 * Revision 1.1 1993/06/11 17:45:46 malcolm
70 * Initial revision
74 #include <stdio.h>
75 #include <math.h>
76 #include "ieeefloat.h"
79 /****************************************************************
80 * The following two routines make up for deficiencies in many
81 * compilers to convert properly between unsigned integers and
82 * floating-point. Some compilers which have this bug are the
83 * THINK_C compiler for the Macintosh and the C compiler for the
84 * Silicon Graphics MIPS-based Iris.
85 ****************************************************************/
87 #ifdef applec /* The Apple C compiler works */
88 # define FloatToUnsigned(f) ((unsigned long)(f))
89 # define UnsignedToFloat(u) ((defdouble)(u))
90 #else /* applec */
91 # define FloatToUnsigned(f) ((unsigned long)(((long)((f) - 2147483648.0)) + 2147483647L + 1))
92 # define UnsignedToFloat(u) (((defdouble)((long)((u) - 2147483647L - 1))) + 2147483648.0)
93 #endif /* applec */
96 /****************************************************************
97 * Single precision IEEE floating-point conversion routines
98 ****************************************************************/
100 #define SEXP_MAX 255
101 #define SEXP_OFFSET 127
102 #define SEXP_SIZE 8
103 #define SEXP_POSITION (32-SEXP_SIZE-1)
105 /****************************************************************
106 * Extended precision IEEE floating-point conversion routines
107 ****************************************************************/
109 defdouble
110 ConvertFromIeeeExtended (bytes)
111 char *bytes;
113 defdouble f;
114 long expon;
115 unsigned long hiMant, loMant;
117 #ifdef TEST
118 printf ("ConvertFromIEEEExtended(%lx,%lx,%lx,%lx,%lx,%lx,%lx,%lx,%lx,%lx\r",
119 (long) bytes[0], (long) bytes[1], (long) bytes[2], (long) bytes[3],
120 (long) bytes[4], (long) bytes[5], (long) bytes[6],
121 (long) bytes[7], (long) bytes[8], (long) bytes[9]);
122 #endif
124 expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
125 hiMant = ((unsigned long) (bytes[2] & 0xFF) << 24)
126 | ((unsigned long) (bytes[3] & 0xFF) << 16)
127 | ((unsigned long) (bytes[4] & 0xFF) << 8)
128 | ((unsigned long) (bytes[5] & 0xFF));
129 loMant = ((unsigned long) (bytes[6] & 0xFF) << 24)
130 | ((unsigned long) (bytes[7] & 0xFF) << 16)
131 | ((unsigned long) (bytes[8] & 0xFF) << 8)
132 | ((unsigned long) (bytes[9] & 0xFF));
134 if (expon == 0 && hiMant == 0 && loMant == 0)
136 f = 0;
138 else
140 if (expon == 0x7FFF)
141 { /* Infinity or NaN */
142 f = HUGE_VAL;
144 else
146 expon -= 16383;
147 f = ldexp (UnsignedToFloat (hiMant), expon -= 31);
148 f += ldexp (UnsignedToFloat (loMant), expon -= 32);
152 if (bytes[0] & 0x80)
153 return -f;
154 else
155 return f;