tcalext6.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. { Tests passing of different records by value to C methods.
  2. One type of these records has one field which is a simple array of bytes,
  3. the other consists of a few fields of atomic size.
  4. Note that it does not only test a single field of these records, but all
  5. by comparing the sum of the field values with the sum returned by the
  6. C function.
  7. }
  8. program calext6;
  9. {$MODE DELPHI}
  10. { requires libgcc for the C functions }
  11. {$ifdef FPUSOFT}
  12. {$define NO_FLOAT}
  13. {$endif}
  14. {$ifdef CPUARMEL}
  15. { for softfloat calls in the C code }
  16. {$linklib gcc}
  17. {$endif}
  18. type
  19. int8_t = shortint;
  20. pint8_t = ^int8_t;
  21. int16_t = smallint;
  22. int32_t = longint;
  23. int64_t = int64;
  24. var
  25. success : boolean;
  26. {$packrecords c}
  27. type
  28. struct1 = record
  29. v : single;
  30. end;
  31. struct2 = record
  32. v : double;
  33. end;
  34. struct3 = record
  35. v1 : single;
  36. v2 : single;
  37. end;
  38. struct4 = record
  39. v1 : double;
  40. v2 : single;
  41. end;
  42. struct5 = record
  43. v1 : double;
  44. v2 : double;
  45. end;
  46. struct6 = record
  47. v1 : double;
  48. v2 : single;
  49. v3 : single;
  50. end;
  51. struct7 = record
  52. v1 : single;
  53. v2 : int32_t;
  54. v3 : double;
  55. end;
  56. struct8 = record
  57. case byte of
  58. 0: (v1: single);
  59. 1: (d: double);
  60. end;
  61. struct9 = record
  62. v1 : int64_t;
  63. v2 : single;
  64. end;
  65. struct10 = record
  66. v1 : int64_t;
  67. v2 : int16_t;
  68. v3 : single;
  69. end;
  70. struct11 = record
  71. v1 : int64_t;
  72. v2 : double;
  73. end;
  74. struct12 = record
  75. v1 : int64_t;
  76. v2 : single;
  77. v3 : single;
  78. end;
  79. struct13 = record
  80. v1 : double;
  81. v2 : int64_t;
  82. end;
  83. struct14 = record
  84. v1 : double;
  85. v2 : int32_t;
  86. v3 : int16_t;
  87. end;
  88. struct15 = record
  89. v1 : double;
  90. v2 : int32_t;
  91. v3 : single;
  92. end;
  93. struct16 = record
  94. v1 : single;
  95. v2 : single;
  96. v3 : single;
  97. v4 : single;
  98. end;
  99. struct17 = record
  100. v1 : single;
  101. v2 : double;
  102. end;
  103. struct31 = record
  104. v1 : cextended;
  105. v2 : single;
  106. end;
  107. procedure fill(var mem; size : integer);
  108. var
  109. i : Integer;
  110. p : pint8_t;
  111. begin
  112. p := @mem;
  113. for i := 0 to size-1 do begin
  114. p^ := random(255)+1;
  115. inc(p);
  116. end;
  117. end;
  118. procedure verify(val1, val2 : int64_t; nr : Integer); overload;
  119. begin
  120. success := success and (val1 = val2);
  121. Write('Testing test ', nr , ', was ', val1, ', should be ', val2, '...');
  122. if (val1 = val2) then
  123. WriteLn('Success.')
  124. else
  125. WriteLn('Failed');
  126. end;
  127. procedure verify(val1, val2 : double; nr : Integer); overload;
  128. begin
  129. success := success and (val1 = val2);
  130. Write('Testing test ', nr , ', was ', val1, ', should be ', val2, '...');
  131. if (val1 = val2) then
  132. WriteLn('Success.')
  133. else
  134. WriteLn('Failed');
  135. end;
  136. {$ifdef FPC_HAS_TYPE_EXTENDED}
  137. procedure verify(val1, val2 : cextended; nr : Integer); overload;
  138. begin
  139. success := success and (val1 = val2);
  140. Write('Testing test ', nr , ', was ', val1, ', should be ', val2, '...');
  141. if (val1 = val2) then
  142. WriteLn('Success.')
  143. else
  144. WriteLn('Failed');
  145. end;
  146. {$endif FPC_HAS_TYPE_EXTENDED}
  147. function check1(s : struct1) : single;
  148. begin
  149. result := s.v;
  150. end;
  151. function check2(s : struct2) : double;
  152. begin
  153. result := s.v;
  154. end;
  155. function check3(s : struct3) : single;
  156. begin
  157. result := s.v1 + s.v2;
  158. end;
  159. function check4(s : struct4) : double;
  160. begin
  161. result := s.v1 + s.v2;
  162. end;
  163. function check5(s : struct5) : double;
  164. begin
  165. result := s.v1 + s.v2;
  166. end;
  167. function check6(s : struct6) : double;
  168. begin
  169. result := s.v1 + s.v2;
  170. end;
  171. function check7(s : struct7) : double;
  172. begin
  173. result := s.v1 + s.v2 + s.v3;
  174. end;
  175. function check8(s : struct8) : double;
  176. begin
  177. result := s.d;
  178. end;
  179. function check9(s : struct9) : int64_t;
  180. begin
  181. result := s.v1 + trunc(s.v2);
  182. end;
  183. function check10(s : struct10) : int64_t;
  184. begin
  185. result := s.v1 + s.v2 + trunc(s.v3);
  186. end;
  187. function check11(s : struct11) : int64_t;
  188. begin
  189. result := s.v1 + trunc(s.v2);
  190. end;
  191. function check12(s : struct12) : int64_t;
  192. begin
  193. result := s.v1 + trunc(s.v2) + trunc(s.v3);
  194. end;
  195. function check13(s : struct13) : int64_t;
  196. begin
  197. result := trunc(s.v1) + s.v2 ;
  198. end;
  199. function check14(s : struct14) : int64_t;
  200. begin
  201. result := trunc(s.v1) + s.v2 + s.v3;
  202. end;
  203. function check15(s : struct15) : int64_t;
  204. begin
  205. result := trunc(s.v1) + s.v2 + trunc(s.v3);
  206. end;
  207. function check16(s : struct16) : single;
  208. begin
  209. result := s.v1 + s.v2 + s.v3 + s.v4;
  210. end;
  211. function check17(s : struct17) : double;
  212. begin
  213. result := s.v1 + s.v2;
  214. end;
  215. function check31(s : struct31) : cextended;
  216. begin
  217. { don't perform an addition, because that causes the C code to depend on
  218. libgcc }
  219. result := s.v1;
  220. end;
  221. {$L tcext6.o}
  222. function pass1(s : struct1; b: byte) : single; cdecl; external;
  223. function pass2(s : struct2; b: byte) : double; cdecl; external;
  224. function pass3(s : struct3; b: byte) : single; cdecl; external;
  225. function pass4(s : struct4; b: byte) : double; cdecl; external;
  226. function pass5(s : struct5; b: byte) : double; cdecl; external;
  227. function pass6(s : struct6; b: byte) : double; cdecl; external;
  228. function pass61(d1,d2,d3,d4,d5: double; s : struct6; b: byte) : double; cdecl; external;
  229. function pass7(s : struct7; b: byte) : double; cdecl; external;
  230. function pass8(s : struct8; b: byte) : double; cdecl; external;
  231. function pass9(s : struct9; b: byte) : int64_t; cdecl; external;
  232. function pass10(s : struct10; b: byte) : int64_t; cdecl; external;
  233. function pass11(s : struct11; b: byte) : int64_t; cdecl; external;
  234. function pass12(s : struct12; b: byte) : int64_t; cdecl; external;
  235. function pass13(s : struct13; b: byte) : int64_t; cdecl; external;
  236. function pass14(s : struct14; b: byte) : int64_t; cdecl; external;
  237. function pass15(s : struct15; b: byte) : int64_t; cdecl; external;
  238. function pass16(s : struct16; b: byte) : single; cdecl; external;
  239. function pass17(s : struct17; b: byte) : single; cdecl; external;
  240. {$ifdef FPC_HAS_TYPE_EXTENDED}
  241. function pass31(s : struct31; b: byte; var ss: single) : cextended; cdecl; external;
  242. {$endif}
  243. function pass1a(b: byte; s : struct1) : struct1; cdecl; external;
  244. function pass2a(b: byte; s : struct2) : struct2; cdecl; external;
  245. function pass3a(b: byte; s : struct3) : struct3; cdecl; external;
  246. function pass4a(b: byte; s : struct4) : struct4; cdecl; external;
  247. function pass5a(b: byte; s : struct5) : struct5; cdecl; external;
  248. function pass6a(b: byte; s : struct6) : struct6; cdecl; external;
  249. function pass7a(b: byte; s : struct7) : struct7; cdecl; external;
  250. function pass8a(b: byte; s : struct8) : struct8; cdecl; external;
  251. function pass9a(b: byte; s : struct9) : struct9; cdecl; external;
  252. function pass10a(b: byte; s : struct10) : struct10; cdecl; external;
  253. function pass11a(b: byte; s : struct11) : struct11; cdecl; external;
  254. function pass12a(b: byte; s : struct12) : struct12; cdecl; external;
  255. function pass13a(b: byte; s : struct13) : struct13; cdecl; external;
  256. function pass14a(b: byte; s : struct14) : struct14; cdecl; external;
  257. function pass15a(b: byte; s : struct15) : struct15; cdecl; external;
  258. function pass16a(b: byte; s : struct16) : struct16; cdecl; external;
  259. function pass17a(b: byte; s : struct17) : struct17; cdecl; external;
  260. {$ifdef FPC_HAS_TYPE_EXTENDED}
  261. function pass31a(b: byte; s : struct31) : struct31; cdecl; external;
  262. {$endif}
  263. procedure dotest;
  264. var
  265. s1, s1a: struct1;
  266. s2, s2a: struct2;
  267. s3, s3a: struct3;
  268. s4, s4a: struct4;
  269. s5, s5a: struct5;
  270. s6, s6a: struct6;
  271. s7, s7a: struct7;
  272. s8, s8a: struct8;
  273. s9, s9a: struct9;
  274. s10, s10a: struct10;
  275. s11, s11a: struct11;
  276. s12, s12a: struct12;
  277. s13, s13a: struct13;
  278. s14, s14a: struct14;
  279. s15, s15a: struct15;
  280. s16, s16a: struct16;
  281. s17, s17a: struct17;
  282. s31, s31a: struct31;
  283. ss: single;
  284. begin
  285. success := true;
  286. {$ifndef NO_FLOAT}
  287. s1.v:=2.0;
  288. s2.v:=3.0;
  289. s3.v1:=4.5;
  290. s3.v2:=5.125;
  291. s4.v1:=6.175;
  292. s4.v2:=7.5;
  293. s5.v1:=8.075;
  294. s5.v2:=9.000125;
  295. s6.v1:=10.25;
  296. s6.v2:=11.5;
  297. s6.v3:=12.125;
  298. s7.v1:=13.5;
  299. s7.v2:=14;
  300. s7.v3:=15.0625;
  301. s8.d:=16.000575;
  302. s9.v1:=$123456789012345;
  303. s9.v2:=17.0;
  304. s10.v1:=$234567890123456;
  305. s10.v2:=-12399;
  306. s10.v3:=18.0;
  307. s11.v1:=$345678901234567;
  308. s11.v2:=19.0;
  309. s12.v1:=$456789012345678;
  310. s12.v2:=20.0;
  311. s12.v3:=21.0;
  312. s13.v1:=22.0;
  313. s13.v2:=$567890123456789;
  314. s14.v1:=23.0;
  315. s14.v2:=$19283774;
  316. s14.v3:=12356;
  317. s15.v1:=24.0;
  318. s15.v2:=$28195647;
  319. s15.v3:=25.0;
  320. s16.v1:=26.5;
  321. s16.v2:=27.75;
  322. s16.v3:=28.25;
  323. s16.v4:=29.125;
  324. s17.v1:=31.25;
  325. s17.v2:=32.125;
  326. s31.v1:=32.625;
  327. s31.v2:=33.5;
  328. verify(pass1(s1,1), check1(s1), 1);
  329. verify(pass2(s2,2), check2(s2), 2);
  330. verify(pass3(s3,3), check3(s3), 3);
  331. verify(pass4(s4,4), check4(s4), 4);
  332. verify(pass5(s5,5), check5(s5), 5);
  333. verify(pass6(s6,6), check6(s6), 6);
  334. verify(pass7(s7,7), check7(s7), 7);
  335. verify(pass8(s8,8), check8(s8), 8);
  336. verify(pass9(s9,9), check9(s9), 9);
  337. verify(pass10(s10,10), check10(s10), 10);
  338. verify(pass11(s11,11), check11(s11), 11);
  339. verify(pass12(s12,12), check12(s12), 12);
  340. verify(pass13(s13,13), check13(s13), 13);
  341. verify(pass14(s14,14), check14(s14), 14);
  342. verify(pass15(s15,15), check15(s15), 15);
  343. verify(pass16(s16,16), check16(s16), 16);
  344. verify(pass17(s17,17), check17(s17), 17);
  345. {$ifdef FPC_HAS_TYPE_EXTENDED}
  346. verify(pass31(s31,31,ss), check31(s31), 31);
  347. verify(ss,s31.v2,32);
  348. {$endif}
  349. verify(check1(pass1a(1,s1)), check1(s1), 41);
  350. verify(check2(pass2a(2,s2)), check2(s2), 42);
  351. verify(check3(pass3a(3,s3)), check3(s3), 43);
  352. verify(check4(pass4a(4,s4)), check4(s4), 44);
  353. verify(check5(pass5a(5,s5)), check5(s5), 45);
  354. verify(check6(pass6a(6,s6)), check6(s6), 46);
  355. verify(check7(pass7a(7,s7)), check7(s7), 47);
  356. verify(check8(pass8a(8,s8)), check8(s8), 48);
  357. verify(check9(pass9a(9,s9)), check9(s9), 49);
  358. verify(check10(pass10a(10,s10)), check10(s10), 50);
  359. verify(check11(pass11a(11,s11)), check11(s11), 51);
  360. verify(check12(pass12a(12,s12)), check12(s12), 52);
  361. verify(check13(pass13a(13,s13)), check13(s13), 53);
  362. verify(check14(pass14a(14,s14)), check14(s14), 54);
  363. verify(check15(pass15a(15,s15)), check15(s15), 55);
  364. verify(check16(pass16a(16,s16)), check16(s16), 56);
  365. verify(check17(pass17a(17,s17)), check17(s17), 57);
  366. {$ifdef FPC_HAS_TYPE_EXTENDED}
  367. verify(check31(pass31a(31,s31)), check31(s31), 71);
  368. {$endif}
  369. verify(pass1a(1,s1).v, s1.v, 81);
  370. verify(pass2a(2,s2).v, s2.v, 82);
  371. verify(pass3a(3,s3).v1, s3.v1, 83);
  372. verify(pass3a(3,s3).v2, s3.v2, 103);
  373. verify(pass4a(4,s4).v1, s4.v1, 84);
  374. verify(pass5a(5,s5).v1, s5.v1, 85);
  375. verify(pass6a(6,s6).v1, s6.v1, 86);
  376. verify(pass7a(7,s7).v1, s7.v1, 87);
  377. verify(pass7a(7,s7).v2, s7.v2, 107);
  378. verify(pass8a(8,s8).d, s8.d, 88);
  379. verify(pass9a(9,s9).v1, s9.v1, 89);
  380. verify(pass10a(10,s10).v1, s10.v1, 90);
  381. verify(pass10a(10,s10).v2, s10.v2, 90);
  382. verify(pass11a(11,s11).v1, s11.v1, 91);
  383. verify(pass12a(12,s12).v1, s12.v1, 92);
  384. verify(pass13a(13,s13).v1, s13.v1, 93);
  385. verify(pass14a(14,s14).v1, s14.v1, 94);
  386. verify(pass15a(15,s15).v1, s15.v1, 95);
  387. verify(pass16a(16,s16).v1, s16.v1, 96);
  388. verify(pass17a(17,s17).v1, s17.v1, 97);
  389. {$ifdef FPC_HAS_TYPE_EXTENDED}
  390. verify(pass31a(31,s31).v1, s31.v1, 101);
  391. {$endif}
  392. s1a:=pass1a(1,s1);
  393. verify(check1(s1a), check1(s1), 111);
  394. s2a:=pass2a(2,s2);
  395. verify(check2(s2a), check2(s2), 112);
  396. s3a:=pass3a(3,s3);
  397. verify(check3(s3a), check3(s3), 113);
  398. s3a:=pass3a(3,s3);
  399. verify(check3(s3a), check3(s3), 114);
  400. s4a:=pass4a(4,s4);
  401. verify(check4(s4a), check4(s4), 115);
  402. s5a:=pass5a(5,s5);
  403. verify(check5(s5a), check5(s5), 116);
  404. s6a:=pass6a(6,s6);
  405. verify(check6(s6a), check6(s6), 117);
  406. s7a:=pass7a(7,s7);
  407. verify(check7(s7a), check7(s7), 118);
  408. s7a:=pass7a(7,s7);
  409. verify(check7(s7a), check7(s7), 119);
  410. s8a:=pass8a(8,s8);
  411. verify(check8(s8a), check8(s8), 120);
  412. s9a:=pass9a(9,s9);
  413. verify(check9(s9a), check9(s9), 121);
  414. s10a:=pass10a(10,s10);
  415. verify(check10(s10a), check10(s10), 122);
  416. s10a:=pass10a(10,s10);
  417. verify(check10(s10a), check10(s10), 123);
  418. s11a:=pass11a(11,s11);
  419. verify(check11(s11a), check11(s11), 124);
  420. s12a:=pass12a(12,s12);
  421. verify(check12(s12a), check12(s12), 125);
  422. s13a:=pass13a(13,s13);
  423. verify(check13(s13a), check13(s13), 126);
  424. s14a:=pass14a(14,s14);
  425. verify(check14(s14a), check14(s14), 127);
  426. s15a:=pass15a(15,s15);
  427. verify(check15(s15a), check15(s15), 128);
  428. s16a:=pass16a(16,s16);
  429. verify(check16(s16a), check16(s16), 129);
  430. s17a:=pass17a(17,s17);
  431. verify(check17(s17a), check17(s17), 130);
  432. {$ifdef FPC_HAS_TYPE_EXTENDED}
  433. s31a:=pass31a(31,s31);
  434. verify(check31(s31a), check31(s31), 131);
  435. verify(s31.v2,s31a.v2,132);
  436. {$endif}
  437. {$endif ndef nofloat}
  438. if (not success) then
  439. halt(1);
  440. end;
  441. begin
  442. dotest;
  443. end.