Coverage Report

Created: 2020-08-18 13:09

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Basic/FixedPoint.h
Line
Count
Source (jump to first uncovered line)
1
//===- FixedPoint.h - Fixed point constant handling -------------*- 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
/// \file
10
/// Defines the fixed point number interface.
11
/// This is a class for abstracting various operations performed on fixed point
12
/// types described in ISO/IEC JTC1 SC22 WG14 N1169 starting at clause 4.
13
//
14
//===----------------------------------------------------------------------===//
15
16
#ifndef LLVM_CLANG_BASIC_FIXEDPOINT_H
17
#define LLVM_CLANG_BASIC_FIXEDPOINT_H
18
19
#include "llvm/ADT/APSInt.h"
20
#include "llvm/ADT/SmallString.h"
21
#include "llvm/Support/raw_ostream.h"
22
23
namespace clang {
24
25
class ASTContext;
26
class QualType;
27
28
/// The fixed point semantics work similarly to llvm::fltSemantics. The width
29
/// specifies the whole bit width of the underlying scaled integer (with padding
30
/// if any). The scale represents the number of fractional bits in this type.
31
/// When HasUnsignedPadding is true and this type is unsigned, the first bit
32
/// in the value this represents is treated as padding.
33
class FixedPointSemantics {
34
public:
35
  FixedPointSemantics(unsigned Width, unsigned Scale, bool IsSigned,
36
                      bool IsSaturated, bool HasUnsignedPadding)
37
      : Width(Width), Scale(Scale), IsSigned(IsSigned),
38
10.2k
        IsSaturated(IsSaturated), HasUnsignedPadding(HasUnsignedPadding) {
39
10.2k
    assert(Width >= Scale && "Not enough room for the scale");
40
10.2k
    assert(!(IsSigned && HasUnsignedPadding) &&
41
10.2k
           "Cannot have unsigned padding on a signed type.");
42
10.2k
  }
43
44
26.8k
  unsigned getWidth() const { return Width; }
45
18.0k
  unsigned getScale() const { return Scale; }
46
33.9k
  bool isSigned() const { return IsSigned; }
47
4.79k
  bool isSaturated() const { return IsSaturated; }
48
812
  bool hasUnsignedPadding() const { return HasUnsignedPadding; }
49
50
  void setSaturated(bool Saturated) { IsSaturated = Saturated; }
51
52
  /// Return the number of integral bits represented by these semantics. These
53
  /// are separate from the fractional bits and do not include the sign or
54
  /// padding bit.
55
5.23k
  unsigned getIntegralBits() const {
56
5.23k
    if (IsSigned || 
(1.45k
!IsSigned1.45k
&&
HasUnsignedPadding1.45k
))
57
4.35k
      return Width - Scale - 1;
58
874
    else
59
874
      return Width - Scale;
60
5.23k
  }
61
62
  /// Return the FixedPointSemantics that allows for calculating the full
63
  /// precision semantic that can precisely represent the precision and ranges
64
  /// of both input values. This does not compute the resulting semantics for a
65
  /// given binary operation.
66
  FixedPointSemantics
67
  getCommonSemantics(const FixedPointSemantics &Other) const;
68
69
  /// Return the FixedPointSemantics for an integer type.
70
  static FixedPointSemantics GetIntegerSemantics(unsigned Width,
71
1.25k
                                                 bool IsSigned) {
72
1.25k
    return FixedPointSemantics(Width, /*Scale=*/0, IsSigned,
73
1.25k
                               /*IsSaturated=*/false,
74
1.25k
                               /*HasUnsignedPadding=*/false);
75
1.25k
  }
76
77
private:
78
  unsigned Width          : 16;
79
  unsigned Scale          : 13;
80
  unsigned IsSigned       : 1;
81
  unsigned IsSaturated    : 1;
82
  unsigned HasUnsignedPadding : 1;
83
};
84
85
/// The APFixedPoint class works similarly to APInt/APSInt in that it is a
86
/// functional replacement for a scaled integer. It is meant to replicate the
87
/// fixed point types proposed in ISO/IEC JTC1 SC22 WG14 N1169. The class carries
88
/// info about the fixed point type's width, sign, scale, and saturation, and
89
/// provides different operations that would normally be performed on fixed point
90
/// types.
91
///
92
/// Semantically this does not represent any existing C type other than fixed
93
/// point types and should eventually be moved to LLVM if fixed point types gain
94
/// native IR support.
95
class APFixedPoint {
96
public:
97
  APFixedPoint(const llvm::APInt &Val, const FixedPointSemantics &Sema)
98
11.5k
      : Val(Val, !Sema.isSigned()), Sema(Sema) {
99
11.5k
    assert(Val.getBitWidth() == Sema.getWidth() &&
100
11.5k
           "The value should have a bit width that matches the Sema width");
101
11.5k
  }
102
103
  APFixedPoint(uint64_t Val, const FixedPointSemantics &Sema)
104
      : APFixedPoint(llvm::APInt(Sema.getWidth(), Val, Sema.isSigned()),
105
3.61k
                     Sema) {}
Unexecuted instantiation: clang::APFixedPoint::APFixedPoint(unsigned long long, clang::FixedPointSemantics const&)
clang::APFixedPoint::APFixedPoint(unsigned long long, clang::FixedPointSemantics const&)
Line
Count
Source
105
3.61k
                     Sema) {}
106
107
  // Zero initialization.
108
3.01k
  APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}
