ctypes.pp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. {$ifndef DISABLE_INLINE}
  16. {$define CTYPES_INLINE}
  17. {$endif}
  18. {$define dummy}
  19. {$endif}
  20. interface
  21. {$ifdef unix}
  22. uses unixtype;
  23. {$i aliasctp.inc}
  24. {$else}
  25. type
  26. {$ifndef FPC}
  27. qword = int64; // Keep h2pas "uses ctypes" headers working with delphi.
  28. ptruint = cardinal;
  29. pptruint = ^ptruint;
  30. {$endif}
  31. { the following type definitions are compiler dependant }
  32. { and system dependant }
  33. cint8 = shortint; pcint8 = ^cint8;
  34. cuint8 = byte; pcuint8 = ^cuint8;
  35. cchar = cint8; pcchar = ^cchar;
  36. cschar = cint8; pcschar = ^cschar;
  37. cuchar = cuint8; pcuchar = ^cuchar;
  38. cint16 = smallint; pcint16 = ^cint16;
  39. cuint16 = word; pcuint16 = ^cuint16;
  40. cshort = cint16; pcshort = ^cshort;
  41. csshort = cint16; pcsshort = ^csshort;
  42. cushort = cuint16; pcushort = ^cushort;
  43. cint32 = longint; pcint32 = ^cint32;
  44. cuint32 = longword; pcuint32 = ^cuint32;
  45. cint64 = int64; pcint64 = ^cint64;
  46. cuint64 = qword; pcuint64 = ^cuint64;
  47. clonglong = cint64; pclonglong = ^clonglong;
  48. cslonglong = cint64; pcslonglong = ^cslonglong;
  49. culonglong = cuint64; pculonglong = ^culonglong;
  50. cbool = longbool; pcbool = ^cbool;
  51. {$if defined(cpu64) and not(defined(win64) and defined(cpux86_64))}
  52. cint = cint32; pcint = ^cint; { minimum range is : 32-bit }
  53. csint = cint32; pcsint = ^csint; { minimum range is : 32-bit }
  54. cuint = cuint32; pcuint = ^cuint; { minimum range is : 32-bit }
  55. clong = int64; pclong = ^clong;
  56. cslong = int64; pcslong = ^cslong;
  57. culong = qword; pculong = ^culong;
  58. {$elseif defined(cpu16)}
  59. { 16-bit int sizes checked against Borland C++ 3.1 and Open Watcom 1.9 }
  60. cint = cint16; pcint = ^cint;
  61. csint = cint16; pcsint = ^csint;
  62. cuint = cuint16; pcuint = ^cuint;
  63. clong = longint; pclong = ^clong;
  64. cslong = longint; pcslong = ^cslong;
  65. culong = cardinal; pculong = ^culong;
  66. {$else}
  67. cint = cint32; pcint = ^cint; { minimum range is : 32-bit }
  68. csint = cint32; pcsint = ^csint; { minimum range is : 32-bit }
  69. cuint = cuint32; pcuint = ^cuint; { minimum range is : 32-bit }
  70. clong = longint; pclong = ^clong;
  71. cslong = longint; pcslong = ^cslong;
  72. culong = cardinal; pculong = ^culong;
  73. {$ifend}
  74. csigned = cint; pcsigned = ^csigned;
  75. cunsigned = cuint; pcunsigned = ^cunsigned;
  76. csize_t = ptruint; pcsize_t = pptruint;
  77. // Kylix compat types
  78. u_long = culong;
  79. u_short = cushort;
  80. coff_t = clong;
  81. {$ifndef FPUNONE}
  82. cfloat = single; pcfloat = ^cfloat;
  83. cdouble = double; pcdouble = ^cdouble;
  84. {$endif}
  85. {$endif}
  86. {$if defined(win64) or defined(wince) or defined(android)}
  87. {$define longdouble_is_double}
  88. {$endif}
  89. {$if defined(linux) and (defined(cpupowerpc) or defined(cpuarm))}
  90. {$define longdouble_is_double}
  91. {$ifend}
  92. {$if defined(darwin) and defined(cpuaarch64)}
  93. {$define longdouble_is_double}
  94. {$ifend}
  95. {$ifndef FPUNONE}
  96. {$if defined(longdouble_is_double) or not defined(FPC_HAS_CEXTENDED)}
  97. clongdouble=double;
  98. {$else}
  99. {$if defined(cpui8086) or defined(cpui386) or defined(cpux86_64) or defined(cpuavr)}
  100. clongdouble = cextended;
  101. {$else}
  102. {$define longdouble_assignment_overload_real128}
  103. clongdouble = packed array [0..15] of byte;
  104. {$ifend}
  105. {$ifend}
  106. Pclongdouble=^clongdouble;
  107. {$ifdef longdouble_assignment_overload_real128}
  108. {Non-x86 typically doesn't have extended. To be fixed once this changes.}
  109. operator := (const v:clongdouble) r:double;{$ifdef CTYPES_INLINE}inline;{$endif}
  110. operator := (const v:double) r:clongdouble;{$ifdef CTYPES_INLINE}inline;{$endif}
  111. {$ifdef dummy}
  112. operator +(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  113. operator +(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  114. operator -(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  115. operator -(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  116. operator *(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  117. operator *(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  118. operator /(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  119. operator /(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  120. operator =(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  121. operator =(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  122. operator <(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  123. operator <(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  124. operator >(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  125. operator >(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  126. operator >=(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  127. operator >=(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  128. operator <=(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  129. operator <=(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  130. {$endif dummy}
  131. {$endif}
  132. {$endif FPUNONE}
  133. implementation
  134. {$ifndef FPUNONE}
  135. {$ifdef longdouble_assignment_overload_real128}
  136. {$ifdef ENDIAN_LITTLE}
  137. const r128_mantissa_ofs=0;
  138. r128_exponent_ofs=14;
  139. {$else}
  140. const r128_mantissa_ofs=2;
  141. r128_exponent_ofs=0;
  142. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  143. {$define USE_UNALIGNED}
  144. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  145. {$endif}
  146. operator := (const v:clongdouble) r:double;
  147. var
  148. exp : word;
  149. mant : qword;
  150. is_neg : boolean;
  151. begin
  152. is_neg:=(pword(@v[r128_exponent_ofs])^and $8000)<>0;
  153. exp:=((Pword(@v[r128_exponent_ofs])^and $7fff)-$4000)+$400;
  154. if is_neg then
  155. exp:=exp+$800;
  156. {$ifdef USE_UNALIGNED}
  157. mant:=unaligned(Pqword(@v[r128_mantissa_ofs])^);
  158. {$else not USE_UNALIGNED}
  159. mant:=Pqword(@v[r128_mantissa_ofs])^;
  160. {$endif not USE_UNALIGNED}
  161. qword(r):=(qword(exp) shl 52) or
  162. (mant shr 12);
  163. end;
  164. operator := (const v:double) r:clongdouble;
  165. var
  166. is_neg : boolean;
  167. exp : word;
  168. begin
  169. is_neg:=(qword(v) shr 63) <> 0;
  170. exp:=$4000 + ((qword(v) shr 52) and $7ff) -$400;
  171. if is_neg then
  172. exp:=exp+$8000;
  173. Pword(@r[r128_exponent_ofs])^:=exp;
  174. {$ifdef USE_UNALIGNED}
  175. unaligned(Pqword(@r[r128_mantissa_ofs])^):=qword(v) shl 12;
  176. Pword(@r[r128_mantissa_ofs+8])^:=0;
  177. Pword(@r[r128_mantissa_ofs+10])^:=0;
  178. {$else not USE_UNALIGNED}
  179. Pqword(@r[r128_mantissa_ofs])^:=qword(v) shl 12;
  180. Pcardinal(@r[r128_mantissa_ofs+8])^:=0;
  181. {$endif not USE_UNALIGNED}
  182. Pword(@r[r128_mantissa_ofs+12])^:=0;
  183. end;
  184. {$ifdef dummy}
  185. // There is no record with a value field in this case
  186. operator +(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  187. begin
  188. r:=e+double(c);
  189. end;
  190. operator +(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  191. begin
  192. r:=double(c)+e;
  193. end;
  194. operator -(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  195. begin
  196. r:=e-double(c);
  197. end;
  198. operator -(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  199. begin
  200. r:=double(c)-e;
  201. end;
  202. operator *(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  203. begin
  204. r:=e*double(c);
  205. end;
  206. operator *(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  207. begin
  208. r:=double(c)*e;
  209. end;
  210. operator /(const e:Double;const c:clongdouble) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  211. begin
  212. r:=e/double(c);
  213. end;
  214. operator /(const c:clongdouble;const e:Double) r:Double;{$ifdef CTYPES_INLINE}inline;{$endif}
  215. begin
  216. r:=double(c)/e;
  217. end;
  218. operator =(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  219. begin
  220. r:=e=double(c);
  221. end;
  222. operator =(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  223. begin
  224. r:=double(c)=e;
  225. end;
  226. operator <(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  227. begin
  228. r:=e<double(c);
  229. end;
  230. operator <(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  231. begin
  232. r:=double(c)<e;
  233. end;
  234. operator >(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  235. begin
  236. r:=e>double(c);
  237. end;
  238. operator >(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  239. begin
  240. r:=double(c)>e;
  241. end;
  242. operator >=(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  243. begin
  244. r:=e>=double(c);
  245. end;
  246. operator >=(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  247. begin
  248. r:=double(c)>=e;
  249. end;
  250. operator <=(const e:Double;const c:clongdouble) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  251. begin
  252. r:=e<=double(c);
  253. end;
  254. operator <=(const c:clongdouble;const e:Double) r:boolean;{$ifdef CTYPES_INLINE}inline;{$endif}
  255. begin
  256. r:=double(c)<=e;
  257. end;
  258. {$endif}
  259. {$endif}
  260. {$endif FPUNONE}
  261. end.