cpprisc16  March 16, 2017
 All Namespaces Files Functions Variables Typedefs Macros
benchmark.cpp
Go to the documentation of this file.
1 /* -*- coding: latin-1 -*- */
2 
3 /** \file benchmark.cpp (March 14, 2017)
4  * \brief Simple example program to compare different multiplication algorithms.
5  *
6  * Piece of cpprisc16.
7  * https://bitbucket.org/OPiMedia/cpprisc16
8  *
9  * GPLv3 --- Copyright (C) 2017 Olivier Pirson
10  * http://www.opimedia.be/
11  */
12 
13 #include <cmath>
14 #include <cstdlib>
15 
16 #include <iostream>
17 
18 #include "cpprisc16/cpprisc16.hpp"
20 
21 using namespace cpprisc16;
22 
23 
24 int
25 main() {
26  const unsigned int nb = 100000;
27 
29  clear_memory();
31  srand(666);
32 
33  // Products
34  std::cout << "x_mul: standard algorithm --- ";
35  for (unsigned int i = 0; i < nb; ++i) {
36  p_movi(1, rand());
37  p_movi(2, rand());
38 
39  x_mul(1, 2, 3, 4, 5, 6, 7);
40  }
41  std::cout << nb_executed << '/' << nb << " = "
42  << std::lround(static_cast<double>(nb_executed)/nb) << std::endl;
43  println_all();
44 
45 
47  clear_memory();
49  srand(666);
50 
51  std::cout << std::endl
52  << "x_mul_karatsuba: Karatsuba algorithm --- ";
53  for (unsigned int i = 0; i < nb; ++i) {
54  p_movi(1, rand());
55  p_movi(2, rand());
56 
57  x_mul_karatsuba(1, 2, 3, 4, 5, 6, 7);
58  }
59  std::cout << nb_executed << '/' << nb << " = "
60  << std::lround(static_cast<double>(nb_executed)/nb) << std::endl;
61  println_all();
62 
63 
64  // Squares
66  clear_memory();
68  srand(666);
69 
70  std::cout << std::endl
71  << "=== Squares ===" << std::endl
72  << "x_mul: standard algorithm --- ";
73  for (unsigned int i = 0; i < nb; ++i) {
74  p_movi(1, rand());
75  p_movi(2, registers[1]);
76 
77  x_mul(1, 2, 3, 4, 5, 6, 7);
78  }
79  std::cout << nb_executed << '/' << nb << " = "
80  << std::lround(static_cast<double>(nb_executed)/nb) << std::endl;
81  println_all();
82 
83 
85  clear_memory();
87  srand(666);
88 
89  std::cout << std::endl
90  << "x_sqr --- ";
91  for (unsigned int i = 0; i < nb; ++i) {
92  p_movi(1, rand());
93  p_movi(2, registers[1]);
94 
95  x_sqr(1, 2, 3, 4, 5, 6, 7);
96  }
97  std::cout << nb_executed << '/' << nb << " = "
98  << std::lround(static_cast<double>(nb_executed)/nb) << std::endl;
99  println_all();
100 
101  return EXIT_SUCCESS;
102 }
void p_movi(unsigned int result, immed_t immed)
(MOV Immediate) R[result] <– immed
Definition: cpprisc16.cpp:166
int main()
Definition: benchmark.cpp:25
void clear_memory()
Reset to 0 all memory items and mark them as not used.
Definition: cpprisc16.cpp:191
void x_sqr(unsigned int a, unsigned int result2, unsigned int tmp1, unsigned int tmp2, unsigned int tmp3, unsigned int tmp4, unsigned int tmp5)
R[result2]:R[a] <– R[a]*R[a].
void x_mul_karatsuba(unsigned int a, unsigned int b, unsigned int tmp1, unsigned int tmp2, unsigned int tmp3, unsigned int tmp4, unsigned int tmp5)
R[b]:R[a] <– R[a] * R[b] by Karatsuba algorithm: https://en.wikipedia.org/wiki/Karatsuba_algorithm.
word16_t registers[8]
Registers.
Definition: cpprisc16.cpp:43
void x_mul(unsigned int a, unsigned int b, unsigned int tmp1, unsigned int tmp2, unsigned int tmp3, unsigned int tmp4, unsigned int tmp5)
R[b]:R[a] <– R[a] * R[b] by standard algorithm: https://en.wikipedia.org/wiki/Multiplication_algorit...
Instructions set of RiSC16: 8 instructions i_* and 4 pseudo-instructions p_*.
uint64_t nb_executed
Number of instructions executed.
Definition: cpprisc16.cpp:41
void clear_registers()
Reset to 0 all registers.
Definition: cpprisc16.cpp:206
void println_all()
Print infos, registers and memory (if used).
Definition: cpprisc16.cpp:260
void clear_nb_executed()
Reset the number of executed instructions.
Definition: cpprisc16.cpp:200
Extended instructions set: some extra operations x_* implemented with RiSC16.