Merge branch 'ct' of git.pipapo.org:cinelerra-ct into ct
[cinelerra_cv/ct.git] / toolame-02l / ieeefloat.c
blob1658b20dc8eec702c7a3e7c9f39dc6fb6e00a7c1
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/10/04 00:11:19 herman Exp $
46 * $Log: ieeefloat.c,v $
47 * Revision 1.1 2003/10/04 00:11:19 herman
48 * New file from 1.1.7 (not in 1.1.6) added from Rich's automakified src tree.
50 * Revision 1.1 2003/07/29 04:17:52 heroine
51 * *** empty log message ***
53 * Revision 1.1 1993/06/11 17:45:46 malcolm
54 * Initial revision
58 #include <stdio.h>
59 #include <math.h>
60 #include "ieeefloat.h"
61 #include "common.h"
63 # define FloatToUnsigned(f) ((unsigned long)(((long)((f) - 2147483648.0)) + 2147483647L + 1))
64 # define UnsignedToFloat(u) (((double)((long)((u) - 2147483647L - 1))) + 2147483648.0)
66 double ConvertFromIeeeExtended (bytes)
67 char *bytes;
69 double f;
70 long expon;
71 unsigned long hiMant, loMant;
73 expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
74 hiMant = ((unsigned long) (bytes[2] & 0xFF) << 24)
75 | ((unsigned long) (bytes[3] & 0xFF) << 16) |
76 ((unsigned long) (bytes[4] & 0xFF) << 8) |
77 ((unsigned long) (bytes[5] & 0xFF));
78 loMant = ((unsigned long) (bytes[6] & 0xFF) << 24)
79 | ((unsigned long) (bytes[7] & 0xFF) << 16) |
80 ((unsigned long) (bytes[8] & 0xFF) << 8) |
81 ((unsigned long) (bytes[9] & 0xFF));
83 if (expon == 0 && hiMant == 0 && loMant == 0) {
84 f = 0;
85 } else {
86 if (expon == 0x7FFF) { /* Infinity or NaN */
87 f = HUGE_VAL;
88 } else {
89 expon -= 16383;
90 f = ldexp (UnsignedToFloat (hiMant), expon -= 31);
91 f += ldexp (UnsignedToFloat (loMant), expon -= 32);
95 if (bytes[0] & 0x80)
96 return -f;
97 else
98 return f;