|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.lang; |
| 22 | + |
| 23 | +import com.sun.max.annotate.*; |
| 24 | +import com.sun.max.unsafe.*; |
| 25 | + |
| 26 | +/** |
| 27 | + * A collection of static methods for doing unsigned arithmetic on Java primitive types |
| 28 | + * where the semantics of the arithmetics operations differ from the signed version. |
| 29 | + * In addition to providing unsigned arithmetic semantics for the programmer, |
| 30 | + * these methods also expose different optimization possibilities to the compiler as |
| 31 | + * well as allowing for them to be implemented as compiler builtins. |
| 32 | + * |
| 33 | + * @author Doug Simon |
| 34 | + */ |
| 35 | +public class Unsigned { |
| 36 | + |
| 37 | + /** |
| 38 | + * Performs unsigned integer division. |
| 39 | + */ |
| 40 | + @INLINE |
| 41 | + @UNSAFE |
| 42 | + public static int idiv(int dividend, int divisor) { |
| 43 | + return Address.fromUnsignedInt(dividend).dividedBy(divisor).toInt(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Performs unsigned long division. |
| 48 | + */ |
| 49 | + @INLINE |
| 50 | + @UNSAFE |
| 51 | + public static long ldiv(long dividend, long divisor) { |
| 52 | + return Address.fromLong(dividend).dividedBy(Address.fromLong(divisor)).toLong(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Performs unsigned integer modulus. |
| 57 | + */ |
| 58 | + @INLINE |
| 59 | + @UNSAFE |
| 60 | + public static int irem(int dividend, int divisor) { |
| 61 | + return Address.fromUnsignedInt(dividend).remainder(divisor); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Performs unsigned long modulus. |
| 66 | + */ |
| 67 | + @INLINE |
| 68 | + @UNSAFE |
| 69 | + public static long lrem(long dividend, long divisor) { |
| 70 | + return Address.fromLong(dividend).remainder(Address.fromLong(divisor)).toLong(); |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments