From 4765a20ae8d949381309588e7ce50daf7aef28c4 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Thu, 19 Jan 2017 17:19:04 +0300 Subject: [PATCH] Add bitwise operations for Short and Byte This commit adds and, or and xor operations to Byte and Short types. It also implements inv operation for all integer types, shl for Long and Char + Int operation. --- runtime/src/main/cpp/Operator.cpp | 19 ++++++++++--- runtime/src/main/kotlin/kotlin/Primitives.kt | 28 +++++++++++++++++++- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/runtime/src/main/cpp/Operator.cpp b/runtime/src/main/cpp/Operator.cpp index d3e9b49c479..0561ae40e0c 100644 --- a/runtime/src/main/cpp/Operator.cpp +++ b/runtime/src/main/cpp/Operator.cpp @@ -16,7 +16,7 @@ KInt Kotlin_Boolean_compareTo_Boolean(KBoolean a, KBoolean b) { if (a == b) //--- Char --------------------------------------------------------------------// KInt Kotlin_Char_compareTo_Char (KChar a, KChar b) { if (a == b) return 0; return (a < b) ? -1 : 1; } -KChar Kotlin_Char_plus_Char (KChar a, KInt b) { return a + b; } +KChar Kotlin_Char_plus_Int (KChar a, KInt b) { return a + b; } KInt Kotlin_Char_minus_Char (KChar a, KChar b) { return a - b; } KChar Kotlin_Char_minus_Int (KChar a, KInt b) { return a - b; } KChar Kotlin_Char_inc (KChar a ) { return a + 1; } @@ -79,6 +79,11 @@ KByte Kotlin_Byte_dec (KByte a ) { return --a; } KInt Kotlin_Byte_unaryPlus (KByte a ) { return +a; } KInt Kotlin_Byte_unaryMinus (KByte a ) { return -a; } +KByte Kotlin_Byte_or_Byte (KByte a, KByte b) { return a | b; } +KByte Kotlin_Byte_xor_Byte (KByte a, KByte b) { return a ^ b; } +KByte Kotlin_Byte_and_Byte (KByte a, KByte b) { return a & b; } +KByte Kotlin_Byte_inv (KByte a ) { return ~a; } + KByte Kotlin_Byte_toByte (KByte a ) { return a; } KChar Kotlin_Byte_toChar (KByte a ) { return a; } KShort Kotlin_Byte_toShort (KByte a ) { return a; } @@ -136,6 +141,11 @@ KShort Kotlin_Short_dec (KShort a ) { return --a; } KInt Kotlin_Short_unaryPlus (KShort a ) { return +a; } KInt Kotlin_Short_unaryMinus (KShort a ) { return -a; } +KShort Kotlin_Short_or_Short (KShort a, KShort b) { return a | b; } +KShort Kotlin_Short_xor_Short (KShort a, KShort b) { return a ^ b; } +KShort Kotlin_Short_and_Short (KShort a, KShort b) { return a & b; } +KShort Kotlin_Short_inv (KShort a ) { return ~a; } + KByte Kotlin_Short_toByte (KShort a ) { return a; } KChar Kotlin_Short_toChar (KShort a ) { return a; } KShort Kotlin_Short_toShort (KShort a ) { return a; } @@ -196,6 +206,7 @@ KInt Kotlin_Int_unaryMinus (KInt a ) { return -a; } KInt Kotlin_Int_or_Int (KInt a, KInt b) { return a | b; } KInt Kotlin_Int_xor_Int (KInt a, KInt b) { return a ^ b; } KInt Kotlin_Int_and_Int (KInt a, KInt b) { return a & b; } +KInt Kotlin_Int_inv (KInt a ) { return ~a; } KInt Kotlin_Int_shl_Int (KInt a, KInt b) { return a << b; } KInt Kotlin_Int_shr_Int (KInt a, KInt b) { return a >> b; } KInt Kotlin_Int_ushr_Int (KInt a, KInt b) { @@ -262,9 +273,9 @@ KLong Kotlin_Long_unaryMinus (KLong a ) { return -a; } KLong Kotlin_Long_xor_Long (KLong a, KLong b) { return a ^ b; } KLong Kotlin_Long_or_Long (KLong a, KLong b) { return a | b; } KLong Kotlin_Long_and_Long (KLong a, KLong b) { return a & b; } -KLong Kotlin_Long_shr_Int (KLong a, KInt b) { - return a >> b; -} +KLong Kotlin_Long_inv (KLong a ) { return ~a; } +KLong Kotlin_Long_shl_Int (KLong a, KInt b) { return a << b; } +KLong Kotlin_Long_shr_Int (KLong a, KInt b) { return a >> b; } KLong Kotlin_Long_ushr_Int (KLong a, KInt b) { return static_cast(a) >> b; } diff --git a/runtime/src/main/kotlin/kotlin/Primitives.kt b/runtime/src/main/kotlin/kotlin/Primitives.kt index 439470934ec..f4d11b7f18f 100644 --- a/runtime/src/main/kotlin/kotlin/Primitives.kt +++ b/runtime/src/main/kotlin/kotlin/Primitives.kt @@ -168,6 +168,19 @@ public final class Byte : Number(), Comparable { @SymbolName("Kotlin_Byte_unaryMinus") external public operator fun unaryMinus(): Int + /** Performs a bitwise AND operation between the two values. */ + @SymbolName("Kotlin_Byte_and_Byte") + external public infix fun and(other: Byte): Byte + /** Performs a bitwise OR operation between the two values. */ + @SymbolName("Kotlin_Byte_or_Byte") + external public infix fun or(other: Byte): Byte + /** Performs a bitwise XOR operation between the two values. */ + @SymbolName("Kotlin_Byte_xor_Byte") + external public infix fun xor(other: Byte): Byte + /** Inverts the bits in this value/ */ + @SymbolName("Kotlin_Byte_inv") + external public fun inv(): Byte + @SymbolName("Kotlin_Byte_toByte") external public override fun toByte(): Byte @SymbolName("Kotlin_Byte_toChar") @@ -377,6 +390,19 @@ public final class Short : Number(), Comparable { @SymbolName("Kotlin_Short_unaryMinus") external public operator fun unaryMinus(): Int + /** Performs a bitwise AND operation between the two values. */ + @SymbolName("Kotlin_Short_and_Short") + external public infix fun and(other: Short): Short + /** Performs a bitwise OR operation between the two values. */ + @SymbolName("Kotlin_Short_or_Short") + external public infix fun or(other: Short): Short + /** Performs a bitwise XOR operation between the two values. */ + @SymbolName("Kotlin_Short_xor_Short") + external public infix fun xor(other: Short): Short + /** Inverts the bits in this value/ */ + @SymbolName("Kotlin_Short_inv") + external public fun inv(): Short + /** Creates a range from this value to the specified [other] value. */ public operator fun rangeTo(other: Byte): IntRange { return IntRange(this.toInt(), other.toInt()) @@ -861,7 +887,7 @@ public final class Long : Number(), Comparable { } /** Shifts this value left by [bits]. */ - @SymbolName("Kotlin_Long_shl_Long") + @SymbolName("Kotlin_Long_shl_Int") external public infix fun shl(bitCount: Int): Long /** Shifts this value right by [bits], filling the leftmost bits with copies of the sign bit. */ @SymbolName("Kotlin_Long_shr_Int")