/*
 * Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */
package org.openjdk.bench.java.util;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Warmup;

import java.util.Arrays;
import java.util.concurrent.TimeUnit;

@BenchmarkMode(Mode.Throughput)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@State(Scope.Thread)
@Warmup(iterations = 4, time = 2)
@Measurement(iterations = 4, time = 2)
@Fork(value = 3)
public class ArraysFill {
    private static final int ARRAYS_COUNT = 16;
    private static final int MAX_MISALIGN = 15;

    @Param({"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
            "12", "13", "14", "15", "16", "17", "31", "32", "33",
            "63", "64", "65", "127", "128", "129", "4096"})
    public int size;

    // Need different arrays, because 8-byte alignment will touch
    // different code path depending on 0x....0 or 0x...8 address
    // Use ARRAYS_COUNT arrays to get average result.
    private byte[][] testByteArray;
    private char[][] testCharArray;
    private short[][] testShortArray;
    private int[][] testIntArray;
    private long[][] testLongArray;
    private float[][] testFloatArray;
    private double[][] testDoubleArray;

    // arrays for misaligned access. Will use different initial
    // offset to have different addresses
    private byte[] testByteArray2;
    private char[] testCharArray2;
    private short[] testShortArray2;
    private int[] testIntArray2;
    private long[] testLongArray2;
    private float[] testFloatArray2;
    private double[] testDoubleArray2;

    private int counter;

    @Setup
    public void setup() {
        testByteArray = new byte[ARRAYS_COUNT][size];
        testCharArray = new char[ARRAYS_COUNT][size];
        testShortArray = new short[ARRAYS_COUNT][size];
        testIntArray = new int[ARRAYS_COUNT][size];
        testLongArray = new long[ARRAYS_COUNT][size];
        testFloatArray = new float[ARRAYS_COUNT][size];
        testDoubleArray = new double[ARRAYS_COUNT][size];

        int sizeForMisalign = size + MAX_MISALIGN;
        testByteArray2 = new byte[sizeForMisalign];
        testCharArray2 = new char[sizeForMisalign];
        testShortArray2 = new short[sizeForMisalign];
        testIntArray2 = new int[sizeForMisalign];
        testLongArray2 = new long[sizeForMisalign];
        testFloatArray2 = new float[sizeForMisalign];
        testDoubleArray2 = new double[sizeForMisalign];

        counter = 0;
    }

    @Benchmark
    public void testCharFill() {
        Arrays.fill(testCharArray[counter++ & (ARRAYS_COUNT - 1)], (char) -1);
    }

    @Benchmark
    public void testByteFill() {
        Arrays.fill(testByteArray[counter++ & (ARRAYS_COUNT - 1)], (byte) -1);
    }

    @Benchmark
    public void testShortFill() {
        Arrays.fill(testShortArray[counter++ & (ARRAYS_COUNT - 1)], (short) -1);
    }

    @Benchmark
    public void testIntFill() {
        Arrays.fill(testIntArray[counter++ & (ARRAYS_COUNT - 1)], -1);
    }

    @Benchmark
    public void testLongFill() {
        Arrays.fill(testLongArray[counter++ & (ARRAYS_COUNT - 1)], -1L);
    }

    @Benchmark
    public void testFloatFill() {
        Arrays.fill(testFloatArray[counter++ & (ARRAYS_COUNT - 1)], -1.0f);
    }

    @Benchmark
    public void testDoubleFill() {
        Arrays.fill(testDoubleArray[counter++ & (ARRAYS_COUNT - 1)], -1.0d);
    }

    @Benchmark
    public void testCharZeroFill() {
        Arrays.fill(testCharArray[counter++ & (ARRAYS_COUNT - 1)], (char) 0);
    }

    @Benchmark
    public void testByteZeroFill() {
        Arrays.fill(testByteArray[counter++ & (ARRAYS_COUNT - 1)], (byte) 0);
    }

    @Benchmark
    public void testShortZeroFill() {
        Arrays.fill(testShortArray[counter++ & (ARRAYS_COUNT - 1)], (short) 0);
    }

    @Benchmark
    public void testIntZeroFill() {
        Arrays.fill(testIntArray[counter++ & (ARRAYS_COUNT - 1)], 0);
    }

    @Benchmark
    public void testLongZeroFill() {
        Arrays.fill(testLongArray[counter++ & (ARRAYS_COUNT - 1)], 0L);
    }

    @Benchmark
    public void testFloatZeroFill() {
        Arrays.fill(testFloatArray[counter++ & (ARRAYS_COUNT - 1)], 0.0f);
    }

    @Benchmark
    public void testDoubleFillZero() {
        Arrays.fill(testDoubleArray[counter++ & (ARRAYS_COUNT - 1)], 0.0d);
    }

    @Benchmark
    public void testCharUnalignedFill() {
        int misalign = counter++ & MAX_MISALIGN;
        Arrays.fill(testCharArray2, misalign, size + misalign, (char) -1);
    }

    @Benchmark
    public void testByteUnalignedFill() {
        int misalign = counter++ & MAX_MISALIGN;
        Arrays.fill(testByteArray2, misalign, size + misalign, (byte) -1);
    }

    @Benchmark
    public void testShortUnalignedFill() {
        int misalign = counter++ & MAX_MISALIGN;
        Arrays.fill(testShortArray2, misalign, size + misalign, (short) -1);
    }

    @Benchmark
    public void testIntUnalignedFill() {
        int misalign = counter++ & MAX_MISALIGN;
        Arrays.fill(testIntArray2, misalign, size + misalign, -1);
    }

    @Benchmark
    public void testLongUnalignedFill() {
        int misalign = counter++ & MAX_MISALIGN;
        Arrays.fill(testLongArray2, misalign, size + misalign, -1L);
    }

    @Benchmark
    public void testFloatUnalignedFill() {
        int misalign = counter++ & MAX_MISALIGN;
        Arrays.fill(testFloatArray2, misalign, size + misalign, -1.0f);
    }

    @Benchmark
    public void testDoubleUnalignedFill() {
        int misalign = counter++ & MAX_MISALIGN;
        Arrays.fill(testDoubleArray2, misalign, size + misalign, -1.0d);
    }
}
