// ghetto implementations of likely/unlikely in ChucK // mark cerqueira fun int likely() { return (maybe || maybe); } fun int unlikely() { return (maybe && maybe); } fun int likely(int howLikely) { maybe => int answer; for (0 => int i; i < howLikely; i++) answer || maybe => answer; return answer; } fun int unlikely(int howUnlikely) { maybe => int answer; for (0 => int i; i < howUnlikely; i++) answer && maybe => answer; return answer; } //------------------------------------------- 100000 => int trials; 0 => float hits; <<< "RUNNING TESTS WITH", trials, "TRIALS\n">>>; <<< "Test 1: likely()" >>>; for (0 => int i; i < trials; i++) { if (likely()) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>; <<< "Test 2: unlikely()" >>>; 0 => hits; for (0 => int i; i < trials; i++) { if (unlikely()) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>; <<< "Test 3: likely(1)" >>>; 0 => hits; for (0 => int i; i < trials; i++) { if (likely(1)) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>; <<< "Test 4: likely(2)" >>>; 0 => hits; for (0 => int i; i < trials; i++) { if (likely(2)) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>; <<< "Test 5: likely(3)" >>>; 0 => hits; for (0 => int i; i < trials; i++) { if (likely(3)) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>; <<< "Test 6: unlikely(1)" >>>; 0 => hits; for (0 => int i; i < trials; i++) { if (unlikely(1)) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>; <<< "Test 7: unlikely(2)" >>>; 0 => hits; for (0 => int i; i < trials; i++) { if (unlikely(2)) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>; <<< "Test 8: unlikely(3)" >>>; 0 => hits; for (0 => int i; i < trials; i++) { if (unlikely(3)) hits + 1 => hits; 1::samp => now; } <<< "\t\t% hits =", hits/trials, "\n" >>>;