1 /* Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
3 This file is part of libgcj.
5 This software is copyrighted work licensed under the terms of the
6 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
10 * @author Andrew Haley <aph@cygnus.com>
11 * @date Tue Sep 22 1998 */
12 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
13 * "The Java Language Specification", ISBN 0-201-63451-1
14 * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
15 * Status: Believed complete and correct.
20 #include <java/lang/String.h>
21 #include <java/lang/Float.h>
22 #include <java/lang/Double.h>
23 #include <java/lang/Integer.h>
24 #include <java/lang/Long.h>
25 #include <java/lang/Math.h>
26 #include <gcj/array.h>
30 jdouble
java::lang::Math::cos(jdouble x
)
32 return (jdouble
)::cos((double)x
);
35 jdouble
java::lang::Math::sin(jdouble x
)
37 return (jdouble
)::sin((double)x
);
40 jdouble
java::lang::Math::tan(jdouble x
)
42 return (jdouble
)::tan((double)x
);
45 jdouble
java::lang::Math::asin(jdouble x
)
47 return (jdouble
)::asin((double)x
);
50 jdouble
java::lang::Math::acos(jdouble x
)
52 return (jdouble
)::acos((double)x
);
55 jdouble
java::lang::Math::atan(jdouble x
)
57 return (jdouble
)::atan((double)x
);
60 jdouble
java::lang::Math::atan2(jdouble y
, jdouble x
)
62 return (jdouble
)::atan2((double)y
, (double)x
);
65 jdouble
java::lang::Math::log(jdouble x
)
67 return (jdouble
)::log((double)x
);
70 jdouble
java::lang::Math::exp(jdouble x
)
72 return (jdouble
)::exp((double)x
);
75 jdouble
java::lang::Math::sqrt(jdouble x
)
77 return (jdouble
)::sqrt((double)x
);
80 jdouble
java::lang::Math::pow(jdouble y
, jdouble x
)
82 return (jdouble
)::pow((double)y
, (double)x
);
85 jdouble
java::lang::Math::IEEEremainder(jdouble y
, jdouble x
)
87 return (jdouble
)::__ieee754_remainder((double)y
, (double)x
);
90 jdouble
java::lang::Math::abs(jdouble x
)
92 return (jdouble
)::fabs((double)x
);
95 jfloat
java::lang::Math::abs(jfloat x
)
97 return (jfloat
)::fabsf((float)x
);
100 jdouble
java::lang::Math::rint(jdouble x
)
102 return (jdouble
)::rint((double)x
);
105 jint
java::lang::Math::round(jfloat x
)
109 if (x
<= (jfloat
)java::lang::Integer::MIN_VALUE
)
110 return java::lang::Integer::MIN_VALUE
;
111 if (x
>= (jfloat
)java::lang::Integer::MAX_VALUE
)
112 return java::lang::Integer::MAX_VALUE
;
114 return (jint
)::rintf((float)x
);
117 jlong
java::lang::Math::round(jdouble x
)
121 if (x
<= (jdouble
)java::lang::Long::MIN_VALUE
)
122 return java::lang::Long::MIN_VALUE
;
123 if (x
>= (jdouble
)java::lang::Long::MAX_VALUE
)
124 return java::lang::Long::MAX_VALUE
;
126 return (jlong
)::rint((double)x
);
129 jdouble
java::lang::Math::floor(jdouble x
)
131 return (jdouble
)::floor((double)x
);
134 jdouble
java::lang::Math::ceil(jdouble x
)
136 return (jdouble
)::ceil((double)x
);
140 floatToIntBits (jfloat value
)
153 jint e
= bits
& 0x7f800000;
154 jint f
= bits
& 0x007fffff;
156 return e
== 0x7f800000 && f
!= 0;
160 java::lang::Math::min(jfloat a
, jfloat b
)
162 jint abits
= floatToIntBits (a
);
163 jint bbits
= floatToIntBits (b
);
165 if (isNaN (abits
) || isNaN (bbits
))
166 return java::lang::Float::NaN
;
168 if (abits
>= 0) // a is +ve
169 return bbits
< 0 ? b
// a is +ve, b is -ve.
170 // a and b are both +ve, so compare magnitudes: the number with
171 // the smallest magnitude is the smallest
172 : (abits
< bbits
? a
: b
);
174 return bbits
>= 0 ? a
// a is -ve, b is +ve.
175 // a and b are both -ve, so compare magnitudes: the number with
176 // the biggest magnitude is the smallest
177 : (abits
> bbits
? a
: b
);
181 java::lang::Math::max(jfloat a
, jfloat b
)
183 jint abits
= floatToIntBits (a
);
184 jint bbits
= floatToIntBits (b
);
186 if (isNaN (abits
) || isNaN (bbits
))
187 return java::lang::Float::NaN
;
189 if (abits
>= 0) // a is +ve
190 return bbits
< 0 ? a
// a is +ve, b is -ve.
191 // a and b are both +ve, so compare magnitudes: the number with
192 // the smallest magnitude is the smallest
193 : (abits
> bbits
? a
: b
);
195 return bbits
>= 0 ? b
// a is -ve, b is +ve.
196 // a and b are both -ve, so compare magnitudes: the number with
197 // the biggest magnitude is the smallest
198 : (abits
< bbits
? a
: b
);
202 doubleToLongBits (jdouble value
)
215 jlong e
= bits
& 0x7ff0000000000000LL
;
216 jlong f
= bits
& 0x000fffffffffffffLL
;
218 return e
== 0x7ff0000000000000LL
&& f
!= 0LL;
223 java::lang::Math::min(jdouble a
, jdouble b
)
225 jlong abits
= doubleToLongBits (a
);
226 jlong bbits
= doubleToLongBits (b
);
228 if (isNaN (abits
) || isNaN (bbits
))
229 return java::lang::Double::NaN
;
231 if (abits
>= 0LL) // a is +ve
232 return bbits
< 0LL ? b
// a is +ve, b is -ve.
233 // a and b are both +ve, so compare magnitudes: the number with
234 // the smallest magnitude is the smallest
235 : (abits
< bbits
? a
: b
);
237 return bbits
>= 0LL ? a
// a is -ve, b is +ve.
238 // a and b are both -ve, so compare magnitudes: the number with
239 // the biggest magnitude is the smallest
240 : (abits
> bbits
? a
: b
);
244 java::lang::Math::max(jdouble a
, jdouble b
)
246 jlong abits
= doubleToLongBits (a
);
247 jlong bbits
= doubleToLongBits (b
);
249 if (isNaN (abits
) || isNaN (bbits
))
250 return java::lang::Double::NaN
;
252 if (abits
>= 0LL) // a is +ve
253 return bbits
< 0LL ? a
// a is +ve, b is -ve.
254 // a and b are both +ve, so compare magnitudes: the number with
255 // the smallest magnitude is the smallest
256 : (abits
> bbits
? a
: b
);
258 return bbits
>= 0LL ? b
// a is -ve, b is +ve.
259 // a and b are both -ve, so compare magnitudes: the number with
260 // the biggest magnitude is the smallest
261 : (abits
< bbits
? a
: b
);