test-operator-overloading.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* Test of C++ API. */
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "libgccjit++.h"
  5. #include <sstream>
  6. #include "harness.h"
  7. struct quadratic
  8. {
  9. double a;
  10. double b;
  11. double c;
  12. double discriminant;
  13. };
  14. /* As per test-quadratic.cc, let's try to inject the equivalent of:
  15. extern double sqrt (double);
  16. void
  17. calc_discriminant (struct quadratic *q)
  18. {
  19. // (b^2 - 4ac)
  20. q->discriminant = (q->b * q->b) - (4 * q->a * q->c);
  21. }
  22. int
  23. test_quadratic (double a, double b, double c, double *r1, double *r2)
  24. {
  25. struct quadratic q;
  26. q.a = a;
  27. q.b = b;
  28. q.c = c;
  29. calc_discriminant (&q);
  30. if (q.discriminant > 0)
  31. {
  32. double s = sqrt (q.discriminant);
  33. *r1 = (-b + s) / (2 * a);
  34. *r2 = (-b - s) / (2 * a);
  35. return 2;
  36. }
  37. else if (q.discriminant == 0)
  38. {
  39. *r1 = -b / (2 * a);
  40. return 1;
  41. }
  42. else return 0;
  43. }
  44. However, we'll use operator overloading for maxium brevity, at the
  45. risk of perhaps being too "magical".
  46. */
  47. /****************************************************************************
  48. Test case
  49. ****************************************************************************/
  50. struct quadratic_test
  51. {
  52. gccjit::context ctxt;
  53. /* "double" and "(double *)". */
  54. gccjit::type numeric_type;
  55. gccjit::type numeric_type_ptr;
  56. /* The value (double)0. */
  57. gccjit::rvalue zero;
  58. gccjit::type int_type;
  59. gccjit::type void_type;
  60. /* "struct quadratic" */
  61. gccjit::type quadratic;
  62. gccjit::field a;
  63. gccjit::field b;
  64. gccjit::field c;
  65. gccjit::field discriminant;
  66. /* "(struct quadratic *)" */
  67. gccjit::type quadratic_ptr;
  68. gccjit::function calc_discriminant;
  69. gccjit::function sqrt;
  70. };
  71. static void
  72. make_types (quadratic_test &testcase)
  73. {
  74. testcase.numeric_type = testcase.ctxt.get_type (GCC_JIT_TYPE_DOUBLE);
  75. testcase.numeric_type_ptr = testcase.numeric_type.get_pointer ();
  76. testcase.zero = testcase.ctxt.zero (testcase.numeric_type);
  77. testcase.int_type = testcase.ctxt.get_int_type <int> ();
  78. testcase.void_type = testcase.ctxt.get_type (GCC_JIT_TYPE_VOID);
  79. testcase.a = testcase.ctxt.new_field (testcase.numeric_type, "a");
  80. testcase.b = testcase.ctxt.new_field (testcase.numeric_type, "b");
  81. testcase.c = testcase.ctxt.new_field (testcase.numeric_type, "c");
  82. testcase.discriminant =
  83. testcase.ctxt.new_field (testcase.numeric_type, "discriminant");
  84. CHECK_STRING_VALUE (testcase.discriminant.get_debug_string ().c_str (),
  85. "discriminant");
  86. std::vector<gccjit::field> fields (4);
  87. fields[0] = testcase.a;
  88. fields[1] = testcase.b;
  89. fields[2] = testcase.c;
  90. fields[3] = testcase.discriminant;
  91. testcase.quadratic =
  92. testcase.ctxt.new_struct_type (
  93. "quadratic",
  94. fields);
  95. testcase.quadratic_ptr = testcase.quadratic.get_pointer ();
  96. }
  97. static void
  98. make_sqrt (quadratic_test &testcase)
  99. {
  100. std::vector<gccjit::param> params (1);
  101. params[0] =
  102. testcase.ctxt.new_param (testcase.numeric_type, "x");
  103. testcase.sqrt =
  104. testcase.ctxt.new_function (GCC_JIT_FUNCTION_IMPORTED,
  105. testcase.numeric_type,
  106. "sqrt",
  107. params,
  108. 0);
  109. }
  110. static void
  111. make_calc_discriminant (quadratic_test &testcase)
  112. {
  113. /* Build "calc_discriminant". */
  114. gccjit::param param_q =
  115. testcase.ctxt.new_param (testcase.quadratic_ptr, "q");
  116. std::vector <gccjit::param> params (1);
  117. params[0] = param_q;
  118. testcase.calc_discriminant =
  119. testcase.ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
  120. testcase.void_type,
  121. "calc_discriminant",
  122. params,
  123. 0);
  124. gccjit::block block = testcase.calc_discriminant.new_block ();
  125. block.add_comment ("(b^2 - 4ac)");
  126. gccjit::rvalue q_a = param_q.dereference_field (testcase.a);
  127. gccjit::rvalue q_b = param_q.dereference_field (testcase.b);
  128. gccjit::rvalue q_c = param_q.dereference_field (testcase.c);
  129. gccjit::rvalue four =
  130. testcase.ctxt.new_rvalue (testcase.numeric_type, 4);
  131. block.add_assignment (
  132. /* q->discriminant =... */
  133. param_q.dereference_field (testcase.discriminant),
  134. /* (q->b * q->b) - (4 * q->a * q->c) */
  135. (q_b * q_b) - (four * q_a * q_c));
  136. block.end_with_return ();
  137. }
  138. static void
  139. make_test_quadratic (quadratic_test &testcase)
  140. {
  141. gccjit::param a = testcase.ctxt.new_param (testcase.numeric_type, "a");
  142. gccjit::param b = testcase.ctxt.new_param (testcase.numeric_type, "b");
  143. gccjit::param c = testcase.ctxt.new_param (testcase.numeric_type, "c");
  144. gccjit::param r1 =
  145. testcase.ctxt.new_param (testcase.numeric_type_ptr, "r1");
  146. gccjit::param r2 =
  147. testcase.ctxt.new_param (testcase.numeric_type_ptr, "r2");
  148. std::vector<gccjit::param> params (5);
  149. params[0] = a;
  150. params[1] = b;
  151. params[2] = c;
  152. params[3] = r1;
  153. params[4] = r2;
  154. gccjit::function test_quadratic =
  155. testcase.ctxt.new_function (GCC_JIT_FUNCTION_EXPORTED,
  156. testcase.int_type,
  157. "test_quadratic",
  158. params,
  159. 0);
  160. /* struct quadratic q; */
  161. gccjit::lvalue q = test_quadratic.new_local (testcase.quadratic, "q");
  162. gccjit::block initial = test_quadratic.new_block ("initial");
  163. gccjit::block on_positive_discriminant
  164. = test_quadratic.new_block ("positive_discriminant");
  165. gccjit::block on_nonpositive_discriminant
  166. = test_quadratic.new_block ("nonpositive_discriminant");
  167. gccjit::block on_zero_discriminant
  168. = test_quadratic.new_block ("zero_discriminant");
  169. gccjit::block on_negative_discriminant
  170. = test_quadratic.new_block ("negative_discriminant");
  171. CHECK_STRING_VALUE (on_zero_discriminant.get_debug_string ().c_str (),
  172. "zero_discriminant");
  173. /* q.a = a; */
  174. initial.add_assignment (q.access_field (testcase.a), a);
  175. /* q.b = b; */
  176. initial.add_assignment (q.access_field (testcase.b), b);
  177. /* q.c = c; */
  178. initial.add_assignment (q.access_field (testcase.c), c);
  179. /* calc_discriminant (&q); */
  180. gccjit::rvalue address_of_q = q.get_address ();
  181. initial.add_eval (testcase.calc_discriminant (address_of_q));
  182. initial.add_comment ("if (q.discriminant > 0)");
  183. initial.end_with_conditional (
  184. q.access_field (testcase.discriminant) > testcase.zero,
  185. on_positive_discriminant,
  186. on_nonpositive_discriminant);
  187. /* Block: "on_positive_discriminant" */
  188. /* double s = sqrt (q.discriminant); */
  189. gccjit::lvalue s = test_quadratic.new_local (testcase.numeric_type, "s");
  190. gccjit::rvalue discriminant_of_q = q.access_field (testcase.discriminant);
  191. on_positive_discriminant.add_assignment (s, testcase.sqrt (discriminant_of_q));
  192. gccjit::rvalue minus_b = -b;
  193. gccjit::rvalue two =
  194. testcase.ctxt.new_rvalue (testcase.numeric_type, 2);
  195. gccjit::rvalue two_a = two * a;
  196. CHECK_STRING_VALUE (two_a.get_debug_string ().c_str (),
  197. "(double)2 * a");
  198. on_positive_discriminant.add_comment ("*r1 = (-b + s) / (2 * a);");
  199. on_positive_discriminant.add_assignment (*r1, (minus_b + s) / two_a);
  200. on_positive_discriminant.add_comment ("*r2 = (-b - s) / (2 * a)");
  201. on_positive_discriminant.add_assignment (*r2, (minus_b - s) / two_a);
  202. /* "return 2;" */
  203. on_positive_discriminant.end_with_return (
  204. testcase.ctxt.new_rvalue (testcase.int_type, 2));
  205. /* Block: "on_nonpositive_discriminant" */
  206. /* "else if (q.discriminant == 0)" */
  207. on_nonpositive_discriminant.add_comment ("else if (q.discriminant == 0)");
  208. on_nonpositive_discriminant.end_with_conditional (
  209. q.access_field (testcase.discriminant) == testcase.zero,
  210. on_zero_discriminant,
  211. on_negative_discriminant);
  212. /* Block: "on_zero_discriminant" */
  213. /* if (q.discriminant == 0) */
  214. on_zero_discriminant.add_comment ("*r1 = -b / (2 * a);");
  215. on_zero_discriminant.add_assignment (*r1, minus_b / two_a);
  216. /* "return 1;" */
  217. on_zero_discriminant.end_with_return (testcase.int_type.one ());
  218. /* Block: "on_negative_discriminant" */
  219. /* else return 0; */
  220. on_negative_discriminant.end_with_return (testcase.int_type.zero ());
  221. /* Verify that output stream operator << works. */
  222. std::ostringstream os;
  223. os << "streamed output: " << address_of_q;
  224. CHECK_STRING_VALUE (os.str ().c_str (), "streamed output: &q");
  225. }
  226. void
  227. create_code (gcc_jit_context *ctxt, void *user_data)
  228. {
  229. struct quadratic_test testcase;
  230. memset (&testcase, 0, sizeof (testcase));
  231. testcase.ctxt = ctxt;
  232. make_types (testcase);
  233. make_sqrt (testcase);
  234. make_calc_discriminant (testcase);
  235. make_test_quadratic (testcase);
  236. }
  237. void
  238. verify_code (gcc_jit_context *ctxt, gcc_jit_result *result)
  239. {
  240. typedef int (*fn_type) (double a, double b, double c,
  241. double *r1, double *r2);
  242. CHECK_NON_NULL (result);
  243. fn_type test_quadratic =
  244. (fn_type)gcc_jit_result_get_code (result, "test_quadratic");
  245. CHECK_NON_NULL (test_quadratic);
  246. /* Verify that the code correctly solves quadratic equations. */
  247. double r1, r2;
  248. /* This one has two solutions: */
  249. CHECK_VALUE (test_quadratic (1, 3, -4, &r1, &r2), 2);
  250. CHECK_VALUE (r1, 1);
  251. CHECK_VALUE (r2, -4);
  252. /* This one has one solution: */
  253. CHECK_VALUE (test_quadratic (4, 4, 1, &r1, &r2), 1);
  254. CHECK_VALUE (r1, -0.5);
  255. /* This one has no real solutions: */
  256. CHECK_VALUE (test_quadratic (4, 1, 1, &r1, &r2), 0);
  257. }