aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/lang/natMath.cc
blob: 8e2f644e7c9c3804a2ef0f618ffb663cf9c05fee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/* Copyright (C) 1998, 1999, 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */
 
/**
 * @author Andrew Haley <aph@cygnus.com>
 * @date Tue Sep 22 1998 */
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
 * "The Java Language Specification", ISBN 0-201-63451-1
 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
 * Status:  Believed complete and correct.
 */
 
#include <config.h>

#include <java/lang/String.h>
#include <java/lang/Float.h>
#include <java/lang/Double.h>
#include <java/lang/Integer.h>
#include <java/lang/Long.h>
#include <java/lang/Math.h>
#include <gcj/array.h>

#include "fdlibm.h"

jdouble java::lang::Math::cos(jdouble x)
{
  return (jdouble)::cos((double)x);
}  

jdouble java::lang::Math::sin(jdouble x)
{
  return (jdouble)::sin((double)x);
}  

jdouble java::lang::Math::tan(jdouble x)
{
  return (jdouble)::tan((double)x);
}  

jdouble java::lang::Math::asin(jdouble x)
{
  return (jdouble)::asin((double)x);
}  

jdouble java::lang::Math::acos(jdouble x)
{
  return (jdouble)::acos((double)x);
}  

jdouble java::lang::Math::atan(jdouble x)
{
  return (jdouble)::atan((double)x);
}  

jdouble java::lang::Math::atan2(jdouble y, jdouble x)
{
  return (jdouble)::atan2((double)y, (double)x);
}  

jdouble java::lang::Math::log(jdouble x)
{
  return (jdouble)::log((double)x);
}  

jdouble java::lang::Math::exp(jdouble x)
{
  return (jdouble)::exp((double)x);
}  

jdouble java::lang::Math::sqrt(jdouble x)
{
  return (jdouble)::sqrt((double)x);
}  

jdouble java::lang::Math::pow(jdouble y, jdouble x)
{
  return (jdouble)::pow((double)y, (double)x);
}  

jdouble java::lang::Math::IEEEremainder(jdouble y, jdouble x)
{
  return (jdouble)::__ieee754_remainder((double)y, (double)x);
}  

jdouble java::lang::Math::abs(jdouble x)
{
  return (jdouble)::fabs((double)x);
}  

jfloat java::lang::Math::abs(jfloat x)
{
  return (jfloat)::fabsf((float)x);
}  

jdouble java::lang::Math::rint(jdouble x)
{
  return (jdouble)::rint((double)x);
}  

jint java::lang::Math::round(jfloat x)
{
  if (x != x)
    return 0;
  if (x <= (jfloat)java::lang::Integer::MIN_VALUE)
    return java::lang::Integer::MIN_VALUE;
  if (x >= (jfloat)java::lang::Integer::MAX_VALUE)
    return java::lang::Integer::MAX_VALUE;

  return (jint)::rintf((float)x);
}  

jlong java::lang::Math::round(jdouble x)
{
  if (x != x)
    return 0;
  if (x <= (jdouble)java::lang::Long::MIN_VALUE)
    return java::lang::Long::MIN_VALUE;
  if (x >= (jdouble)java::lang::Long::MAX_VALUE)
    return java::lang::Long::MAX_VALUE;

  return (jlong)::rint((double)x);
}  

jdouble java::lang::Math::floor(jdouble x)
{
  return (jdouble)::floor((double)x);
}  

jdouble java::lang::Math::ceil(jdouble x)
{
  return (jdouble)::ceil((double)x);
}  

static inline int
floatToIntBits (jfloat value)
{
  union {
    jint l;
    jfloat d;
  } u;
  u.d = value;
  return u.l;
}

static inline bool
isNaN (jint bits)
{
  jint e = bits & 0x7f800000;
  jint f = bits & 0x007fffff;

  return e == 0x7f800000 && f != 0;
}

jfloat
java::lang::Math::min(jfloat a, jfloat b)
{
  jint abits = floatToIntBits (a);
  jint bbits = floatToIntBits (b);
  
  if (isNaN (abits) || isNaN (bbits))
    return java::lang::Float::NaN;
  
  if (abits >= 0) // a is +ve
    return bbits < 0 ? b  // a is +ve, b is -ve.
      // a and b are both +ve, so compare magnitudes: the number with
      // the smallest magnitude is the smallest
      : (abits < bbits ? a : b);
  else // a is -ve
    return bbits >= 0 ? a  // a is -ve, b is +ve.
      // a and b are both -ve, so compare magnitudes: the number with
      // the biggest magnitude is the smallest
      : (abits > bbits ? a : b);
}

jfloat 
java::lang::Math::max(jfloat a, jfloat b)
{
  jint abits = floatToIntBits (a);
  jint bbits = floatToIntBits (b);
  
  if (isNaN (abits) || isNaN (bbits))
    return java::lang::Float::NaN;
  
  if (abits >= 0) // a is +ve
    return bbits < 0 ? a  // a is +ve, b is -ve.
      // a and b are both +ve, so compare magnitudes: the number with
      // the smallest magnitude is the smallest
      : (abits > bbits ? a : b);
  else // a is -ve
    return bbits >= 0 ? b  // a is -ve, b is +ve.
      // a and b are both -ve, so compare magnitudes: the number with
      // the biggest magnitude is the smallest
      : (abits < bbits ? a : b);
}

static inline jlong
doubleToLongBits (jdouble value)
{
  union {
    jlong l;
    jdouble d;
  } u;
  u.d = value;
  return u.l;
}

static inline bool 
isNaN (jlong bits)
{
  jlong e = bits & 0x7ff0000000000000LL;
  jlong f = bits & 0x000fffffffffffffLL;
  
  return e == 0x7ff0000000000000LL && f != 0LL;
}


jdouble
java::lang::Math::min(jdouble a, jdouble b)
{
  jlong abits = doubleToLongBits (a);
  jlong bbits = doubleToLongBits (b);
  
  if (isNaN (abits) || isNaN (bbits))
    return java::lang::Double::NaN;

  if (abits >= 0LL) // a is +ve
    return bbits < 0LL ? b  // a is +ve, b is -ve.
      // a and b are both +ve, so compare magnitudes: the number with
      // the smallest magnitude is the smallest
      : (abits < bbits ? a : b);
  else // a is -ve
    return bbits >= 0LL ? a  // a is -ve, b is +ve.
      // a and b are both -ve, so compare magnitudes: the number with
      // the biggest magnitude is the smallest
      : (abits > bbits ? a : b);
}

jdouble
java::lang::Math::max(jdouble a, jdouble b)
{
  jlong abits = doubleToLongBits (a);
  jlong bbits = doubleToLongBits (b);
  
  if (isNaN (abits) || isNaN (bbits))
    return java::lang::Double::NaN;

  if (abits >= 0LL) // a is +ve
    return bbits < 0LL ? a  // a is +ve, b is -ve.
      // a and b are both +ve, so compare magnitudes: the number with
      // the smallest magnitude is the smallest
      : (abits > bbits ? a : b);
  else // a is -ve
    return bbits >= 0LL ? b  // a is -ve, b is +ve.
      // a and b are both -ve, so compare magnitudes: the number with
      // the biggest magnitude is the smallest
      : (abits < bbits ? a : b);
}