You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_ssum.c 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*****************************************************************************
  2. Copyright (c) 2023, The OpenBLAS Project
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. 1. Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. 2. Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in
  11. the documentation and/or other materials provided with the
  12. distribution.
  13. 3. Neither the name of the OpenBLAS project nor the names of
  14. its contributors may be used to endorse or promote products
  15. derived from this software without specific prior written
  16. permission.
  17. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  26. USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. **********************************************************************************/
  28. #include "utest/openblas_utest.h"
  29. #include <cblas.h>
  30. #define ELEMENTS 50
  31. #define INCREMENT 2
  32. #ifdef BUILD_SINGLE
  33. /**
  34. * Fortran API specific test
  35. * Test ssum by comparing it against pre-calculated values
  36. */
  37. CTEST(ssum, bad_args_N_0){
  38. blasint i;
  39. blasint N = 0, inc = 1;
  40. float x[ELEMENTS];
  41. for (i = 0; i < ELEMENTS * inc; i ++) {
  42. x[i] = 1000 - i;
  43. }
  44. float sum = BLASFUNC(ssum)(&N, x, &inc);
  45. ASSERT_DBL_NEAR_TOL(0.0f, sum, SINGLE_EPS);
  46. }
  47. /**
  48. * Fortran API specific test
  49. * Test ssum by comparing it against pre-calculated values
  50. */
  51. CTEST(ssum, step_zero){
  52. blasint i;
  53. blasint N = ELEMENTS, inc = 0;
  54. float x[ELEMENTS];
  55. x[0] = 0.0f;
  56. for (i = 0; i < N * inc; i ++) {
  57. x[i] = i + 1000;
  58. }
  59. x[8] = 0.0f;
  60. float sum = BLASFUNC(ssum)(&N, x, &inc);
  61. ASSERT_DBL_NEAR_TOL(0.0f, sum, SINGLE_EPS);
  62. }
  63. /**
  64. * Fortran API specific test
  65. * Test ssum by comparing it against pre-calculated values
  66. */
  67. CTEST(ssum, step_1_N_1){
  68. blasint N = 1, inc = 1;
  69. float x[] = {1.1f};
  70. float sum = BLASFUNC(ssum)(&N, x, &inc);
  71. ASSERT_DBL_NEAR_TOL(1.1f, sum, SINGLE_EPS);
  72. }
  73. /**
  74. * Fortran API specific test
  75. * Test ssum by comparing it against pre-calculated values
  76. */
  77. CTEST(ssum, step_2_N_1){
  78. blasint N = 1, inc = 2;
  79. float x[] = {1.1f, 0.0f};
  80. float sum = BLASFUNC(ssum)(&N, x, &inc);
  81. ASSERT_DBL_NEAR_TOL(1.1f, sum, SINGLE_EPS);
  82. }
  83. /**
  84. * Fortran API specific test
  85. * Test ssum by comparing it against pre-calculated values
  86. */
  87. CTEST(ssum, step_1_N_2){
  88. blasint N = 2, inc = 1;
  89. float x[] = {1.1f, -1.0f};
  90. float sum = BLASFUNC(ssum)(&N, x, &inc);
  91. ASSERT_DBL_NEAR_TOL(0.1f, sum, SINGLE_EPS);
  92. }
  93. /**
  94. * Fortran API specific test
  95. * Test ssum by comparing it against pre-calculated values
  96. */
  97. CTEST(ssum, step_2_N_2){
  98. blasint N = 2, inc = 2;
  99. float x[] = {1.1f, -1.5f, 1.0f, 1.0f};
  100. float sum = BLASFUNC(ssum)(&N, x, &inc);
  101. ASSERT_DBL_NEAR_TOL(2.1f, sum, SINGLE_EPS);
  102. }
  103. /**
  104. * Fortran API specific test
  105. * Test ssum by comparing it against pre-calculated values
  106. */
  107. CTEST(ssum, step_1_N_3){
  108. blasint N = 3, inc = 1;
  109. float x[] = {1.1f, 1.0f, 2.2f};
  110. float sum = BLASFUNC(ssum)(&N, x, &inc);
  111. ASSERT_DBL_NEAR_TOL(4.3f, sum, SINGLE_EPS);
  112. }
  113. /**
  114. * Fortran API specific test
  115. * Test ssum by comparing it against pre-calculated values
  116. */
  117. CTEST(ssum, step_2_N_3){
  118. blasint N = 3, inc = 2;
  119. float x[] = {1.1f, 0.0f, -1.0f, -3.0f, 2.2f, 3.0f};
  120. float sum = BLASFUNC(ssum)(&N, x, &inc);
  121. ASSERT_DBL_NEAR_TOL(2.3f, sum, SINGLE_EPS);
  122. }
  123. /**
  124. * Fortran API specific test
  125. * Test ssum by comparing it against pre-calculated values
  126. */
  127. CTEST(ssum, step_1_N_4){
  128. blasint N = 4, inc = 1;
  129. float x[] = {1.1f, 1.0f, -2.2f, 3.3f};
  130. float sum = BLASFUNC(ssum)(&N, x, &inc);
  131. ASSERT_DBL_NEAR_TOL(3.2f, sum, SINGLE_EPS);
  132. }
  133. /**
  134. * Fortran API specific test
  135. * Test ssum by comparing it against pre-calculated values
  136. */
  137. CTEST(ssum, step_2_N_4){
  138. blasint N = 4, inc = 2;
  139. float x[] = {1.1f, 0.0f, 1.0f, 2.0f, 2.2f, 2.7f, -3.3f, -5.9f};
  140. float sum = BLASFUNC(ssum)(&N, x, &inc);
  141. ASSERT_DBL_NEAR_TOL(1.0f, sum, SINGLE_EPS);
  142. }
  143. /**
  144. * Fortran API specific test
  145. * Test ssum by comparing it against pre-calculated values
  146. */
  147. CTEST(ssum, step_1_N_5){
  148. blasint N = 5, inc = 1;
  149. float x[] = {0.0f, 1.0f, 2.2f, 3.3f, 0.0f};
  150. float sum = BLASFUNC(ssum)(&N, x, &inc);
  151. ASSERT_DBL_NEAR_TOL(6.5f, sum, SINGLE_EPS);
  152. }
  153. /**
  154. * Fortran API specific test
  155. * Test ssum by comparing it against pre-calculated values
  156. */
  157. CTEST(ssum, step_2_N_5){
  158. blasint N = 5, inc = 2;
  159. float x[] = {0.0f, 3.0f, 1.0f, -2.2f, 2.2f, -1.7f, 3.3f, 14.5f, 0.0f, -9.0f};
  160. float sum = BLASFUNC(ssum)(&N, x, &inc);
  161. ASSERT_DBL_NEAR_TOL(6.5f, sum, SINGLE_EPS);
  162. }
  163. /**
  164. * Fortran API specific test
  165. * Test ssum by comparing it against pre-calculated values
  166. */
  167. CTEST(ssum, step_1_N_50){
  168. blasint i;
  169. blasint N = ELEMENTS, inc = 1;
  170. float x[ELEMENTS];
  171. for (i = 0; i < N * inc; i ++) {
  172. x[i] = (i & 1) ? -1.0f : 1.0f;
  173. }
  174. float sum = BLASFUNC(ssum)(&N, x, &inc);
  175. ASSERT_DBL_NEAR_TOL(0.0f, sum, SINGLE_EPS);
  176. }
  177. /**
  178. * Fortran API specific test
  179. * Test ssum by comparing it against pre-calculated values
  180. */
  181. CTEST(ssum, step_2_N_50){
  182. blasint i;
  183. blasint N = ELEMENTS, inc = INCREMENT;
  184. float x[ELEMENTS * INCREMENT];
  185. for (i = 0; i < N * inc; i ++) {
  186. x[i] = (i & 1) ? -1.0f : 1.0f;
  187. }
  188. float sum = BLASFUNC(ssum)(&N, x, &inc);
  189. ASSERT_DBL_NEAR_TOL(50.0f, sum, SINGLE_EPS);
  190. }
  191. #ifndef NO_CBLAS
  192. /**
  193. * C API specific test
  194. * Test ssum by comparing it against pre-calculated values
  195. */
  196. CTEST(ssum, c_api_bad_args_N_0){
  197. blasint i;
  198. blasint N = 0, inc = 1;
  199. float x[ELEMENTS];
  200. for (i = 0; i < ELEMENTS * inc; i ++) {
  201. x[i] = 1000 - i;
  202. }
  203. float sum = cblas_ssum(N, x, inc);
  204. ASSERT_DBL_NEAR_TOL(0.0f, sum, SINGLE_EPS);
  205. }
  206. /**
  207. * C API specific test
  208. * Test ssum by comparing it against pre-calculated values
  209. */
  210. CTEST(ssum, c_api_step_zero){
  211. blasint i;
  212. blasint N = ELEMENTS, inc = 0;
  213. float x[ELEMENTS];
  214. x[0] = 0.0f;
  215. for (i = 0; i < N * inc; i ++) {
  216. x[i] = i + 1000;
  217. }
  218. x[8] = 0.0f;
  219. float sum = cblas_ssum(N, x, inc);
  220. ASSERT_DBL_NEAR_TOL(0.0f, sum, SINGLE_EPS);
  221. }
  222. /**
  223. * C API specific test
  224. * Test ssum by comparing it against pre-calculated values
  225. */
  226. CTEST(ssum, c_api_step_1_N_1){
  227. blasint N = 1, inc = 1;
  228. float x[] = {1.1f};
  229. float sum = cblas_ssum(N, x, inc);
  230. ASSERT_DBL_NEAR_TOL(1.1f, sum, SINGLE_EPS);
  231. }
  232. /**
  233. * C API specific test
  234. * Test ssum by comparing it against pre-calculated values
  235. */
  236. CTEST(ssum, c_api_step_2_N_1){
  237. blasint N = 1, inc = 2;
  238. float x[] = {1.1f, 0.0f};
  239. float sum = cblas_ssum(N, x, inc);
  240. ASSERT_DBL_NEAR_TOL(1.1f, sum, SINGLE_EPS);
  241. }
  242. /**
  243. * C API specific test
  244. * Test ssum by comparing it against pre-calculated values
  245. */
  246. CTEST(ssum, c_api_step_1_N_2){
  247. blasint N = 2, inc = 1;
  248. float x[] = {1.1f, -1.0f};
  249. float sum = cblas_ssum(N, x, inc);
  250. ASSERT_DBL_NEAR_TOL(0.1f, sum, SINGLE_EPS);
  251. }
  252. /**
  253. * C API specific test
  254. * Test ssum by comparing it against pre-calculated values
  255. */
  256. CTEST(ssum, c_api_step_2_N_2){
  257. blasint N = 2, inc = 2;
  258. float x[] = {1.1f, -1.5f, 1.0f, 1.0f};
  259. float sum = cblas_ssum(N, x, inc);
  260. ASSERT_DBL_NEAR_TOL(2.1f, sum, SINGLE_EPS);
  261. }
  262. /**
  263. * C API specific test
  264. * Test ssum by comparing it against pre-calculated values
  265. */
  266. CTEST(ssum, c_api_step_1_N_3){
  267. blasint N = 3, inc = 1;
  268. float x[] = {1.1f, 1.0f, 2.2f};
  269. float sum = cblas_ssum(N, x, inc);
  270. ASSERT_DBL_NEAR_TOL(4.3f, sum, SINGLE_EPS);
  271. }
  272. /**
  273. * C API specific test
  274. * Test ssum by comparing it against pre-calculated values
  275. */
  276. CTEST(ssum, c_api_step_2_N_3){
  277. blasint N = 3, inc = 2;
  278. float x[] = {1.1f, 0.0f, -1.0f, -3.0f, 2.2f, 3.0f};
  279. float sum = cblas_ssum(N, x, inc);
  280. ASSERT_DBL_NEAR_TOL(2.3f, sum, SINGLE_EPS);
  281. }
  282. /**
  283. * C API specific test
  284. * Test ssum by comparing it against pre-calculated values
  285. */
  286. CTEST(ssum, c_api_step_1_N_4){
  287. blasint N = 4, inc = 1;
  288. float x[] = {1.1f, 1.0f, -2.2f, 3.3f};
  289. float sum = cblas_ssum(N, x, inc);
  290. ASSERT_DBL_NEAR_TOL(3.2f, sum, SINGLE_EPS);
  291. }
  292. /**
  293. * C API specific test
  294. * Test ssum by comparing it against pre-calculated values
  295. */
  296. CTEST(ssum, c_api_step_2_N_4){
  297. blasint N = 4, inc = 2;
  298. float x[] = {1.1f, 0.0f, 1.0f, 2.0f, 2.2f, 2.7f, -3.3f, -5.9f};
  299. float sum = cblas_ssum(N, x, inc);
  300. ASSERT_DBL_NEAR_TOL(1.0f, sum, SINGLE_EPS);
  301. }
  302. /**
  303. * C API specific test
  304. * Test ssum by comparing it against pre-calculated values
  305. */
  306. CTEST(ssum, c_api_step_1_N_5){
  307. blasint N = 5, inc = 1;
  308. float x[] = {0.0f, 1.0f, 2.2f, 3.3f, 0.0f};
  309. float sum = cblas_ssum(N, x, inc);
  310. ASSERT_DBL_NEAR_TOL(6.5f, sum, SINGLE_EPS);
  311. }
  312. /**
  313. * C API specific test
  314. * Test ssum by comparing it against pre-calculated values
  315. */
  316. CTEST(ssum, c_api_step_2_N_5){
  317. blasint N = 5, inc = 2;
  318. float x[] = {0.0f, 3.0f, 1.0f, -2.2f, 2.2f, -1.7f, 3.3f, 14.5f, 0.0f, -9.0f};
  319. float sum = cblas_ssum(N, x, inc);
  320. ASSERT_DBL_NEAR_TOL(6.5f, sum, SINGLE_EPS);
  321. }
  322. /**
  323. * C API specific test
  324. * Test ssum by comparing it against pre-calculated values
  325. */
  326. CTEST(ssum, c_api_step_1_N_50){
  327. blasint i;
  328. blasint N = ELEMENTS, inc = 1;
  329. float x[ELEMENTS];
  330. for (i = 0; i < N * inc; i ++) {
  331. x[i] = (i & 1) ? -1.0f : 1.0f;
  332. }
  333. float sum = cblas_ssum(N, x, inc);
  334. ASSERT_DBL_NEAR_TOL(0.0f, sum, SINGLE_EPS);
  335. }
  336. /**
  337. * C API specific test
  338. * Test ssum by comparing it against pre-calculated values
  339. */
  340. CTEST(ssum, c_api_step_2_N_50){
  341. blasint i;
  342. blasint N = ELEMENTS, inc = INCREMENT;
  343. float x[ELEMENTS * INCREMENT];
  344. for (i = 0; i < N * inc; i ++) {
  345. x[i] = (i & 1) ? -1.0f : 1.0f;
  346. }
  347. float sum = cblas_ssum(N, x, inc);
  348. ASSERT_DBL_NEAR_TOL(50.0f, sum, SINGLE_EPS);
  349. }
  350. #endif
  351. #endif