• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "math_entrypoints.h"
18 
19 namespace art {
20 
art_l2d(int64_t l)21 extern "C" double art_l2d(int64_t l) {
22   return static_cast<double>(l);
23 }
24 
art_l2f(int64_t l)25 extern "C" float art_l2f(int64_t l) {
26   return static_cast<float>(l);
27 }
28 
29 /*
30  * Float/double conversion requires clamping to min and max of integer form.  If
31  * target doesn't support this normally, use these.
32  */
art_d2l(double d)33 extern "C" int64_t art_d2l(double d) {
34   static const double kMaxLong = static_cast<double>(static_cast<int64_t>(0x7fffffffffffffffULL));
35   static const double kMinLong = static_cast<double>(static_cast<int64_t>(0x8000000000000000ULL));
36   if (d >= kMaxLong) {
37     return static_cast<int64_t>(0x7fffffffffffffffULL);
38   } else if (d <= kMinLong) {
39     return static_cast<int64_t>(0x8000000000000000ULL);
40   } else if (d != d)  {  // NaN case
41     return 0;
42   } else {
43     return static_cast<int64_t>(d);
44   }
45 }
46 
art_f2l(float f)47 extern "C" int64_t art_f2l(float f) {
48   static const float kMaxLong = static_cast<float>(static_cast<int64_t>(0x7fffffffffffffffULL));
49   static const float kMinLong = static_cast<float>(static_cast<int64_t>(0x8000000000000000ULL));
50   if (f >= kMaxLong) {
51     return static_cast<int64_t>(0x7fffffffffffffffULL);
52   } else if (f <= kMinLong) {
53     return static_cast<int64_t>(0x8000000000000000ULL);
54   } else if (f != f) {  // NaN case
55     return 0;
56   } else {
57     return static_cast<int64_t>(f);
58   }
59 }
60 
art_d2i(double d)61 extern "C" int32_t art_d2i(double d) {
62   static const double kMaxInt = static_cast<double>(static_cast<int32_t>(0x7fffffffUL));
63   static const double kMinInt = static_cast<double>(static_cast<int32_t>(0x80000000UL));
64   if (d >= kMaxInt) {
65     return static_cast<int32_t>(0x7fffffffUL);
66   } else if (d <= kMinInt) {
67     return static_cast<int32_t>(0x80000000UL);
68   } else if (d != d)  {  // NaN case
69     return 0;
70   } else {
71     return static_cast<int32_t>(d);
72   }
73 }
74 
art_f2i(float f)75 extern "C" int32_t art_f2i(float f) {
76   static const float kMaxInt = static_cast<float>(static_cast<int32_t>(0x7fffffffUL));
77   static const float kMinInt = static_cast<float>(static_cast<int32_t>(0x80000000UL));
78   if (f >= kMaxInt) {
79     return static_cast<int32_t>(0x7fffffffUL);
80   } else if (f <= kMinInt) {
81     return static_cast<int32_t>(0x80000000UL);
82   } else if (f != f) {  // NaN case
83     return 0;
84   } else {
85     return static_cast<int32_t>(f);
86   }
87 }
88 
89 }  // namespace art
90