Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2012 Google Inc. All Rights Reserved. |
| 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 <stdint.h> |
| 18 | |
| 19 | namespace art { |
| 20 | |
| 21 | int CmplFloat(float a, float b) { |
| 22 | if (a == b) { |
| 23 | return 0; |
| 24 | } else if (a < b) { |
| 25 | return -1; |
| 26 | } else if (a > b) { |
| 27 | return 1; |
| 28 | } |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | int CmpgFloat(float a, float b) { |
| 33 | if (a == b) { |
| 34 | return 0; |
| 35 | } else if (a < b) { |
| 36 | return -1; |
| 37 | } else if (a > b) { |
| 38 | return 1; |
| 39 | } |
| 40 | return 1; |
| 41 | } |
| 42 | |
| 43 | int CmpgDouble(double a, double b) { |
| 44 | if (a == b) { |
| 45 | return 0; |
| 46 | } else if (a < b) { |
| 47 | return -1; |
| 48 | } else if (a > b) { |
| 49 | return 1; |
| 50 | } |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | int CmplDouble(double a, double b) { |
| 55 | if (a == b) { |
| 56 | return 0; |
| 57 | } else if (a < b) { |
| 58 | return -1; |
| 59 | } else if (a > b) { |
| 60 | return 1; |
| 61 | } |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Float/double conversion requires clamping to min and max of integer form. If |
| 67 | * target doesn't support this normally, use these. |
| 68 | */ |
| 69 | int64_t D2L(double d) { |
| 70 | static const double kMaxLong = (double) (int64_t) 0x7fffffffffffffffULL; |
| 71 | static const double kMinLong = (double) (int64_t) 0x8000000000000000ULL; |
| 72 | if (d >= kMaxLong) { |
| 73 | return (int64_t) 0x7fffffffffffffffULL; |
| 74 | } else if (d <= kMinLong) { |
| 75 | return (int64_t) 0x8000000000000000ULL; |
| 76 | } else if (d != d) { // NaN case |
| 77 | return 0; |
| 78 | } else { |
| 79 | return (int64_t) d; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | int64_t F2L(float f) { |
| 84 | static const float kMaxLong = (float) (int64_t) 0x7fffffffffffffffULL; |
| 85 | static const float kMinLong = (float) (int64_t) 0x8000000000000000ULL; |
| 86 | if (f >= kMaxLong) { |
| 87 | return (int64_t) 0x7fffffffffffffffULL; |
| 88 | } else if (f <= kMinLong) { |
| 89 | return (int64_t) 0x8000000000000000ULL; |
| 90 | } else if (f != f) { // NaN case |
| 91 | return 0; |
| 92 | } else { |
| 93 | return (int64_t) f; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | } // namespace art |