/*
 * Copyright (c) 2016, Swiss Institute of Bioinformatics.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 *  * Neither the name of Oracle nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package sib.swiss.swissprot;

import org.openjdk.jmh.annotations.*;
import java.nio.ByteBuffer;
import java.util.Random;
@State(Scope.Thread)
public class GuanineCytosineCount {
    private static final int size = 2 * 1024 * 1024;
    private ByteBuffer dna;
    @Setup
    public void setUp(){
	dna = ByteBuffer.allocateDirect(size);
	Random random = new Random();
	for (int i=0;i<size;i++) {
		float next = random.nextFloat();
		if (next < 0.3) {
			dna.put((byte) 'a');
		} else if (next < 0.3) {
			dna.put((byte) 't');
		} else if (next < 0.8) {
			dna.put((byte) 'c');
		} else if (next < 0.99) {
			dna.put((byte) 'g'); 
		} else {
			dna.put((byte) 'n');
		}
	}
    }

    @TearDown
    public void tearDown(){
	    dna=null;
    }

    @Benchmark
    public int countUsingSimpleIfStatements() {
	int a=0,c=0,g=0,t =0,n =0;
	for (int i=0;i<size;i++)
	{
		char nucleotide = (char) dna.get(i);

		if (nucleotide == 'a')
			a++;
		if (nucleotide == 'c')
			c++;
		if (nucleotide == 't')
			t++;
		if (nucleotide == 'g')
			g++;
		if (nucleotide == 'n')
			n++;

	}
	int count =a+c+g+t+n;
	assert count == size : "count should equals the array size";
    	return count;
    }

    @Benchmark
    public int countUsingAnArray() {
	int []nucleotides=new int[256];
	for (int i=0;i<size;i++)
	{
		byte nucleotide = dna.get(i);
		nucleotides[nucleotide]++;
	}
	int count = nucleotides['a']+nucleotides['c']+nucleotides['g']+nucleotides['t']+nucleotides['n'];
	assert count == size : "count should equals the array size";
	return count;
    }
}

@@ -1586,16 +1586,910 @@
 E.%0A */%0A%0A
+/**%0A * There are two benchmark loops here. The first is a simple%0A * counting loop, the second uses an array lookup to avoid a %0A * unpredicatable branch.%0A *%0A * However, the first if statements need not branch either.%0A * On X86 they could be compiled to something like this in asm%0A *  cmp  edi, 1 ; compare b to 1, assuming b is stored edi%0A *  lafh        ; puts the flags in the AH register.%0A *  shr 6, eax  ; move the 6th bit to the end%0A *  not eax     ; if equals then the ZF = 0 but we need it to be one,%0A *              ; don't care about the other bits%0A *  and eax, 1  ; if the 6th bit is true then we have 1 set else 0.%0A *              ; promote AH to the full eax register as well%0A *  add eax, r13; Add the 1 if true or 0 if false in the CMP to the%0A *              ; destination register r13.%0A * If this compilation is done the first method could even be faster %0A * than the second.%0A **/%0A
 package 

Document why this is an interesting benchmark