Skip to content

Instantly share code, notes, and snippets.

@travisdowns
Last active March 18, 2019 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save travisdowns/fa71f11a0d650373eb3b319965b336f7 to your computer and use it in GitHub Desktop.
Save travisdowns/fa71f11a0d650373eb3b319965b336f7 to your computer and use it in GitHub Desktop.
package com.example.scrap;
import java.util.ArrayList;
import java.util.stream.Collectors;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.PeekingIterator;
public class DecodeSim {
private static int MAX_BYTES = 16;
private static int MAX_INS = 4;
private static boolean VERBOSE = false;
// various patterns
private static final Iterable<Integer> MONOID_PATTERN = Iterables.cycle(3,3,3,3,4,5,5,6);
ArrayList<Integer> decodeOne(PeekingIterator<Integer> sizeStream) {
int bytes = 0;
ArrayList<Integer> decoded = new ArrayList<>(5);
while (sizeStream.hasNext()) {
int next = sizeStream.peek();
if ((bytes + next) > MAX_BYTES || decoded.size() >= MAX_INS) {
break;
}
bytes += next;
decoded.add(sizeStream.next());
};
if (VERBOSE) {
System.out.println("Decoded " + decoded.size() + " instructions using " + bytes + " bytes: "
+ decoded);
}
return decoded;
}
void decodeMany(Iterable<Integer> sizes) {
int cycles = 0, instructions = 0, bytes = 0;
PeekingIterator<Integer> itr = Iterators.peekingIterator(sizes.iterator());
System.out.println(String.format("%8s%12s%12s%12s", "cycles", "IPC", "bytes/cycle", "decoded"));
while (itr.hasNext()) {
ArrayList<Integer> result = decodeOne(itr);
instructions += result.size();
bytes += result.stream().mapToInt(Integer::intValue).sum();
cycles++;
System.out.println(String.format("%8d%12.3f%12.3f%12s", cycles,
(double)instructions/cycles, (double)bytes/cycles, packedStr(result)));
}
}
private Object packedStr(ArrayList<Integer> a) {
return a.stream().map(i -> i.toString()).collect(Collectors.joining());
}
public static void main(String[] args) {
DecodeSim sim = new DecodeSim();
// Iterable<Integer> ALL4 = Iterables.cycle(4);
sim.decodeMany(Iterables.limit(MONOID_PATTERN, 1000));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment