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
16namespace Fortran::common {
17
18static constexpr int maxKind{16};
19
20// A predicate that is true when a kind value is a kind that could possibly
21// be supported for an intrinsic type category on some target instruction
22// set architecture.
23static constexpr bool IsValidKindOfIntrinsicType(
24 TypeCategory category, std::int64_t kind) {
25 switch (category) {
26 case TypeCategory::Integer:
27 case TypeCategory::Unsigned:
28 return kind == 1 || kind == 2 || kind == 4 || kind == 8 || kind == 16;
29 case TypeCategory::Real:
30 case TypeCategory::Complex:
31 return kind == 2 || kind == 3 || kind == 4 || kind == 8 || kind == 10 ||
32 kind == 16;
33 case TypeCategory::Character:
34 return kind == 1 || kind == 2 || kind == 4;
35 case TypeCategory::Logical:
36 return kind == 1 || kind == 2 || kind == 4 || kind == 8;
37 default:
38 return false;
39 }
40}
41
42static constexpr int TypeSizeInBytes(TypeCategory category, std::int64_t kind) {
43 if (IsValidKindOfIntrinsicType(category, kind)) {
44 if (category == TypeCategory::Real || category == TypeCategory::Complex) {
45 int precision{PrecisionOfRealKind(kind)};
46 int bits{BitsForBinaryPrecision(precision)};
47 if (bits == 80) { // x87 is stored in 16-byte containers
48 bits = 128;
49 }
50 if (category == TypeCategory::Complex) {
51 bits *= 2;
52 }
53 return bits >> 3;
54 } else {
55 return kind;
56 }
57 } else {
58 return -1;
59 }
60}
61
62} // namespace Fortran::common
63#endif // FORTRAN_COMMON_TYPE_KINDS_H_
Definition bit-population-count.h:20