ISO_Fortran_binding.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /* Declarations for ISO Fortran binding.
  2. Copyright (C) 2018-2022 Free Software Foundation, Inc.
  3. Contributed by Daniel Celis Garza <celisdanieljr@gmail.com>
  4. This file is part of the GNU Fortran runtime library (libgfortran).
  5. Libgfortran is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. Libgfortran is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #ifndef ISO_FORTRAN_BINDING_H
  21. #define ISO_FORTRAN_BINDING_H
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #include <stddef.h> /* Standard ptrdiff_t tand size_t. */
  26. #include <stdint.h> /* Integer types. */
  27. /* Constants, defined as macros. */
  28. #define CFI_VERSION 1
  29. #define CFI_MAX_RANK 15
  30. /* Attributes. */
  31. #define CFI_attribute_pointer 0
  32. #define CFI_attribute_allocatable 1
  33. #define CFI_attribute_other 2
  34. /* Error codes.
  35. Note that CFI_FAILURE and CFI_INVALID_STRIDE are specific to GCC
  36. and not part of the Fortran standard */
  37. #define CFI_SUCCESS 0
  38. #define CFI_FAILURE 1
  39. #define CFI_ERROR_BASE_ADDR_NULL 2
  40. #define CFI_ERROR_BASE_ADDR_NOT_NULL 3
  41. #define CFI_INVALID_ELEM_LEN 4
  42. #define CFI_INVALID_RANK 5
  43. #define CFI_INVALID_TYPE 6
  44. #define CFI_INVALID_ATTRIBUTE 7
  45. #define CFI_INVALID_EXTENT 8
  46. #define CFI_INVALID_STRIDE 9
  47. #define CFI_INVALID_DESCRIPTOR 10
  48. #define CFI_ERROR_MEM_ALLOCATION 11
  49. #define CFI_ERROR_OUT_OF_BOUNDS 12
  50. /* CFI type definitions. */
  51. typedef ptrdiff_t CFI_index_t;
  52. typedef int8_t CFI_rank_t;
  53. typedef int8_t CFI_attribute_t;
  54. typedef int16_t CFI_type_t;
  55. /* CFI_dim_t. */
  56. typedef struct CFI_dim_t
  57. {
  58. CFI_index_t lower_bound;
  59. CFI_index_t extent;
  60. CFI_index_t sm;
  61. }
  62. CFI_dim_t;
  63. /* CFI_cdesc_t, C descriptors are cast to this structure as follows:
  64. CFI_CDESC_T(CFI_MAX_RANK) foo;
  65. CFI_cdesc_t * bar = (CFI_cdesc_t *) &foo;
  66. */
  67. typedef struct CFI_cdesc_t
  68. {
  69. void *base_addr;
  70. size_t elem_len;
  71. int version;
  72. CFI_rank_t rank;
  73. CFI_attribute_t attribute;
  74. CFI_type_t type;
  75. CFI_dim_t dim[];
  76. }
  77. CFI_cdesc_t;
  78. /* CFI_CDESC_T with an explicit type. */
  79. #define CFI_CDESC_TYPE_T(r, base_type) \
  80. struct { \
  81. base_type *base_addr; \
  82. size_t elem_len; \
  83. int version; \
  84. CFI_rank_t rank; \
  85. CFI_attribute_t attribute; \
  86. CFI_type_t type; \
  87. CFI_dim_t dim[r]; \
  88. }
  89. #define CFI_CDESC_T(r) CFI_CDESC_TYPE_T (r, void)
  90. /* CFI function declarations. */
  91. extern void *CFI_address (const CFI_cdesc_t *, const CFI_index_t []);
  92. extern int CFI_allocate (CFI_cdesc_t *, const CFI_index_t [], const CFI_index_t [],
  93. size_t);
  94. extern int CFI_deallocate (CFI_cdesc_t *);
  95. extern int CFI_establish (CFI_cdesc_t *, void *, CFI_attribute_t, CFI_type_t, size_t,
  96. CFI_rank_t, const CFI_index_t []);
  97. extern int CFI_is_contiguous (const CFI_cdesc_t *);
  98. extern int CFI_section (CFI_cdesc_t *, const CFI_cdesc_t *, const CFI_index_t [],
  99. const CFI_index_t [], const CFI_index_t []);
  100. extern int CFI_select_part (CFI_cdesc_t *, const CFI_cdesc_t *, size_t, size_t);
  101. extern int CFI_setpointer (CFI_cdesc_t *, CFI_cdesc_t *, const CFI_index_t []);
  102. /* Types and kind numbers. Allows bitwise and to reveal the intrinsic type of a kind type. It also allows us to find the kind parameter by inverting the bit-shift equation.
  103. CFI_type_kind_shift = 8
  104. CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
  105. CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0
  106. CFI_type_example = CFI_intrinsic_type + (CFI_type_kind << CFI_type_kind_shift)
  107. Defining the CFI_type_example.
  108. CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0 << CFI_type_kind_shift
  109. -------------------------
  110. 1 0 0 0 0 0 0 0 0 0 0 0 +
  111. CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
  112. -------------------------
  113. CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0
  114. Finding the intrinsic type with the logical mask.
  115. CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0 &
  116. CFI_type_mask = 0 0 0 0 1 1 1 1 1 1 1 1
  117. -------------------------
  118. CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
  119. Using the intrinsic type and kind shift to find the kind value of the type.
  120. CFI_type_kind = (CFI_type_example - CFI_intrinsic_type) >> CFI_type_kind_shift
  121. CFI_type_example = 1 0 0 0 0 0 0 0 0 0 1 0 -
  122. CFI_intrinsic_type = 0 0 0 0 0 0 0 0 0 0 1 0
  123. -------------------------
  124. 1 0 0 0 0 0 0 0 0 0 0 0 >> CFI_type_kind_shift
  125. -------------------------
  126. CFI_type_kind = 0 0 0 0 0 0 0 0 1 0 0 0
  127. */
  128. #define CFI_type_mask 0xFF
  129. #define CFI_type_kind_shift 8
  130. /* Intrinsic types. Their kind number defines their storage size. */
  131. #define CFI_type_Integer 1
  132. #define CFI_type_Logical 2
  133. #define CFI_type_Real 3
  134. #define CFI_type_Complex 4
  135. #define CFI_type_Character 5
  136. /* Types with no kind. */
  137. #define CFI_type_struct 6
  138. #define CFI_type_cptr 7
  139. #define CFI_type_cfunptr 8
  140. #define CFI_type_other -1
  141. /* Types with kind parameter.
  142. The kind parameter represents the type's byte size. The exception is
  143. real kind = 10, which has byte size of 128 bits but 80 bit precision.
  144. Complex variables are double the byte size of their real counterparts.
  145. The ucs4_char matches wchar_t if sizeof (wchar_t) == 4.
  146. */
  147. #define CFI_type_char (CFI_type_Character + (1 << CFI_type_kind_shift))
  148. #define CFI_type_ucs4_char (CFI_type_Character + (4 << CFI_type_kind_shift))
  149. /* C-Fortran Interoperability types. */
  150. #define CFI_type_signed_char (CFI_type_Integer + (sizeof (char) << CFI_type_kind_shift))
  151. #define CFI_type_short (CFI_type_Integer + (sizeof (short) << CFI_type_kind_shift))
  152. #define CFI_type_int (CFI_type_Integer + (sizeof (int) << CFI_type_kind_shift))
  153. #define CFI_type_long (CFI_type_Integer + (sizeof (long) << CFI_type_kind_shift))
  154. #define CFI_type_long_long (CFI_type_Integer + (sizeof (long long) << CFI_type_kind_shift))
  155. #define CFI_type_size_t (CFI_type_Integer + (sizeof (size_t) << CFI_type_kind_shift))
  156. #define CFI_type_int8_t (CFI_type_Integer + (sizeof (int8_t) << CFI_type_kind_shift))
  157. #define CFI_type_int16_t (CFI_type_Integer + (sizeof (int16_t) << CFI_type_kind_shift))
  158. #define CFI_type_int32_t (CFI_type_Integer + (sizeof (int32_t) << CFI_type_kind_shift))
  159. #define CFI_type_int64_t (CFI_type_Integer + (sizeof (int64_t) << CFI_type_kind_shift))
  160. #define CFI_type_int_least8_t (CFI_type_Integer + (sizeof (int_least8_t) << CFI_type_kind_shift))
  161. #define CFI_type_int_least16_t (CFI_type_Integer + (sizeof (int_least16_t) << CFI_type_kind_shift))
  162. #define CFI_type_int_least32_t (CFI_type_Integer + (sizeof (int_least32_t) << CFI_type_kind_shift))
  163. #define CFI_type_int_least64_t (CFI_type_Integer + (sizeof (int_least64_t) << CFI_type_kind_shift))
  164. #define CFI_type_int_fast8_t (CFI_type_Integer + (sizeof (int_fast8_t) << CFI_type_kind_shift))
  165. #define CFI_type_int_fast16_t (CFI_type_Integer + (sizeof (int_fast16_t) << CFI_type_kind_shift))
  166. #define CFI_type_int_fast32_t (CFI_type_Integer + (sizeof (int_fast32_t) << CFI_type_kind_shift))
  167. #define CFI_type_int_fast64_t (CFI_type_Integer + (sizeof (int_fast64_t) << CFI_type_kind_shift))
  168. #define CFI_type_intmax_t (CFI_type_Integer + (sizeof (intmax_t) << CFI_type_kind_shift))
  169. #define CFI_type_intptr_t (CFI_type_Integer + (sizeof (intptr_t) << CFI_type_kind_shift))
  170. #define CFI_type_ptrdiff_t (CFI_type_Integer + (sizeof (ptrdiff_t) << CFI_type_kind_shift))
  171. #define CFI_type_Bool (CFI_type_Logical + (sizeof (_Bool) << CFI_type_kind_shift))
  172. #define CFI_type_float (CFI_type_Real + (sizeof (float) << CFI_type_kind_shift))
  173. #define CFI_type_double (CFI_type_Real + (sizeof (double) << CFI_type_kind_shift))
  174. #define CFI_type_float_Complex (CFI_type_Complex + (sizeof (float) << CFI_type_kind_shift))
  175. #define CFI_type_double_Complex (CFI_type_Complex + (sizeof (double) << CFI_type_kind_shift))
  176. /* If GCC supports int128_t on this target, it predefines
  177. __SIZEOF_INT128__ to 16. */
  178. #if defined(__SIZEOF_INT128__)
  179. #if (__SIZEOF_INT128__ == 16)
  180. #define CFI_type_int128_t (CFI_type_Integer + (16 << CFI_type_kind_shift))
  181. #define CFI_type_int_least128_t (CFI_type_Integer + (16 << CFI_type_kind_shift))
  182. #define CFI_type_int_fast128_t (CFI_type_Integer + (16 << CFI_type_kind_shift))
  183. #else
  184. #error "Can't determine kind of int128_t"
  185. #endif
  186. #else
  187. #define CFI_type_int128_t -2
  188. #define CFI_type_int_least128_t -2
  189. #define CFI_type_int_fast128_t -2
  190. #endif
  191. /* The situation with long double support is more complicated; we need to
  192. examine the type in more detail to figure out its kind.
  193. GCC and some other compilers predefine the __LDBL* macros; otherwise
  194. get the parameters we need from float.h. */
  195. #if (defined (__LDBL_MANT_DIG__) \
  196. && defined (__LDBL_MIN_EXP__) \
  197. && defined (__LDBL_MAX_EXP__) \
  198. && defined (__DBL_MANT_DIG__) \
  199. && defined (__DBL_MIN_EXP__) \
  200. && defined (__DBL_MAX_EXP__))
  201. #define __CFI_LDBL_MANT_DIG__ __LDBL_MANT_DIG__
  202. #define __CFI_LDBL_MIN_EXP__ __LDBL_MIN_EXP__
  203. #define __CFI_LDBL_MAX_EXP__ __LDBL_MAX_EXP__
  204. #define __CFI_DBL_MANT_DIG__ __DBL_MANT_DIG__
  205. #define __CFI_DBL_MIN_EXP__ __DBL_MIN_EXP__
  206. #define __CFI_DBL_MAX_EXP__ __DBL_MAX_EXP__
  207. #else
  208. #include <float.h>
  209. #if (defined (LDBL_MANT_DIG) \
  210. && defined (LDBL_MIN_EXP) \
  211. && defined (LDBL_MAX_EXP) \
  212. && defined (DBL_MANT_DIG) \
  213. && defined (DBL_MIN_EXP) \
  214. && defined (DBL_MAX_EXP))
  215. #define __CFI_LDBL_MANT_DIG__ LDBL_MANT_DIG
  216. #define __CFI_LDBL_MIN_EXP__ LDBL_MIN_EXP
  217. #define __CFI_LDBL_MAX_EXP__ LDBL_MAX_EXP
  218. #define __CFI_DBL_MANT_DIG__ DBL_MANT_DIG
  219. #define __CFI_DBL_MIN_EXP__ DBL_MIN_EXP
  220. #define __CFI_DBL_MAX_EXP__ DBL_MAX_EXP
  221. #else
  222. #define CFI_no_long_double 1
  223. #endif /* Definitions from float.h. */
  224. #endif /* Definitions from compiler builtins. */
  225. /* Can't determine anything about long double support? */
  226. #if (defined (CFI_no_long_double))
  227. #define CFI_type_long_double -2
  228. #define CFI_type_long_double_Complex -2
  229. /* Long double is the same kind as double. */
  230. #elif (__CFI_LDBL_MANT_DIG__ == __CFI_DBL_MANT_DIG__ \
  231. && __CFI_LDBL_MIN_EXP__ == __CFI_DBL_MIN_EXP__ \
  232. && __CFI_LDBL_MAX_EXP__ == __CFI_DBL_MAX_EXP__)
  233. #define CFI_type_long_double CFI_type_double
  234. #define CFI_type_long_double_Complex CFI_type_double_Complex
  235. /* This is the 80-bit encoding on x86; Fortran assigns it kind 10. */
  236. #elif ((__CFI_LDBL_MANT_DIG__ == 64 || __CFI_LDBL_MANT_DIG__ == 53) \
  237. && __CFI_LDBL_MIN_EXP__ == -16381 \
  238. && __CFI_LDBL_MAX_EXP__ == 16384)
  239. #define CFI_type_long_double (CFI_type_Real + (10 << CFI_type_kind_shift))
  240. #define CFI_type_long_double_Complex (CFI_type_Complex + (10 << CFI_type_kind_shift))
  241. /* This is the 96-bit encoding on m68k; Fortran assigns it kind 10. */
  242. #elif (__CFI_LDBL_MANT_DIG__ == 64 \
  243. && __CFI_LDBL_MIN_EXP__ == -16382 \
  244. && __CFI_LDBL_MAX_EXP__ == 16384)
  245. #define CFI_type_long_double (CFI_type_Real + (10 << CFI_type_kind_shift))
  246. #define CFI_type_long_double_Complex (CFI_type_Complex + (10 << CFI_type_kind_shift))
  247. /* This is the IEEE 128-bit encoding, same as _Float128. */
  248. #elif (__CFI_LDBL_MANT_DIG__ == 113 \
  249. && __CFI_LDBL_MIN_EXP__ == -16381 \
  250. && __CFI_LDBL_MAX_EXP__ == 16384)
  251. #define CFI_type_long_double (CFI_type_Real + (16 << CFI_type_kind_shift))
  252. #define CFI_type_long_double_Complex (CFI_type_Complex + (16 << CFI_type_kind_shift))
  253. /* This is the IBM128 encoding used on PowerPC; also assigned kind 16. */
  254. #elif (__CFI_LDBL_MANT_DIG__ == 106 \
  255. && __CFI_LDBL_MIN_EXP__ == -968 \
  256. && __CFI_LDBL_MAX_EXP__ == 1024)
  257. #define CFI_type_long_double (CFI_type_Real + (16 << CFI_type_kind_shift))
  258. #define CFI_type_long_double_Complex (CFI_type_Complex + (16 << CFI_type_kind_shift))
  259. #define CFI_no_float128 1
  260. /* It's a bug if we get here. If you've got a target that has some other
  261. long double encoding, you need add something here for Fortran to
  262. recognize it. */
  263. #else
  264. #error "Can't determine kind of long double"
  265. #endif
  266. /* Similarly for _Float128. This always refers to the IEEE encoding
  267. and not some other 128-bit representation, so if we already used
  268. kind 16 for a non-IEEE representation, this one must be unsupported
  269. in Fortran even if it's available in C. */
  270. #if (!defined (CFI_no_float128) \
  271. && defined(__FLT128_MANT_DIG__) && __FLT128_MANT_DIG__ == 113 \
  272. && defined(__FLT128_MIN_EXP__) && __FLT128_MIN_EXP__ == -16381 \
  273. && defined(__FLT128_MAX_EXP__) && __FLT128_MAX_EXP__ == 16384)
  274. #define CFI_type_float128 (CFI_type_Real + (16 << CFI_type_kind_shift))
  275. #define CFI_type_float128_Complex (CFI_type_Complex + (16 << CFI_type_kind_shift))
  276. #else
  277. #define CFI_type_float128 -2
  278. #define CFI_type_float128_Complex -2
  279. #endif
  280. #ifdef __cplusplus
  281. }
  282. #endif
  283. #endif /* ISO_FORTRAN_BINDING_H */