FLANG
type-kinds.h
1//===-- include/flang/Common/type-kinds.h -----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef FORTRAN_COMMON_TYPE_KINDS_H_
10#define FORTRAN_COMMON_TYPE_KINDS_H_
11
12#include "Fortran-consts.h"
13#include "real.h"
14#include <cinttypes>
15
16// Canonical lists of supported Fortran kinds for each intrinsic type.
17#define FORTRAN_INTEGER_KINDS {1, 2, 4, 8, 16}
18#define FORTRAN_UNSIGNED_KINDS FORTRAN_INTEGER_KINDS
19#define FORTRAN_REAL_KINDS {2, 3, 4, 8, 10, 16}
20#define FORTRAN_LOGICAL_KINDS {1, 2, 4, 8}
21#define FORTRAN_CHARACTER_KINDS {1, 2, 4}
22
23namespace Fortran::common {
24
25static constexpr int IntegerKinds[] FORTRAN_INTEGER_KINDS;
26static constexpr int UnsignedKinds[] FORTRAN_UNSIGNED_KINDS;
27static constexpr int RealKinds[] FORTRAN_REAL_KINDS;
28static constexpr int LogicalKinds[] FORTRAN_LOGICAL_KINDS;
29static constexpr int CharacterKinds[] FORTRAN_CHARACTER_KINDS;
30
31static constexpr int maxKind{16};
32
33template <typename T, std::size_t N>
34static constexpr bool IsKindInList(const T (&kinds)[N], std::int64_t kind) {
35 for (std::size_t i{0}; i < N; ++i) {
36 if (kinds[i] == kind)
37 return true;
38 }
39 return false;
40}
41
42// A predicate that is true when a kind value is a kind that could possibly
43// be supported for an intrinsic type category on some target instruction
44// set architecture.
45static constexpr bool IsValidKindOfIntrinsicType(
46 TypeCategory category, std::int64_t kind) {
47 switch (category) {
48 case TypeCategory::Integer:
49 case TypeCategory::Unsigned: {
50 constexpr int kinds[] = FORTRAN_INTEGER_KINDS;
51 return IsKindInList(kinds, kind);
52 }
53 case TypeCategory::Real:
54 case TypeCategory::Complex: {
55 constexpr int kinds[] = FORTRAN_REAL_KINDS;
56 return IsKindInList(kinds, kind);
57 }
58 case TypeCategory::Character: {
59 constexpr int kinds[] = FORTRAN_CHARACTER_KINDS;
60 return IsKindInList(kinds, kind);
61 }
62 case TypeCategory::Logical: {
63 constexpr int kinds[] = FORTRAN_LOGICAL_KINDS;
64 return IsKindInList(kinds, kind);
65 }
66 default:
67 return false;
68 }
69}
70
71static constexpr int TypeSizeInBytes(TypeCategory category, std::int64_t kind) {
72 if (IsValidKindOfIntrinsicType(category, kind)) {
73 if (category == TypeCategory::Real || category == TypeCategory::Complex) {
74 int precision{PrecisionOfRealKind(kind)};
75 int bits{BitsForBinaryPrecision(precision)};
76 if (bits == 80) { // x87 is stored in 16-byte containers
77 bits = 128;
78 }
79 if (category == TypeCategory::Complex) {
80 bits *= 2;
81 }
82 return bits >> 3;
83 } else {
84 return kind;
85 }
86 } else {
87 return -1;
88 }
89}
90
91} // namespace Fortran::common
92#endif // FORTRAN_COMMON_TYPE_KINDS_H_
Definition bit-population-count.h:20