Subversion Repositories tendra.SVN

Rev

Rev 6 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 7u83 1
/*
6 7u83 2
 * Copyright (c) 2002-2005 The TenDRA Project <http://www.tendra.org/>.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of The TenDRA Project nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific, prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
18
 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
 * EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * $Id$
30
 */
31
/*
2 7u83 32
    		 Crown Copyright (c) 1997
33
 
34
    This TenDRA(r) Computer Program is subject to Copyright
35
    owned by the United Kingdom Secretary of State for Defence
36
    acting through the Defence Evaluation and Research Agency
37
    (DERA).  It is made available to Recipients with a
38
    royalty-free licence for its use, reproduction, transfer
39
    to other parties and amendment for any purpose not excluding
40
    product development provided that any such use et cetera
41
    shall be deemed to be acceptance of the following conditions:-
42
 
43
        (1) Its Recipients shall ensure that this Notice is
44
        reproduced upon any copies or amended versions of it;
45
 
46
        (2) Any amended version of it shall be clearly marked to
47
        show both the nature of and the organisation responsible
48
        for the relevant amendment or amendments;
49
 
50
        (3) Its onward transfer from a recipient to another
51
        party shall be deemed to be that party's acceptance of
52
        these conditions;
53
 
54
        (4) DERA gives no warranty or assurance as to its
55
        quality or suitability for any purpose and DERA accepts
56
        no liability whatsoever in relation to any use to which
57
        it may be put.
58
*/
59
 
60
 
61
/**********************************************************************
62
$Author: release $
63
$Date: 1998/01/17 15:55:47 $
64
$Revision: 1.1.1.1 $
65
$Log: f64.c,v $
66
 * Revision 1.1.1.1  1998/01/17  15:55:47  release
67
 * First version to be checked into rolling release.
68
 *
69
 * Revision 1.1  1995/04/06  10:44:05  currie
70
 * Initial revision
71
 *
72
***********************************************************************/
73
 
74
 
75
 
76
#include "config.h"
77
#include "common_types.h"
78
#include "flpttypes.h"
79
#include "messages_c.h"
80
#include "flpt.h"
81
#include "basicread.h"
82
#include "expmacs.h"
83
#include "exp.h"
84
 
85
#include "f64.h"
86
 
87
 
88
 
89
/* PROCEDURES */
90
 
6 7u83 91
flt64
92
flt_to_f64(flpt fp, int sg, int *ov)
2 7u83 93
{
6 7u83 94
  flt *f = &flptnos[fp];
2 7u83 95
  flt64 res;
96
  int i = f->exp;
97
  *ov = 0;
98
 
6 7u83 99
  if (f->sign == 0 || i < 0) {
2 7u83 100
    res.big = 0;
101
    res.small = 0;
6 7u83 102
  } else {
2 7u83 103
    res.big = 0;
104
    res.small = f->mant[i];
6 7u83 105
    if (i > 0) {
106
      res.small += (unsigned int)(f->mant[i - 1] << 16);
107
    }
108
    if (i > 1) {
109
      res.big = (int)f->mant[i - 2];
110
    }
111
    if (i > 2) {
112
      res.big += (int)(f->mant[i - 3] << 16);
113
    }
114
    if (i > 3 || (sg && res.big < 0)) {
2 7u83 115
      *ov = 1;
6 7u83 116
    }
117
  }
2 7u83 118
 
6 7u83 119
  if (f->sign == -1) {
2 7u83 120
    res.small = ~res.small;
121
    res.big = ~res.big;
122
    if (res.small == 0xffffffff) {
123
      ++res.big;
6 7u83 124
    }
2 7u83 125
    ++res.small;
6 7u83 126
  }
2 7u83 127
 
128
  return res;
129
}
130
 
6 7u83 131
 
132
flpt
133
f64_to_flt(flt64 a, int sg)
2 7u83 134
{
135
  flpt r = new_flpt();
6 7u83 136
  flt *res = &flptnos[r];
2 7u83 137
  flt_zero(res);
138
 
6 7u83 139
  if (a.big == 0 && a.small == 0) {
2 7u83 140
    return r;
6 7u83 141
  }
2 7u83 142
 
143
  if (sg && a.big < 0) {
144
    a.small = ~a.small;
145
    a.big = ~a.big;
146
    if (a.small == 0xffffffff) {
147
      ++a.big;
6 7u83 148
    }
2 7u83 149
    ++a.small;
150
    res->sign = -1;
6 7u83 151
  } else {
152
    res->sign = 1;
2 7u83 153
  }
154
 
155
  if (a.big == 0) {
156
    if ((a.small & 0xffff0000) == 0) {
157
      res->exp = 0;
158
      res->mant[0] = (unsigned short)(a.small & 0xffff);
6 7u83 159
    } else {
2 7u83 160
      res->exp = 1;
161
      res->mant[0] = (unsigned short)((a.small & 0xffff0000) >> 16);
162
      res->mant[1] = (unsigned short)(a.small & 0xffff);
6 7u83 163
    }
164
  } else {
2 7u83 165
    if ((a.big & (int)0xffff0000) == 0) {
166
      res->exp = 2;
167
      res->mant[0] = (unsigned short)(a.big & 0xffff);
168
      res->mant[1] = (unsigned short)((a.small & 0xffff0000) >> 16);
169
      res->mant[2] = (unsigned short)(a.small & 0xffff);
6 7u83 170
    } else {
2 7u83 171
      res->exp = 3;
172
      res->mant[0] = (unsigned short)(((unsigned int)a.big >> 16) & 0xffff);
173
      res->mant[1] = (unsigned short)(a.big & 0xffff);
174
      res->mant[2] = (unsigned short)((a.small & 0xffff0000) >> 16);
175
      res->mant[3] = (unsigned short)(a.small & 0xffff);
6 7u83 176
    }
177
  }
2 7u83 178
 
179
  return r;
180
}
181
 
6 7u83 182
 
183
int
184
f64_to_flpt(flt64 a, int sg, int *pr, int sz)
2 7u83 185
{
186
  int t = (int)a.small;
187
  *pr = 0;
188
 
6 7u83 189
  if (sg && (t >> 31) == a.big) {
2 7u83 190
    return t;
6 7u83 191
  }
2 7u83 192
 
6 7u83 193
  if (!sg && a.big == 0 && ((a.small & 0x80000000) == 0 || sz <= 32)) {
2 7u83 194
    return t;
6 7u83 195
  }
2 7u83 196
 
197
  *pr = 1;
6 7u83 198
  return f64_to_flt(a, sg);
2 7u83 199
}
200
 
201
 
6 7u83 202
flt64
203
int_to_f64(int i, int sg)
2 7u83 204
{
205
  flt64 res;
206
  res.small = (unsigned int)i;
6 7u83 207
  if (sg && i < 0) {
2 7u83 208
    res.big = -1;
6 7u83 209
  } else {
2 7u83 210
    res.big = 0;
6 7u83 211
  }
2 7u83 212
  return res;
213
}
214
 
6 7u83 215
 
216
flt64
217
exp_to_f64(exp e)
2 7u83 218
{
219
  int ov;
6 7u83 220
  if (isbigval(e)) {
2 7u83 221
    return flt_to_f64(no(e), is_signed(sh(e)), &ov);
6 7u83 222
  }
2 7u83 223
  return int_to_f64(no(e), is_signed(sh(e)));
224
}