Skip to content

Instantly share code, notes, and snippets.

View babanin's full-sized avatar

Ivan Babanin babanin

View GitHub Profile
@babanin
babanin / StoreFloatsInDouble.java
Created August 12, 2013 09:34
pack two floats in single double
import java.nio.ByteBuffer;
/**
* @author Ivan Babanin ([email protected])
*/
public class StoreFloatsInDouble {
public static void main(String[] args) {
double pack = pack(1.1f, 1.2f);
float[] unpack = unpack(pack);
System.out.println(unpack[0] + "\n" + unpack[1]);
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
* @author Ivan Babanin ([email protected])
*/
public class TestFJP {
public static void main(String[] args) throws InterruptedException {
ForkJoinPool forkJoinPool = new ForkJoinPool(1);
forkJoinPool.submit(new Task(0));
static {
// high value may be configured by property
int h = 127;
String integerCacheHighPropValue =
sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
if (integerCacheHighPropValue != null) {
int i = parseInt(integerCacheHighPropValue);
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
@babanin
babanin / quiz1.java
Last active December 30, 2015 04:19
Integer x = 127;
System.out.println(x); // 1
System.out.println(127); // 2
System.out.println((Integer) 127); // 3
System.out.println(new Integer(127)); // 4
private static class LongCache {
private LongCache(){}
static final Long cache[] = new Long[-(-128) + 127 + 1];
static {
for(int i = 0; i < cache.length; i++)
cache[i] = new Long(i - 128);
}
}
Field value = Integer.class.getDeclaredField("value");
value.setAccessible(true);
value.set((Integer) 127, 0);
import java.security.MessageDigest
import scala.io.Source
/**
* Created by Ivan Babanin ([email protected]) on 4/6/2014
*/
object MD5File {
def hash(filePath: String): String =
MessageDigest.getInstance("MD5").digest(Source.fromFile(filePath).map(_.toByte).toArray)
.map( 0xFF & _ )
@babanin
babanin / jetty-web.xml
Created November 29, 2014 20:25
Jetty Virtualhosts
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Set name="contextPath">/</Set>
<Set name="virtualHosts">
<Array type="java.lang.String">
<Item>${YOUR_HOST_NAME}</Item>
<Item>${YOUR_HOST_NAME_2}</Item>
</Array>
package org.openjdk;
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;
// Compile with "-mavx2 -std=c99 -flax-vector-conversions -O3"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <immintrin.h>
#include <time.h>
#include <sys/time.h>
#define TEST_ITER 10000