Unexecuted instantiation: clang::APFixedPoint::APFixedPoint(clang::FixedPointSemantics const&)
clang::APFixedPoint::APFixedPoint(clang::FixedPointSemantics const&)
Line
Count
Source
108
3.01k
  APFixedPoint(const FixedPointSemantics &Sema) : APFixedPoint(0, Sema) {}
109
110
5.56k
  llvm::APSInt getValue() const { return llvm::APSInt(Val, !Sema.isSigned()); }
111
3.26k
  inline unsigned getWidth() const { return Sema.getWidth(); }
112
9.28k
  inline unsigned getScale() const { return Sema.getScale(); }
113
274
  inline bool isSaturated() const { return Sema.isSaturated(); }
114
542
  inline bool isSigned() const { return Sema.isSigned(); }
115
0
  inline bool hasPadding() const { return Sema.hasUnsignedPadding(); }
116
1.44k
  FixedPointSemantics getSemantics() const { return Sema; }
117
118
24
  bool getBoolValue() const { return Val.getBoolValue(); }
119
120
  // Convert this number to match the semantics provided. If the overflow
121
  // parameter is provided, set this value to true or false to indicate if this
122
  // operation results in an overflow.
123
  APFixedPoint convert(const FixedPointSemantics &DstSema,
124
                       bool *Overflow = nullptr) const;
125
126
  // Perform binary operations on a fixed point type. The resulting fixed point
127
  // value will be in the common, full precision semantics that can represent
128
  // the precision and ranges of both input values. See convert() for an
129
  // explanation of the Overflow parameter.
130
  APFixedPoint add(const APFixedPoint &Other, bool *Overflow = nullptr) const;
131
  APFixedPoint sub(const APFixedPoint &Other, bool *Overflow = nullptr) const;
132
  APFixedPoint mul(const APFixedPoint &Other, bool *Overflow = nullptr) const;
133
  APFixedPoint div(const APFixedPoint &Other, bool *Overflow = nullptr) const;
134
135
  // Perform shift operations on a fixed point type. Unlike the other binary
136
  // operations, the resulting fixed point value will be in the original
137
  // semantic.
138
  APFixedPoint shl(unsigned Amt, bool *Overflow = nullptr) const;
139
40
  APFixedPoint shr(unsigned Amt, bool *Overflow = nullptr) const {
140
    // Right shift cannot overflow.
141
40
    if (Overflow)
142
40
      *Overflow = false;
143
40
    return APFixedPoint(Val >> Amt, Sema);
144
40
  }
145
146
  /// Perform a unary negation (-X) on this fixed point type, taking into
147
  /// account saturation if applicable.
148
  APFixedPoint negate(bool *Overflow = nullptr) const;
149
150
  /// Return the integral part of this fixed point number, rounded towards
151
  /// zero. (-2.5k -> -2)
152
188
  llvm::APSInt getIntPart() const {
153
188
    if (Val < 0 && 
Val != -Val34
) // Cover the case when we have the min val
154
28
      return -(-Val >> getScale());
155
160
    else
156
160
      return Val >> getScale();
157
188
  }
158
159
  /// Return the integral part of this fixed point number, rounded towards
160
  /// zero. The value is stored into an APSInt with the provided width and sign.
161
  /// If the overflow parameter is provided, and the integral value is not able
162
  /// to be fully stored in the provided width and sign, the overflow parameter
163
  /// is set to true.
164
  ///
165
  /// If the overflow parameter is provided, set this value to true or false to
166
  /// indicate if this operation results in an overflow.
167
  llvm::APSInt convertToInt(unsigned DstWidth, bool DstSign,
168
                            bool *Overflow = nullptr) const;
169
170
  void toString(llvm::SmallVectorImpl<char> &Str) const;
171
26
  std::string toString() const {
172
26
    llvm::SmallString<40> S;
173
26
    toString(S);
174
26
    return std::string(S.str());
175
26
  }
176
177
  // If LHS > RHS, return 1. If LHS == RHS, return 0. If LHS < RHS, return -1.
178
  int compare(const APFixedPoint &Other) const;
179
  bool operator==(const APFixedPoint &Other) const {
180
    return compare(Other) == 0;
181
  }
182
0
  bool operator!=(const APFixedPoint &Other) const {
183
0
    return compare(Other) != 0;
184
0
  }
185
174
  bool operator>(const APFixedPoint &Other) const { return compare(Other) > 0; }
186
220
  bool operator<(const APFixedPoint &Other) const { return compare(Other) < 0; }
187
0
  bool operator>=(const APFixedPoint &Other) const {
188
0
    return compare(Other) >= 0;
189
0
  }
190
0
  bool operator<=(const APFixedPoint &Other) const {
191
0
    return compare(Other) <= 0;
192
0
  }
193
194
  static APFixedPoint getMax(const FixedPointSemantics &Sema);
195
  static APFixedPoint getMin(const FixedPointSemantics &Sema);
196
197
  /// Create an APFixedPoint with a value equal to that of the provided integer,
198
  /// and in the same semantics as the provided target semantics. If the value
199
  /// is not able to fit in the specified fixed point semantics, and the
200
  /// overflow parameter is provided, it is set to true.
201
  static APFixedPoint getFromIntValue(const llvm::APSInt &Value,
202
                                      const FixedPointSemantics &DstFXSema,
203
                                      bool *Overflow = nullptr);
204
205
private:
206
  llvm::APSInt Val;
207
  FixedPointSemantics Sema;
208
};
209
210
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
211
0
                                     const APFixedPoint &FX) {
212
0
  OS << FX.toString();
213
0
  return OS;
214
0
}
215
216
}  // namespace clang
217
218
#endif