comparison graal/com.oracle.max.cri/src/com/oracle/max/cri/intrinsics/UnsignedMath.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
1 /*
2 * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.max.cri.intrinsics;
24
25 import static com.oracle.max.cri.intrinsics.IntrinsicIDs.*;
26
27 import java.math.*;
28
29 import com.sun.max.annotate.*;
30
31 /**
32 * {@link INTRINSIC} method definitions for unsigned comparisons.
33 * All methods have correct, but slow, standard Java implementations so that
34 * they can be used with compilers not supporting the intrinsics.
35 */
36 public class UnsignedMath {
37 private static final long MASK = 0xffffffffL;
38
39 /**
40 * @see IntrinsicIDs#UCMP_AT
41 */
42 @INTRINSIC(UCMP_AT)
43 public static boolean aboveThan(int a, int b) {
44 return (a & MASK) > (b & MASK);
45 }
46
47 /**
48 * @see IntrinsicIDs#UCMP_AE
49 */
50 @INTRINSIC(UCMP_AE)
51 public static boolean aboveOrEqual(int a, int b) {
52 return (a & MASK) >= (b & MASK);
53 }
54
55 /**
56 * @see IntrinsicIDs#UCMP_BT
57 */
58 @INTRINSIC(UCMP_BT)
59 public static boolean belowThan(int a, int b) {
60 return (a & MASK) < (b & MASK);
61 }
62
63 /**
64 * @see IntrinsicIDs#UCMP_BE
65 */
66 @INTRINSIC(UCMP_BE)
67 public static boolean belowOrEqual(int a, int b) {
68 return (a & MASK) <= (b & MASK);
69 }
70
71
72 /**
73 * @see IntrinsicIDs#UCMP_AT
74 */
75 @INTRINSIC(UCMP_AT)
76 public static boolean aboveThan(long a, long b) {
77 return (a > b) ^ ((a < 0) != (b < 0));
78 }
79
80 /**
81 * @see IntrinsicIDs#UCMP_AE
82 */
83 @INTRINSIC(UCMP_AE)
84 public static boolean aboveOrEqual(long a, long b) {
85 return (a >= b) ^ ((a < 0) != (b < 0));
86 }
87
88 /**
89 * @see IntrinsicIDs#UCMP_BT
90 */
91 @INTRINSIC(UCMP_BT)
92 public static boolean belowThan(long a, long b) {
93 return (a < b) ^ ((a < 0) != (b < 0));
94 }
95
96 /**
97 * @see IntrinsicIDs#UCMP_BE
98 */
99 @INTRINSIC(UCMP_BE)
100 public static boolean belowOrEqual(long a, long b) {
101 return (a <= b) ^ ((a < 0) != (b < 0));
102 }
103
104
105 /**
106 * @see IntrinsicIDs#UDIV
107 */
108 @INTRINSIC(UDIV)
109 public static int divide(int a, int b) {
110 return (int) ((a & MASK) / (b & MASK));
111 }
112
113 /**
114 * @see IntrinsicIDs#UREM
115 */
116 @INTRINSIC(UREM)
117 public static int remainder(int a, int b) {
118 return (int) ((a & MASK) % (b & MASK));
119 }
120
121
122 /**
123 * @see IntrinsicIDs#UDIV
124 */
125 @INTRINSIC(UDIV)
126 public static long divide(long a, long b) {
127 return bi(a).divide(bi(b)).longValue();
128 }
129
130 /**
131 * @see IntrinsicIDs#UREM
132 */
133 @INTRINSIC(UREM)
134 public static long remainder(long a, long b) {
135 return bi(a).remainder(bi(b)).longValue();
136 }
137
138 private static BigInteger bi(long unsigned) {
139 return unsigned >= 0 ? BigInteger.valueOf(unsigned) : BigInteger.valueOf(unsigned & 0x7fffffffffffffffL).setBit(63);
140 }
141 }
142