ctypes.pp 10.0 KB

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