ctypes.pp 5.2 KB

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