ctypes.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. {$inline on}
  14. interface
  15. {$ifdef unix}
  16. uses unixtype;
  17. {$i aliasctp.inc}
  18. {$else}
  19. type
  20. {$ifndef FPC}
  21. qword = int64; // Keep h2pas "uses ctypes" headers working with delphi.
  22. {$endif}
  23. { the following type definitions are compiler dependant }
  24. { and system dependant }
  25. cint8 = shortint; pcint8 = ^cint8;
  26. cuint8 = byte; pcuint8 = ^cuint8;
  27. cchar = cint8; pcchar = ^cchar;
  28. cschar = cint8; pcschar = ^cschar;
  29. cuchar = cuint8; pcuchar = ^cuchar;
  30. cint16 = smallint; pcint16 = ^cint16;
  31. cuint16 = word; pcuint16 = ^cuint16;
  32. cshort = cint16; pcshort = ^cshort;
  33. csshort = cint16; pcsshort = ^csshort;
  34. cushort = cuint16; pcushort = ^cushort;
  35. cint32 = longint; pcint32 = ^cint32;
  36. cuint32 = longword; pcuint32 = ^cuint32;
  37. cint = cint32; pcint = ^cint; { minimum range is : 32-bit }
  38. csint = cint32; pcsint = ^csint; { minimum range is : 32-bit }
  39. cuint = cuint32; pcuint = ^cuint; { minimum range is : 32-bit }
  40. csigned = cint; pcsigned = ^csigned;
  41. cunsigned = cuint; pcunsigned = ^cunsigned;
  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. clong = int64; pclong = ^clong;
  50. cslong = int64; pcslong = ^cslong;
  51. culong = qword; pculong = ^culong;
  52. {$else}
  53. clong = longint; pclong = ^clong;
  54. cslong = longint; pcslong = ^cslong;
  55. culong = cardinal; pculong = ^culong;
  56. {$endif}
  57. cfloat = single; pcfloat = ^cfloat;
  58. cdouble = double; pcdouble = ^cdouble;
  59. {$endif}
  60. {$ifdef windows}
  61. {$define longdouble_is_double}
  62. {$endif}
  63. {$if defined(linux) and (defined(cpupowerpc) or defined(cpuarm))}
  64. {$define longdouble_is_double}
  65. {$endif}
  66. {$ifdef longdouble_is_double}
  67. clongdouble=double;
  68. {$else}
  69. {$ifdef x86}
  70. {$define longdouble_assignment_overload_real80}
  71. clongdouble = packed record
  72. value:extended;
  73. {$ifdef defined(cpu64) or defined(darwin)}
  74. padding:array[0..5] of byte;
  75. {$else}
  76. padding:array[0..1] of byte;
  77. {$endif}
  78. end;
  79. {$else}
  80. {$define longdouble_assignment_overload_real128}
  81. clongdouble = packed array [0..15] of byte;
  82. {$endif}
  83. {$endif}
  84. Pclongdouble=^clongdouble;
  85. // Kylix compat types
  86. u_long = culong;
  87. u_short = cushort;
  88. {$ifdef longdouble_assignment_overload_real80}
  89. operator := (const v:clongdouble):r:extended;
  90. operator := (const v:extended):r:clongdouble;
  91. {$endif}
  92. {$ifdef longdouble_assignment_overload_real128}
  93. {Non-x86 typically doesn't have extended. To be fixed once this changes.}
  94. operator := (const v:clongdouble) r:double;
  95. operator := (const v:double) r:clongdouble;
  96. {$endif}
  97. implementation
  98. {$ifdef longdouble_assignment_overload_real80}
  99. operator := (const v:clongdouble) r:extended;inline;
  100. begin
  101. r:=v.value;
  102. end;
  103. operator := (const v:extended) r:clongdouble;inline;
  104. begin
  105. r.value:=v;
  106. end;
  107. {$endif}
  108. {$ifdef longdouble_assignment_overload_real128}
  109. {$ifdef ENDIAN_LITTLE}
  110. const r128_mantissa_ofs=0;
  111. r128_exponent_ofs=14;
  112. {$else}
  113. const r128_mantissa_ofs=2;
  114. r128_exponent_ofs=0;
  115. {$endif}
  116. operator := (const v:clongdouble) r:double;inline;
  117. begin
  118. qword(r):=(qword(Pword(@v[r128_exponent_ofs])^) shl 52) or
  119. (Pqword(@v[r128_mantissa_ofs])^ shr 12);
  120. end;
  121. operator := (const v:double) r:clongdouble;inline;
  122. begin
  123. Pword(@r[r128_exponent_ofs])^:=qword(v) shr 52;
  124. Pqword(@r[r128_mantissa_ofs])^:=qword(v) shl 12;
  125. Pcardinal(@r[r128_mantissa_ofs+8])^:=0;
  126. Pword(@r[r128_mantissa_ofs+12])^:=0;
  127. end;
  128. {$endif}
  129. end.