1093 // The goal of this code to avoid undefined or implementation-defined
1094 // behavior. The use of an lvalue to reference cast is explicitly
1095 // permitted by Lvalues and rvalues [basic.lval]. [Section 3.10 Para
1096 // 15 in C++03]
1097 #define JAVA_INTEGER_OP(OP, NAME, TYPE, UNSIGNED_TYPE) \
1098 inline TYPE NAME (TYPE in1, TYPE in2) { \
1099 UNSIGNED_TYPE ures = static_cast<UNSIGNED_TYPE>(in1); \
1100 ures OP ## = static_cast<UNSIGNED_TYPE>(in2); \
1101 return reinterpret_cast<TYPE&>(ures); \
1102 }
1103
1104 JAVA_INTEGER_OP(+, java_add, jint, juint)
1105 JAVA_INTEGER_OP(-, java_subtract, jint, juint)
1106 JAVA_INTEGER_OP(*, java_multiply, jint, juint)
1107 JAVA_INTEGER_OP(+, java_add, jlong, julong)
1108 JAVA_INTEGER_OP(-, java_subtract, jlong, julong)
1109 JAVA_INTEGER_OP(*, java_multiply, jlong, julong)
1110
1111 #undef JAVA_INTEGER_OP
1112
1113 //----------------------------------------------------------------------------------------------------
1114 // The goal of this code is to provide saturating operations for int/uint.
1115 // Checks overflow conditions and saturates the result to min_jint/max_jint.
1116 #define SATURATED_INTEGER_OP(OP, NAME, TYPE1, TYPE2) \
1117 inline int NAME (TYPE1 in1, TYPE2 in2) { \
1118 jlong res = static_cast<jlong>(in1); \
1119 res OP ## = static_cast<jlong>(in2); \
1120 if (res > max_jint) { \
1121 res = max_jint; \
1122 } else if (res < min_jint) { \
1123 res = min_jint; \
1124 } \
1125 return static_cast<int>(res); \
1126 }
1127
1128 SATURATED_INTEGER_OP(+, saturated_add, int, int)
1129 SATURATED_INTEGER_OP(+, saturated_add, int, uint)
1130 SATURATED_INTEGER_OP(+, saturated_add, uint, int)
1131 SATURATED_INTEGER_OP(+, saturated_add, uint, uint)
1132
|
1093 // The goal of this code to avoid undefined or implementation-defined
1094 // behavior. The use of an lvalue to reference cast is explicitly
1095 // permitted by Lvalues and rvalues [basic.lval]. [Section 3.10 Para
1096 // 15 in C++03]
1097 #define JAVA_INTEGER_OP(OP, NAME, TYPE, UNSIGNED_TYPE) \
1098 inline TYPE NAME (TYPE in1, TYPE in2) { \
1099 UNSIGNED_TYPE ures = static_cast<UNSIGNED_TYPE>(in1); \
1100 ures OP ## = static_cast<UNSIGNED_TYPE>(in2); \
1101 return reinterpret_cast<TYPE&>(ures); \
1102 }
1103
1104 JAVA_INTEGER_OP(+, java_add, jint, juint)
1105 JAVA_INTEGER_OP(-, java_subtract, jint, juint)
1106 JAVA_INTEGER_OP(*, java_multiply, jint, juint)
1107 JAVA_INTEGER_OP(+, java_add, jlong, julong)
1108 JAVA_INTEGER_OP(-, java_subtract, jlong, julong)
1109 JAVA_INTEGER_OP(*, java_multiply, jlong, julong)
1110
1111 #undef JAVA_INTEGER_OP
1112
1113 // Provide integer shift operations with Java semantics. No overflow
1114 // issues - left shifts simply discard shifted out bits. No undefined
1115 // behavior for large or negative shift quantities; instead the actual
1116 // shift distance is the argument modulo the lhs value's size in bits.
1117 // No undefined or implementation defined behavior for shifting negative
1118 // values; left shift discards bits, right shift sign extends. We use
1119 // the same safe conversion technique as above for java_add and friends.
1120 #define JAVA_INTEGER_SHIFT_OP(OP, NAME, TYPE, XTYPE) \
1121 inline TYPE NAME (TYPE lhs, jint rhs) { \
1122 const uint rhs_mask = (sizeof(TYPE) * 8) - 1; \
1123 STATIC_ASSERT(rhs_mask == 31 || rhs_mask == 63); \
1124 XTYPE xres = static_cast<XTYPE>(lhs); \
1125 xres OP ## = (rhs & rhs_mask); \
1126 return reinterpret_cast<TYPE&>(xres); \
1127 }
1128
1129 JAVA_INTEGER_SHIFT_OP(<<, java_shift_left, jint, juint)
1130 JAVA_INTEGER_SHIFT_OP(<<, java_shift_left, jlong, julong)
1131 // For signed shift right, assume C++ implementation >> sign extends.
1132 JAVA_INTEGER_SHIFT_OP(>>, java_shift_right, jint, jint)
1133 JAVA_INTEGER_SHIFT_OP(>>, java_shift_right, jlong, jlong)
1134 // For >>> use C++ unsigned >>.
1135 JAVA_INTEGER_SHIFT_OP(>>, java_shift_right_unsigned, jint, juint)
1136 JAVA_INTEGER_SHIFT_OP(>>, java_shift_right_unsigned, jlong, julong)
1137
1138 #undef JAVA_INTEGER_SHIFT_OP
1139
1140 //----------------------------------------------------------------------------------------------------
1141 // The goal of this code is to provide saturating operations for int/uint.
1142 // Checks overflow conditions and saturates the result to min_jint/max_jint.
1143 #define SATURATED_INTEGER_OP(OP, NAME, TYPE1, TYPE2) \
1144 inline int NAME (TYPE1 in1, TYPE2 in2) { \
1145 jlong res = static_cast<jlong>(in1); \
1146 res OP ## = static_cast<jlong>(in2); \
1147 if (res > max_jint) { \
1148 res = max_jint; \
1149 } else if (res < min_jint) { \
1150 res = min_jint; \
1151 } \
1152 return static_cast<int>(res); \
1153 }
1154
1155 SATURATED_INTEGER_OP(+, saturated_add, int, int)
1156 SATURATED_INTEGER_OP(+, saturated_add, int, uint)
1157 SATURATED_INTEGER_OP(+, saturated_add, uint, int)
1158 SATURATED_INTEGER_OP(+, saturated_add, uint, uint)
1159
|