Skip to content

Reusable @CCode Library Example

Bogdan edited this page Jun 21, 2026 · 1 revision
package lib;

import net.sixik.ga_utils.javatogpu.api.anotations.CCode;
import net.sixik.ga_utils.javatogpu.api.anotations.CCodeLibrary;

@CCodeLibrary
public final class ReusableMath {

    static final float SCALE = 0.5f;

    @CCode(inline = true)
    public static float square(float value) {
        return (value * value) * SCALE;
    }

    @CCode
    public static float norm(float value) {
        return square(value) + 1.0f;
    }
}
package sample;

import lib.ReusableMath;
import net.sixik.ga_utils.javatogpu.api.GPU;
import net.sixik.ga_utils.javatogpu.api.anotations.GPUGlobal;

public final class Demo {

    @net.sixik.ga_utils.javatogpu.api.anotations.GPU
    public static void kernel(@GPUGlobal float[] input, @GPUGlobal float[] output) {
        int id = GPU.get_global_id(0);
        output[id] = ReusableMath.norm(input[id]);
    }
}

Notes:

  • compile the helper library with the JavaToGpu processor enabled
  • put the compiled helper library classes or JAR on the consumer compilation classpath
  • mark reusable helper owner classes with @CCodeLibrary

Clone this wiki locally