From e2c1ddd7e224abfad28cf60b0bff9d47e1a171c8 Mon Sep 17 00:00:00 2001 From: Alexey Stepanov Date: Thu, 11 Aug 2016 17:23:53 +0300 Subject: [PATCH] translator: add comparison for floating point types --- .../translator/llvm/types/LLVMDoubleType.kt | 24 +++++++++++++++++++ .../translator/llvm/types/LLVMFloatType.kt | 24 +++++++++++++++++++ .../input/floating_point_operators_1.txt | 2 ++ .../kotlin/floating_point_operators_1.kt | 12 ++++++++++ 4 files changed, 62 insertions(+) create mode 100644 translator/src/test/kotlin/tests/input/floating_point_operators_1.txt create mode 100644 translator/src/test/kotlin/tests/kotlin/floating_point_operators_1.kt diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMDoubleType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMDoubleType.kt index c65befa9251..138f0694f31 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMDoubleType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMDoubleType.kt @@ -18,6 +18,30 @@ class LLVMDoubleType() : LLVMType() { override fun operatorPlus(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMDoubleType(), "fadd double $firstOp, $secondOp") + override fun operatorInc(firstOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMDoubleType(), "fadd double $firstOp, 1.0") + + override fun operatorDec(firstOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMDoubleType(), "fsub double $firstOp, 1.0") + + override fun operatorLt(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp olt double $firstOp, $secondOp") + + override fun operatorGt(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp ogt double $firstOp, $secondOp") + + override fun operatorLeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp ole double i32 $firstOp, $secondOp") + + override fun operatorGeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp oge double i32 $firstOp, $secondOp") + + override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp oeq double $firstOp, $secondOp") + + override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp one double $firstOp, $secondOp") + override fun equals(other: Any?): Boolean { return other is LLVMDoubleType } diff --git a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt index e2bd347f7d9..2756c0cf7f7 100644 --- a/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt +++ b/translator/src/main/kotlin/org/kotlinnative/translator/llvm/types/LLVMFloatType.kt @@ -18,6 +18,30 @@ class LLVMFloatType() : LLVMType() { override fun operatorPlus(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = LLVMExpression(LLVMFloatType(), "fadd float $firstOp, $secondOp") + override fun operatorInc(firstOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMDoubleType(), "fadd float $firstOp, 1.0") + + override fun operatorDec(firstOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMDoubleType(), "fsub float $firstOp, 1.0") + + override fun operatorLt(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp olt float $firstOp, $secondOp") + + override fun operatorGt(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp ogt float $firstOp, $secondOp") + + override fun operatorLeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp ole float i32 $firstOp, $secondOp") + + override fun operatorGeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp oge float i32 $firstOp, $secondOp") + + override fun operatorEq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp oeq float $firstOp, $secondOp") + + override fun operatorNeq(firstOp: LLVMSingleValue, secondOp: LLVMSingleValue): LLVMExpression = + LLVMExpression(LLVMBooleanType(), "fcmp one float $firstOp, $secondOp") + override fun equals(other: Any?): Boolean { return other is LLVMFloatType } diff --git a/translator/src/test/kotlin/tests/input/floating_point_operators_1.txt b/translator/src/test/kotlin/tests/input/floating_point_operators_1.txt new file mode 100644 index 00000000000..af281b291ce --- /dev/null +++ b/translator/src/test/kotlin/tests/input/floating_point_operators_1.txt @@ -0,0 +1,2 @@ +floating_point_operators_1_double_leq_Double_Double(14.9301, 5.6) == 0 +floating_point_operators_1_double_leq_Double_Double(1.49301, 5.6) == 1 diff --git a/translator/src/test/kotlin/tests/kotlin/floating_point_operators_1.kt b/translator/src/test/kotlin/tests/kotlin/floating_point_operators_1.kt new file mode 100644 index 00000000000..951a1fb499b --- /dev/null +++ b/translator/src/test/kotlin/tests/kotlin/floating_point_operators_1.kt @@ -0,0 +1,12 @@ +fun floating_point_operators_1_double_leq(x: Double, y: Double): Int { + if (x < y){ + return 1 + } + else{ + return 0 + } +} + +fun floating_point_operators_1_getValue(b: Boolean): Boolean{ + return b +} \ No newline at end of file