Skip to content

Commit 3e600e0

Browse files
committed
[GR-36107] Fix boolean to signed value cast
PullRequest: graal/10952
2 parents b39631a + 4fd34c9 commit 3e600e0

File tree

7 files changed

+158
-11
lines changed

7 files changed

+158
-11
lines changed

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/nodes/cast/LLVMToDoubleNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2016, 2022, Oracle and/or its affiliates.
33
*
44
* All rights reserved.
55
*
@@ -118,7 +118,7 @@ protected LLVMToDoubleNode createRecursive() {
118118

119119
@Specialization
120120
protected double doDouble(boolean from) {
121-
return from ? 1.0 : 0.0;
121+
return from ? -1.0 : 0.0;
122122
}
123123

124124
@Specialization

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/nodes/cast/LLVMToFloatNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2016, 2022, Oracle and/or its affiliates.
33
*
44
* All rights reserved.
55
*
@@ -116,7 +116,7 @@ protected LLVMToFloatNode createRecursive() {
116116

117117
@Specialization
118118
protected float doFloat(boolean from) {
119-
return from ? 1.0f : 0.0f;
119+
return from ? -1.0f : 0.0f;
120120
}
121121

122122
@Specialization

sulong/projects/com.oracle.truffle.llvm.runtime/src/com/oracle/truffle/llvm/runtime/nodes/cast/LLVMToVectorNode.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
*
44
* All rights reserved.
55
*
@@ -148,7 +148,7 @@ protected LLVMI8Vector doI1(LLVMI1Vector from) {
148148
assert from.getLength() == getVectorLength();
149149
final byte[] vector = new byte[getVectorLength()];
150150
for (int i = 0; i < getVectorLength(); i++) {
151-
vector[i] = (byte) (from.getValue(i) ? 0xff : 0);
151+
vector[i] = (byte) (from.getValue(i) ? -1 : 0);
152152
}
153153
return LLVMI8Vector.create(vector);
154154
}
@@ -223,7 +223,7 @@ protected LLVMI16Vector doI1(LLVMI1Vector from) {
223223
assert from.getLength() == getVectorLength();
224224
final short[] vector = new short[getVectorLength()];
225225
for (int i = 0; i < getVectorLength(); i++) {
226-
vector[i] = (short) (from.getValue(i) ? 1 : 0);
226+
vector[i] = (short) (from.getValue(i) ? -1 : 0);
227227
}
228228
return LLVMI16Vector.create(vector);
229229
}
@@ -298,7 +298,7 @@ protected LLVMI32Vector doI1(LLVMI1Vector from) {
298298
assert from.getLength() == getVectorLength();
299299
final int[] vector = new int[getVectorLength()];
300300
for (int i = 0; i < getVectorLength(); i++) {
301-
vector[i] = from.getValue(i) ? 1 : 0;
301+
vector[i] = from.getValue(i) ? -1 : 0;
302302
}
303303
return LLVMI32Vector.create(vector);
304304
}
@@ -373,7 +373,7 @@ protected LLVMI64Vector doI1(LLVMI1Vector from) {
373373
assert from.getLength() == getVectorLength();
374374
final long[] vector = new long[getVectorLength()];
375375
for (int i = 0; i < getVectorLength(); i++) {
376-
vector[i] = from.getValue(i) ? 1 : 0;
376+
vector[i] = from.getValue(i) ? -1 : 0;
377377
}
378378
return LLVMI64Vector.create(vector);
379379
}
@@ -460,7 +460,7 @@ protected LLVMFloatVector doI1(LLVMI1Vector from) {
460460
assert from.getLength() == getVectorLength();
461461
final float[] vector = new float[getVectorLength()];
462462
for (int i = 0; i < getVectorLength(); i++) {
463-
vector[i] = from.getValue(i) ? 1 : 0;
463+
vector[i] = from.getValue(i) ? -1 : 0;
464464
}
465465
return LLVMFloatVector.create(vector);
466466
}
@@ -535,7 +535,7 @@ protected LLVMDoubleVector doI1(LLVMI1Vector from) {
535535
assert from.getLength() == getVectorLength();
536536
final double[] vector = new double[getVectorLength()];
537537
for (int i = 0; i < getVectorLength(); i++) {
538-
vector[i] = from.getValue(i) ? 1 : 0;
538+
vector[i] = from.getValue(i) ? -1 : 0;
539539
}
540540
return LLVMDoubleVector.create(vector);
541541
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
2+
target triple = "x86_64-unknown-linux-gnu"
3+
4+
@.str = private unnamed_addr constant [8 x i8] c"%lf,%lf\00"
5+
6+
define i32 @main() #0 {
7+
%1 = sitofp <4 x i1> <i1 0, i1 -1, i1 0, i1 -1> to <4 x double>
8+
%2 = extractelement <4 x double> %1, i64 0
9+
%3 = extractelement <4 x double> %1, i64 1
10+
%4 = call i32 (i8*, ...) @printf(i8* nonnull dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), double %2, double %3)
11+
ret i32 0
12+
}
13+
14+
declare i32 @printf(i8* nocapture readonly, ...) #1
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates.
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without modification, are
7+
* permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this list of
10+
* conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+
* conditions and the following disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
*
16+
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
17+
* endorse or promote products derived from this software without specific prior written
18+
* permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
21+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
* OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <stdio.h>
32+
33+
int a[4] = { 0, 1, 0, 1 };
34+
int main() {
35+
int v[4];
36+
for (int i = 0; i < 4; i++)
37+
v[i] = -(a[i] > 0);
38+
printf("%d,%d", v[0], v[1]);
39+
return 0;
40+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates.
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without modification, are
7+
* permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this list of
10+
* conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+
* conditions and the following disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
*
16+
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
17+
* endorse or promote products derived from this software without specific prior written
18+
* permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
21+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
* OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <stdio.h>
32+
#include <stdlib.h>
33+
34+
int32_t a[4] = { 0, 1, 0, 1 };
35+
int main() {
36+
float v[4];
37+
for (int i = 0; i < 4; i++)
38+
v[i] = -(a[i] > 0);
39+
printf("%f,%f\n", v[0], v[1]);
40+
double w[4];
41+
for (int i = 0; i < 4; i++)
42+
w[i] = -(a[i] > 0);
43+
printf("%lf,%lf", w[0], w[1]);
44+
return 0;
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2022, Oracle and/or its affiliates.
3+
*
4+
* All rights reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without modification, are
7+
* permitted provided that the following conditions are met:
8+
*
9+
* 1. Redistributions of source code must retain the above copyright notice, this list of
10+
* conditions and the following disclaimer.
11+
*
12+
* 2. Redistributions in binary form must reproduce the above copyright notice, this list of
13+
* conditions and the following disclaimer in the documentation and/or other materials provided
14+
* with the distribution.
15+
*
16+
* 3. Neither the name of the copyright holder nor the names of its contributors may be used to
17+
* endorse or promote products derived from this software without specific prior written
18+
* permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
21+
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22+
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23+
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25+
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28+
* OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <stdio.h>
32+
33+
int a;
34+
int b = 1;
35+
36+
float sitofp(int i) {
37+
return -(i > 0);
38+
}
39+
40+
double sitolfp(int i) {
41+
return -(i > 0);
42+
}
43+
44+
int main() {
45+
printf("%f,%f\n", sitofp(a), sitofp(b));
46+
printf("%lf,%lf", sitolfp(a), sitolfp(b));
47+
return 0;
48+
}

0 commit comments

Comments
 (0)