ctypes.pp 11 KB

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