< prev index next >

test/micro/org/openjdk/bench/jdk/incubator/foreign/points/support/PanamaPoint.java

Print this page

  1 /*
  2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.bench.jdk.incubator.foreign.points.support;
 24 


 25 import jdk.incubator.foreign.FunctionDescriptor;
 26 import jdk.incubator.foreign.SymbolLookup;
 27 import jdk.incubator.foreign.MemoryAddress;

 28 import jdk.incubator.foreign.MemoryLayout;
 29 import jdk.incubator.foreign.MemorySegment;
 30 import jdk.incubator.foreign.CLinker;
 31 import jdk.incubator.foreign.ResourceScope;

 32 
 33 import java.lang.invoke.MethodHandle;
 34 import java.lang.invoke.VarHandle;
 35 
 36 import static java.lang.invoke.MethodType.methodType;
 37 import static jdk.incubator.foreign.MemoryLayout.PathElement.groupElement;
 38 import static jdk.incubator.foreign.CLinker.*;
 39 
 40 public class PanamaPoint implements AutoCloseable {
 41 
 42     public static final MemoryLayout LAYOUT = MemoryLayout.structLayout(
 43         C_INT.withName("x"),
 44         C_INT.withName("y")
 45     );
 46 
 47     private static final VarHandle VH_x = LAYOUT.varHandle(int.class, groupElement("x"));
 48     private static final VarHandle VH_y = LAYOUT.varHandle(int.class, groupElement("y"));
 49     private static final MethodHandle MH_distance;
 50     private static final MethodHandle MH_distance_ptrs;
 51 
 52     static {
 53         CLinker abi = CLinker.getInstance();
 54         System.loadLibrary("Point");
 55         SymbolLookup lookup = SymbolLookup.loaderLookup();
 56         MH_distance = abi.downcallHandle(
 57             lookup.lookup("distance").get(),
 58             methodType(double.class, MemorySegment.class, MemorySegment.class),
 59             FunctionDescriptor.of(C_DOUBLE, LAYOUT, LAYOUT)
 60         );
 61         MH_distance_ptrs = abi.downcallHandle(
 62             lookup.lookup("distance_ptrs").get(),
 63             methodType(double.class, MemoryAddress.class, MemoryAddress.class),
 64             FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_POINTER)
 65         );
 66     }
 67 
 68     private final MemorySegment segment;
 69 
 70     public PanamaPoint(int x, int y) {
 71         this(MemorySegment.allocateNative(LAYOUT, ResourceScope.newConfinedScope()), x, y);
 72     }
 73 
 74     public PanamaPoint(MemorySegment segment, int x, int y) {
 75         this(segment);
 76         setX(x);
 77         setY(y);
 78     }
 79 
 80     public PanamaPoint(MemorySegment segment) {
 81         this.segment = segment;
 82     }
 83 
 84     public void setX(int x) {

 90     }
 91 
 92     public void setY(int y) {
 93         VH_y.set(segment, y);
 94     }
 95 
 96     public int getY() {
 97         return (int) VH_y.get(segment);
 98     }
 99 
100     public double distanceTo(PanamaPoint other) {
101         try {
102             return (double) MH_distance.invokeExact(segment, other.segment);
103         } catch (Throwable throwable) {
104             throw new InternalError(throwable);
105         }
106     }
107 
108     public double distanceToPtrs(PanamaPoint other) {
109         try {
110             return (double) MH_distance_ptrs.invokeExact(segment.address(), other.segment.address());
111         } catch (Throwable throwable) {
112             throw new InternalError(throwable);
113         }
114     }
115 
116     @Override
117     public void close() {
118         segment.scope().close();
119     }
120 }

  1 /*
  2  * Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 package org.openjdk.bench.jdk.incubator.foreign.points.support;
 24 
 25 import jdk.incubator.foreign.Addressable;
 26 import jdk.incubator.foreign.CLinker;
 27 import jdk.incubator.foreign.FunctionDescriptor;

 28 import jdk.incubator.foreign.MemoryAddress;
 29 import jdk.incubator.foreign.SymbolLookup;
 30 import jdk.incubator.foreign.MemoryLayout;
 31 import jdk.incubator.foreign.MemorySegment;

 32 import jdk.incubator.foreign.ResourceScope;
 33 import org.openjdk.bench.jdk.incubator.foreign.CLayouts;
 34 
 35 import java.lang.invoke.MethodHandle;
 36 import java.lang.invoke.VarHandle;
 37 
 38 import static java.lang.invoke.MethodType.methodType;
 39 import static jdk.incubator.foreign.MemoryLayout.PathElement.groupElement;

 40 
 41 public class PanamaPoint extends CLayouts implements AutoCloseable {
 42 
 43     public static final MemoryLayout LAYOUT = MemoryLayout.structLayout(
 44         C_INT.withName("x"),
 45         C_INT.withName("y")
 46     );
 47 
 48     private static final VarHandle VH_x = LAYOUT.varHandle(groupElement("x"));
 49     private static final VarHandle VH_y = LAYOUT.varHandle(groupElement("y"));
 50     private static final MethodHandle MH_distance;
 51     private static final MethodHandle MH_distance_ptrs;
 52 
 53     static {
 54         CLinker abi = CLinker.systemCLinker();
 55         System.loadLibrary("Point");
 56         SymbolLookup lookup = SymbolLookup.loaderLookup();
 57         MH_distance = abi.downcallHandle(
 58             lookup.lookup("distance").get(),
 59                 FunctionDescriptor.of(C_DOUBLE, LAYOUT, LAYOUT)

 60         );
 61         MH_distance_ptrs = abi.downcallHandle(
 62                 lookup.lookup("distance_ptrs").get(),
 63                 FunctionDescriptor.of(C_DOUBLE, C_POINTER, C_POINTER)

 64         );
 65     }
 66 
 67     private final MemorySegment segment;
 68 
 69     public PanamaPoint(int x, int y) {
 70         this(MemorySegment.allocateNative(LAYOUT, ResourceScope.newConfinedScope()), x, y);
 71     }
 72 
 73     public PanamaPoint(MemorySegment segment, int x, int y) {
 74         this(segment);
 75         setX(x);
 76         setY(y);
 77     }
 78 
 79     public PanamaPoint(MemorySegment segment) {
 80         this.segment = segment;
 81     }
 82 
 83     public void setX(int x) {

 89     }
 90 
 91     public void setY(int y) {
 92         VH_y.set(segment, y);
 93     }
 94 
 95     public int getY() {
 96         return (int) VH_y.get(segment);
 97     }
 98 
 99     public double distanceTo(PanamaPoint other) {
100         try {
101             return (double) MH_distance.invokeExact(segment, other.segment);
102         } catch (Throwable throwable) {
103             throw new InternalError(throwable);
104         }
105     }
106 
107     public double distanceToPtrs(PanamaPoint other) {
108         try {
109             return (double) MH_distance_ptrs.invokeExact((Addressable)segment, (Addressable)other.segment);
110         } catch (Throwable throwable) {
111             throw new InternalError(throwable);
112         }
113     }
114 
115     @Override
116     public void close() {
117         segment.scope().close();
118     }
119 }
< prev index next >