ctypes.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. {$if defined(longdouble_is_double) or not defined(FPC_HAS_CEXTENDED)}
  80. clongdouble=double;
  81. {$else}
  82. {$if defined(cpui386) or defined(cpux86_64) or defined(cpuavr)}
  83. clongdouble = cextended;
  84. {$else}
  85. {$define longdouble_assignment_overload_real128}
  86. clongdouble = packed array [0..15] of byte;
  87. {$ifend}
  88. {$ifend}
  89. Pclongdouble=^clongdouble;
  90. {$ifdef longdouble_assignment_overload_real128}
  91. {Non-x86 typically doesn't have extended. To be fixed once this changes.}
  92. operator := (const v:clongdouble) r:double;inline;
  93. operator := (const v:double) r:clongdouble;inline;
  94. {$ifdef dummy}
  95. operator +(const e:Double;const c:clongdouble) r:Double;inline;
  96. operator +(const c:clongdouble;const e:Double) r:Double;inline;
  97. operator -(const e:Double;const c:clongdouble) r:Double;inline;
  98. operator -(const c:clongdouble;const e:Double) r:Double;inline;
  99. operator *(const e:Double;const c:clongdouble) r:Double;inline;
  100. operator *(const c:clongdouble;const e:Double) r:Double;inline;
  101. operator /(const e:Double;const c:clongdouble) r:Double;inline;
  102. operator /(const c:clongdouble;const e:Double) r:Double;inline;
  103. operator =(const e:Double;const c:clongdouble) r:boolean;inline;
  104. operator =(const c:clongdouble;const e:Double) r:boolean;inline;
  105. operator <(const e:Double;const c:clongdouble) r:boolean;inline;
  106. operator <(const c:clongdouble;const e:Double) r:boolean;inline;
  107. operator >(const e:Double;const c:clongdouble) r:boolean;inline;
  108. operator >(const c:clongdouble;const e:Double) r:boolean;inline;
  109. operator >=(const e:Double;const c:clongdouble) r:boolean;inline;
  110. operator >=(const c:clongdouble;const e:Double) r:boolean;inline;
  111. operator <=(const e:Double;const c:clongdouble) r:boolean;inline;
  112. operator <=(const c:clongdouble;const e:Double) r:boolean;inline;
  113. {$endif dummy}
  114. {$endif}
  115. {$endif FPUNONE}
  116. implementation
  117. {$ifndef FPUNONE}
  118. {$ifdef longdouble_assignment_overload_real128}
  119. {$ifdef ENDIAN_LITTLE}
  120. const r128_mantissa_ofs=0;
  121. r128_exponent_ofs=14;
  122. {$else}
  123. const r128_mantissa_ofs=2;
  124. r128_exponent_ofs=0;
  125. {$ifdef FPC_REQUIRES_PROPER_ALIGNMENT}
  126. {$define USE_UNALIGNED}
  127. {$endif FPC_REQUIRES_PROPER_ALIGNMENT}
  128. {$endif}
  129. operator := (const v:clongdouble) r:double;
  130. var
  131. exp : word;
  132. mant : qword;
  133. is_neg : boolean;
  134. begin
  135. is_neg:=(pword(@v[r128_exponent_ofs])^and $8000)<>0;
  136. exp:=((Pword(@v[r128_exponent_ofs])^and $7fff)-$4000)+$400;
  137. if is_neg then
  138. exp:=exp+$800;
  139. {$ifdef USE_UNALIGNED}
  140. mant:=unaligned(Pqword(@v[r128_mantissa_ofs])^);
  141. {$else not USE_UNALIGNED}
  142. mant:=Pqword(@v[r128_mantissa_ofs])^;
  143. {$endif not USE_UNALIGNED}
  144. qword(r):=(qword(exp) shl 52) or
  145. (mant shr 12);
  146. end;
  147. operator := (const v:double) r:clongdouble;
  148. var
  149. is_neg : boolean;
  150. exp : word;
  151. begin
  152. is_neg:=(qword(v) shr 63) <> 0;
  153. exp:=$4000 + ((qword(v) shr 52) and $7ff) -$400;
  154. if is_neg then
  155. exp:=exp+$8000;
  156. Pword(@r[r128_exponent_ofs])^:=exp;
  157. {$ifdef USE_UNALIGNED}
  158. unaligned(Pqword(@r[r128_mantissa_ofs])^):=qword(v) shl 12;
  159. Pword(@r[r128_mantissa_ofs+8])^:=0;
  160. Pword(@r[r128_mantissa_ofs+10])^:=0;
  161. {$else not USE_UNALIGNED}
  162. Pqword(@r[r128_mantissa_ofs])^:=qword(v) shl 12;
  163. Pcardinal(@r[r128_mantissa_ofs+8])^:=0;
  164. {$endif not USE_UNALIGNED}
  165. Pword(@r[r128_mantissa_ofs+12])^:=0;
  166. end;
  167. {$ifdef dummy}
  168. // There is no record with a value field in this case
  169. operator +(const e:Double;const c:clongdouble) r:Double;inline;
  170. begin
  171. r:=e+double(c);
  172. end;
  173. operator +(const c:clongdouble;const e:Double) r:Double;inline;
  174. begin
  175. r:=double(c)+e;
  176. end;
  177. operator -(const e:Double;const c:clongdouble) r:Double;inline;
  178. begin
  179. r:=e-double(c);
  180. end;
  181. operator -(const c:clongdouble;const e:Double) r:Double;inline;
  182. begin
  183. r:=double(c)-e;
  184. end;
  185. operator *(const e:Double;const c:clongdouble) r:Double;inline;
  186. begin
  187. r:=e*double(c);
  188. end;
  189. operator *(const c:clongdouble;const e:Double) r:Double;inline;
  190. begin
  191. r:=double(c)*e;
  192. end;
  193. operator /(const e:Double;const c:clongdouble) r:Double;inline;
  194. begin
  195. r:=e/double(c);
  196. end;
  197. operator /(const c:clongdouble;const e:Double) r:Double;inline;
  198. begin
  199. r:=double(c)/e;
  200. end;
  201. operator =(const e:Double;const c:clongdouble) r:boolean;inline;
  202. begin
  203. r:=e=double(c);
  204. end;
  205. operator =(const c:clongdouble;const e:Double) r:boolean;inline;
  206. begin
  207. r:=double(c)=e;
  208. end;
  209. operator <(const e:Double;const c:clongdouble) r:boolean;inline;
  210. begin
  211. r:=e<double(c);
  212. end;
  213. operator <(const c:clongdouble;const e:Double) r:boolean;inline;
  214. begin
  215. r:=double(c)<e;
  216. end;
  217. operator >(const e:Double;const c:clongdouble) r:boolean;inline;
  218. begin
  219. r:=e>double(c);
  220. end;
  221. operator >(const c:clongdouble;const e:Double) r:boolean;inline;
  222. begin
  223. r:=double(c)>e;
  224. end;
  225. operator >=(const e:Double;const c:clongdouble) r:boolean;inline;
  226. begin
  227. r:=e>=double(c);
  228. end;
  229. operator >=(const c:clongdouble;const e:Double) r:boolean;inline;
  230. begin
  231. r:=double(c)>=e;
  232. end;
  233. operator <=(const e:Double;const c:clongdouble) r:boolean;inline;
  234. begin
  235. r:=e<=double(c);
  236. end;
  237. operator <=(const c:clongdouble;const e:Double) r:boolean;inline;
  238. begin
  239. r:=double(c)<=e;
  240. end;
  241. {$endif}
  242. {$endif}
  243. {$endif FPUNONE}
  244. end.