ctypes.pp 10 KB

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