ctypes.pp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. {
  2. This file is part of the Free Pascal run time library.
  3. Copyright (c) 2004 by Marco van de Voort, member of the
  4. Free Pascal development team
  5. Implements C types for in header conversions
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit ctypes;
  13. {$ifdef FPC}
  14. {$inline on}
  15. {$define dummy}
  16. {$endif}
  17. interface
  18. {$ifdef unix}
  19. uses unixtype;
  20. {$i aliasctp.inc}
  21. {$else}
  22. type
  23. {$ifndef FPC}
  24. qword = int64; // Keep h2pas "uses ctypes" headers working with delphi.
  25. ptruint = cardinal;
  26. pptruint = ^ptruint;
  27. {$endif}
  28. { the following type definitions are compiler dependant }
  29. { and system dependant }
  30. cint8 = shortint; pcint8 = ^cint8;
  31. cuint8 = byte; pcuint8 = ^cuint8;
  32. cchar = cint8; pcchar = ^cchar;
  33. cschar = cint8; pcschar = ^cschar;
  34. cuchar = cuint8; pcuchar = ^cuchar;
  35. cint16 = smallint; pcint16 = ^cint16;
  36. cuint16 = word; pcuint16 = ^cuint16;
  37. cshort = cint16; pcshort = ^cshort;
  38. csshort = cint16; pcsshort = ^csshort;
  39. cushort = cuint16; pcushort = ^cushort;
  40. cint32 = longint; pcint32 = ^cint32;
  41. cuint32 = longword; pcuint32 = ^cuint32;
  42. cint = cint32; pcint = ^cint; { minimum range is : 32-bit }
  43. csint = cint32; pcsint = ^csint; { minimum range is : 32-bit }
  44. cuint = cuint32; pcuint = ^cuint; { minimum range is : 32-bit }
  45. csigned = cint; pcsigned = ^csigned;
  46. cunsigned = cuint; pcunsigned = ^cunsigned;
  47. cint64 = int64; pcint64 = ^cint64;
  48. cuint64 = qword; pcuint64 = ^cuint64;
  49. clonglong = cint64; pclonglong = ^clonglong;
  50. cslonglong = cint64; pcslonglong = ^cslonglong;
  51. culonglong = cuint64; pculonglong = ^culonglong;
  52. cbool = longbool; pcbool = ^cbool;
  53. {$if defined(cpu64) and not(defined(win64) and defined(cpux86_64))}
  54. clong = int64; pclong = ^clong;
  55. cslong = int64; pcslong = ^cslong;
  56. culong = qword; pculong = ^culong;
  57. {$else}
  58. clong = longint; pclong = ^clong;
  59. cslong = longint; pcslong = ^cslong;
  60. culong = cardinal; pculong = ^culong;
  61. {$ifend}
  62. csize_t = ptruint; pcsize_t = pptruint;
  63. // Kylix compat types
  64. u_long = culong;
  65. u_short = cushort;
  66. coff_t = clong;
  67. {$ifndef FPUNONE}
  68. cfloat = single; pcfloat = ^cfloat;
  69. cdouble = double; pcdouble = ^cdouble;
  70. {$endif}
  71. {$endif}
  72. {$ifdef defined(win64) or defined(wince)}
  73. {$define longdouble_is_double}
  74. {$endif}
  75. {$if defined(linux) and (defined(cpupowerpc) or defined(cpuarm))}
  76. {$define longdouble_is_double}
  77. {$ifend}
  78. {$ifndef FPUNONE}
  79. {$ifdef longdouble_is_double}
  80. clongdouble=double;
  81. {$else}
  82. {$if defined(cpui386) or defined(cpux86_64)}
  83. {$define longdouble_assignment_overload_real80}
  84. clongdouble = packed record
  85. value:extended;
  86. {$ifdef defined(cpu64) or defined(darwin)}
  87. padding:array[0..5] of byte;
  88. {$else}
  89. padding:array[0..1] of byte;
  90. {$endif}
  91. end;
  92. {$else}
  93. {$define longdouble_assignment_overload_real128}
  94. clongdouble = packed array [0..15] of byte;
  95. {$ifend}
  96. {$ifend}
  97. Pclongdouble=^clongdouble;
  98. {$ifdef longdouble_assignment_overload_real80}
  99. operator := (const v:clongdouble) r:extended;inline;
  100. operator := (const v:extended) r:clongdouble;inline;
  101. operator +(const e:Extended;const c:clongdouble) r:extended;inline;
  102. operator +(const c:clongdouble;const e:Extended) r:extended;inline;
  103. operator -(const e:Extended;const c:clongdouble) r:extended;inline;
  104. operator -(const c:clongdouble;const e:Extended) r:extended;inline;
  105. operator *(const e:Extended;const c:clongdouble) r:extended;inline;
  106. operator *(const c:clongdouble;const e:Extended) r:extended;inline;
  107. operator /(const e:Extended;const c:clongdouble) r:extended;inline;
  108. operator /(const c:clongdouble;const e:Extended) r:extended;inline;
  109. operator =(const e:Extended;const c:clongdouble) r:boolean;inline;
  110. operator =(const c:clongdouble;const e:Extended) r:boolean;inline;
  111. operator <(const e:Extended;const c:clongdouble) r:boolean;inline;
  112. operator <(const c:clongdouble;const e:Extended) r:boolean;inline;
  113. operator >(const e:Extended;const c:clongdouble) r:boolean;inline;
  114. operator >(const c:clongdouble;const e:Extended) r:boolean;inline;
  115. operator >=(const e:Extended;const c:clongdouble) r:boolean;inline;
  116. operator >=(const c:clongdouble;const e:Extended) r:boolean;inline;
  117. operator <=(const e:Extended;const c:clongdouble) r:boolean;inline;
  118. operator <=(const c:clongdouble;const e:Extended) r:boolean;inline;
  119. {$endif}
  120. {$ifdef longdouble_assignment_overload_real128}
  121. {Non-x86 typically doesn't have extended. To be fixed once this changes.}
  122. operator := (const v:clongdouble) r:double;inline;
  123. operator := (const v:double) r:clongdouble;inline;
  124. {$ifdef dummy}
  125. operator +(const e:Double;const c:clongdouble) r:Double;inline;
  126. operator +(const c:clongdouble;const e:Double) r:Double;inline;
  127. operator -(const e:Double;const c:clongdouble) r:Double;inline;
  128. operator -(const c:clongdouble;const e:Double) r:Double;inline;
  129. operator *(const e:Double;const c:clongdouble) r:Double;inline;
  130. operator *(const c:clongdouble;const e:Double) r:Double;inline;
  131. operator /(const e:Double;const c:clongdouble) r:Double;inline;
  132. operator /(const c:clongdouble;const e:Double) r:Double;inline;
  133. operator =(const e:Double;const c:clongdouble) r:boolean;inline;
  134. operator =(const c:clongdouble;const e:Double) r:boolean;inline;
  135. operator <(const e:Double;const c:clongdouble) r:boolean;inline;
  136. operator <(const c:clongdouble;const e:Double) r:boolean;inline;
  137. operator >(const e:Double;const c:clongdouble) r:boolean;inline;
  138. operator >(const c:clongdouble;const e:Double) r:boolean;inline;
  139. operator >=(const e:Double;const c:clongdouble) r:boolean;inline;
  140. operator >=(const c:clongdouble;const e:Double) r:boolean;inline;
  141. operator <=(const e:Double;const c:clongdouble) r:boolean;inline;
  142. operator <=(const c:clongdouble;const e:Double) r:boolean;inline;
  143. {$endif dummy}
  144. {$endif}
  145. {$endif FPUNONE}
  146. implementation
  147. {$ifndef FPUNONE}
  148. {$ifdef longdouble_assignment_overload_real80}
  149. operator := (const v:clongdouble) r:extended;
  150. begin
  151. r:=v.value;
  152. end;
  153. operator := (const v:extended) r:clongdouble;
  154. begin
  155. r.value:=v;
  156. end;
  157. operator +(const e:Extended;const c:clongdouble) r:extended;inline;
  158. begin
  159. r:=e+c.value;
  160. end;
  161. operator +(const c:clongdouble;const e:Extended) r:extended;inline;
  162. begin
  163. r:=c.value+e;
  164. end;
  165. operator -(const e:Extended;const c:clongdouble) r:extended;inline;
  166. begin
  167. r:=e-c.value;
  168. end;
  169. operator -(const c:clongdouble;const e:Extended) r:extended;inline;
  170. begin
  171. r:=c.value-e;
  172. end;
  173. operator *(const e:Extended;const c:clongdouble) r:extended;inline;
  174. begin
  175. r:=e*c.value;
  176. end;
  177. operator *(const c:clongdouble;const e:Extended) r:extended;inline;
  178. begin
  179. r:=c.value*e;
  180. end;
  181. operator /(const e:Extended;const c:clongdouble) r:extended;inline;
  182. begin
  183. r:=e/c.value;
  184. end;
  185. operator /(const c:clongdouble;const e:Extended) r:extended;inline;
  186. begin
  187. r:=c.value/e;
  188. end;
  189. operator =(const e:Extended;const c:clongdouble) r:boolean;inline;
  190. begin
  191. r:=e=c.value;
  192. end;
  193. operator =(const c:clongdouble;const e:Extended) r:boolean;inline;
  194. begin
  195. r:=c.value=e;
  196. end;
  197. operator <(const e:Extended;const c:clongdouble) r:boolean;inline;
  198. begin
  199. r:=e<c.value;
  200. end;
  201. operator <(const c:clongdouble;const e:Extended) r:boolean;inline;
  202. begin
  203. r:=c.value<e;
  204. end;
  205. operator >(const e:Extended;const c:clongdouble) r:boolean;inline;
  206. begin
  207. r:=e>c.value;
  208. end;
  209. operator >(const c:clongdouble;const e:Extended) r:boolean;inline;
  210. begin
  211. r:=c.value>e;
  212. end;
  213. operator >=(const e:Extended;const c:clongdouble) r:boolean;inline;
  214. begin
  215. r:=e>=c.value;
  216. end;
  217. operator >=(const c:clongdouble;const e:Extended) r:boolean;inline;
  218. begin
  219. r:=c.value>=e;
  220. end;
  221. operator <=(const e:Extended;const c:clongdouble) r:boolean;inline;
  222. begin
  223. r:=e<=c.value;
  224. end;
  225. operator <=(const c:clongdouble;const e:Extended) r:boolean;inline;
  226. begin
  227. r:=c.value<=e;
  228. end;
  229. {$endif}
  230. {$ifdef longdouble_assignment_overload_real128}
  231. {$ifdef ENDIAN_LITTLE}
  232. const r128_mantissa_ofs=0;
  233. r128_exponent_ofs=14;
  234. {$else}
  235. const r128_mantissa_ofs=2;
  236. r128_exponent_ofs=0;
  237. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  238. {$define USE_UNALIGNED}
  239. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  240. {$endif}
  241. operator := (const v:clongdouble) r:double;
  242. var
  243. exp : word;
  244. mant : qword;
  245. is_neg : boolean;
  246. begin
  247. is_neg:=(pword(@v[r128_exponent_ofs])^and $8000)<>0;
  248. exp:=((Pword(@v[r128_exponent_ofs])^and $7fff)-$4000)+$400;
  249. if is_neg then
  250. exp:=exp+$800;
  251. {$ifdef USE_UNALIGNED}
  252. mant:=unaligned(Pqword(@v[r128_mantissa_ofs])^);
  253. {$else not USE_UNALIGNED}
  254. mant:=Pqword(@v[r128_mantissa_ofs])^;
  255. {$endif not USE_UNALIGNED}
  256. qword(r):=(qword(exp) shl 52) or
  257. (mant shr 12);
  258. end;
  259. operator := (const v:double) r:clongdouble;
  260. var
  261. is_neg : boolean;
  262. exp : word;
  263. begin
  264. is_neg:=(qword(v) shr 63) <> 0;
  265. exp:=$4000 + ((qword(v) shr 52) and $7ff) -$400;
  266. if is_neg then
  267. exp:=exp+$8000;
  268. Pword(@r[r128_exponent_ofs])^:=exp;
  269. {$ifdef USE_UNALIGNED}
  270. unaligned(Pqword(@r[r128_mantissa_ofs])^):=qword(v) shl 12;
  271. Pword(@r[r128_mantissa_ofs+8])^:=0;
  272. Pword(@r[r128_mantissa_ofs+10])^:=0;
  273. {$else not USE_UNALIGNED}
  274. Pqword(@r[r128_mantissa_ofs])^:=qword(v) shl 12;
  275. Pcardinal(@r[r128_mantissa_ofs+8])^:=0;
  276. {$endif not USE_UNALIGNED}
  277. Pword(@r[r128_mantissa_ofs+12])^:=0;
  278. end;
  279. {$ifdef dummy}
  280. // There is no record with a value field in this case
  281. operator +(const e:Double;const c:clongdouble) r:Double;inline;
  282. begin
  283. r:=e+double(c);
  284. end;
  285. operator +(const c:clongdouble;const e:Double) r:Double;inline;
  286. begin
  287. r:=double(c)+e;
  288. end;
  289. operator -(const e:Double;const c:clongdouble) r:Double;inline;
  290. begin
  291. r:=e-double(c);
  292. end;
  293. operator -(const c:clongdouble;const e:Double) r:Double;inline;
  294. begin
  295. r:=double(c)-e;
  296. end;
  297. operator *(const e:Double;const c:clongdouble) r:Double;inline;
  298. begin
  299. r:=e*double(c);
  300. end;
  301. operator *(const c:clongdouble;const e:Double) r:Double;inline;
  302. begin
  303. r:=double(c)*e;
  304. end;
  305. operator /(const e:Double;const c:clongdouble) r:Double;inline;
  306. begin
  307. r:=e/double(c);
  308. end;
  309. operator /(const c:clongdouble;const e:Double) r:Double;inline;
  310. begin
  311. r:=double(c)/e;
  312. end;
  313. operator =(const e:Double;const c:clongdouble) r:boolean;inline;
  314. begin
  315. r:=e=double(c);
  316. end;
  317. operator =(const c:clongdouble;const e:Double) r:boolean;inline;
  318. begin
  319. r:=double(c)=e;
  320. end;
  321. operator <(const e:Double;const c:clongdouble) r:boolean;inline;
  322. begin
  323. r:=e<double(c);
  324. end;
  325. operator <(const c:clongdouble;const e:Double) r:boolean;inline;
  326. begin
  327. r:=double(c)<e;
  328. end;
  329. operator >(const e:Double;const c:clongdouble) r:boolean;inline;
  330. begin
  331. r:=e>double(c);
  332. end;
  333. operator >(const c:clongdouble;const e:Double) r:boolean;inline;
  334. begin
  335. r:=double(c)>e;
  336. end;
  337. operator >=(const e:Double;const c:clongdouble) r:boolean;inline;
  338. begin
  339. r:=e>=double(c);
  340. end;
  341. operator >=(const c:clongdouble;const e:Double) r:boolean;inline;
  342. begin
  343. r:=double(c)>=e;
  344. end;
  345. operator <=(const e:Double;const c:clongdouble) r:boolean;inline;
  346. begin
  347. r:=e<=double(c);
  348. end;
  349. operator <=(const c:clongdouble;const e:Double) r:boolean;inline;
  350. begin
  351. r:=double(c)<=e;
  352. end;
  353. {$endif}
  354. {$endif}
  355. {$endif FPUNONE}
  356. end.