An Efficient Matrix Multiplication Algorithm for Accelerating Inference in Binary and Ternary Neural Networks