xref: /aosp_15_r20/external/tensorflow/tensorflow/core/platform/denormal.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_PLATFORM_DENORMAL_H_
17 #define TENSORFLOW_CORE_PLATFORM_DENORMAL_H_
18 
19 #include "tensorflow/core/platform/macros.h"
20 
21 namespace tensorflow {
22 namespace port {
23 
24 // State for handling of denormals.
25 class DenormalState {
26  public:
DenormalState(bool flush_to_zero,bool denormals_are_zero)27   DenormalState(bool flush_to_zero, bool denormals_are_zero)
28       : flush_to_zero_(flush_to_zero),
29         denormals_are_zero_(denormals_are_zero) {}
30 
31   // Output denormals of floating-point operations are flushed to zero.
flush_to_zero()32   inline bool flush_to_zero() const { return flush_to_zero_; }
33 
34   // Input denormals to floating-point operations are treated as zero.
denormals_are_zero()35   inline bool denormals_are_zero() const { return denormals_are_zero_; }
36 
37   bool operator==(const DenormalState& other) const;
38   bool operator!=(const DenormalState& other) const;
39 
40  private:
41   bool flush_to_zero_;
42   bool denormals_are_zero_;
43 };
44 
45 // Gets the platform's current state for handling denormals.
46 DenormalState GetDenormalState();
47 
48 // Sets handling of denormals if the platform allows it. Returns `true` if the
49 // platform supports setting denormals to the specified state. Otherwise the
50 // denormal state remains unmodified and false is returned.
51 bool SetDenormalState(const DenormalState& state);
52 
53 // Remembers the flush denormal state on construction and restores that same
54 // state on destruction.
55 class ScopedRestoreFlushDenormalState {
56  public:
57   ScopedRestoreFlushDenormalState();
58   ~ScopedRestoreFlushDenormalState();
59 
60  private:
61   DenormalState denormal_state_;
62   TF_DISALLOW_COPY_AND_ASSIGN(ScopedRestoreFlushDenormalState);
63 };
64 
65 // While this class is active, denormal floating point numbers are flushed
66 // to zero.  The destructor restores the original flags.
67 class ScopedFlushDenormal {
68  public:
69   ScopedFlushDenormal();
70 
71  private:
72   ScopedRestoreFlushDenormalState restore_;
73   TF_DISALLOW_COPY_AND_ASSIGN(ScopedFlushDenormal);
74 };
75 
76 // While this class is active, denormal floating point numbers are not flushed
77 // to zero.  The destructor restores the original flags.
78 class ScopedDontFlushDenormal {
79  public:
80   ScopedDontFlushDenormal();
81 
82  private:
83   ScopedRestoreFlushDenormalState restore_;
84   TF_DISALLOW_COPY_AND_ASSIGN(ScopedDontFlushDenormal);
85 };
86 
87 }  // namespace port
88 }  // namespace tensorflow
89 
90 #endif  // TENSORFLOW_CORE_PLATFORM_DENORMAL_H_
91