summaryrefslogtreecommitdiff
path: root/chapter18
diff options
context:
space:
mode:
Diffstat (limited to 'chapter18')
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/node/RoundF32Node.java1
-rw-r--r--chapter18/src/main/java/com/seaofnodes/simple/type/TypeStruct.java2
-rw-r--r--chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java27
3 files changed, 29 insertions, 1 deletions
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/node/RoundF32Node.java b/chapter18/src/main/java/com/seaofnodes/simple/node/RoundF32Node.java
index 78e7698c..10a0e4aa 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/node/RoundF32Node.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/node/RoundF32Node.java
@@ -16,6 +16,7 @@ public class RoundF32Node extends Node {
public Type compute() {
if (in(1)._type instanceof TypeFloat i0 && i0.isConstant() )
return TypeFloat.constant((float)i0.value());
+ if( in(1)._type==TypeFloat.F64 ) return TypeFloat.F32;
return in(1)._type;
}
diff --git a/chapter18/src/main/java/com/seaofnodes/simple/type/TypeStruct.java b/chapter18/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
index bc640955..fff6a7db 100644
--- a/chapter18/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
+++ b/chapter18/src/main/java/com/seaofnodes/simple/type/TypeStruct.java
@@ -49,7 +49,7 @@ public class TypeStruct extends Type {
// Array
public static TypeStruct makeAry(TypeInteger len, int lenAlias, Type body, int bodyAlias) {
- assert body instanceof TypeInteger || (body instanceof TypeNil tn && tn.nullable());
+ assert body instanceof TypeInteger || body instanceof TypeFloat || (body instanceof TypeNil tn && tn.nullable());
return make("[" + body.str() + "]",
Field.make("#" ,len , lenAlias,true ),
Field.make("[]",body,bodyAlias,false));
diff --git a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
index c4d5b9ac..0758fbf5 100644
--- a/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
+++ b/chapter18/src/test/java/com/seaofnodes/simple/Chapter18Test.java
@@ -9,6 +9,33 @@ import static org.junit.Assert.fail;
import org.junit.Ignore;
public class Chapter18Test {
+ @Test public void testFloatArrays() {
+ for( String type : new String[]{"flt","f64","f32"} ) {
+ CodeGen zero = new CodeGen("var a = new "+type+"[arg]; return a[arg-1];")
+ .parse().opto().typeCheck();
+ assertEquals("0.0",Eval2.eval(zero,3));
+ // Eval2 does not execute RoundF32 yet; check nonconstant narrowing here.
+ new CodeGen(type+" x = arg; var a = new "+type+"[arg]; a[0] = x; return a[0];")
+ .parse().opto().typeCheck();
+ }
+ for( String type : new String[]{"flt","f64"} ) {
+ CodeGen loop = new CodeGen("var a = new "+type+"[arg]; "+
+ "for(int i=0; i<arg; i++) a[i] += i+0.25; return a[0]+a[arg-1];")
+ .parse().opto().typeCheck();
+ assertEquals("0.5",Eval2.eval(loop,1));
+ assertEquals("3.5",Eval2.eval(loop,4));
+
+ CodeGen rounded = new CodeGen("var a = new "+type+"[arg]; "+
+ "for(int i=0; i<arg; i++) a[i] = 16777216.0+i; return a[arg-1];")
+ .parse().opto().typeCheck();
+ assertEquals("1.6777217E7",Eval2.eval(rounded,2));
+ }
+ CodeGen rounded = new CodeGen("var a = new f32[arg]; "+
+ "for(int i=0; i<arg; i++) a[i] = 16777217.0; return a[arg-1];")
+ .parse().opto().typeCheck();
+ assertEquals("1.6777216E7",Eval2.eval(rounded,2));
+ }
+
@Test public void testNeverExitLoopDepth() {
String[] sources = {
"if(arg) while(1) {} return 7;",