1/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#ifndef TENSORFLOW_TSL_PLATFORM_DENORMAL_H_
17#define TENSORFLOW_TSL_PLATFORM_DENORMAL_H_
18
19#include "tensorflow/tsl/platform/macros.h"
20
21namespace tsl {
22namespace port {
23
24// State for handling of denormals.
25class DenormalState {
26 public:
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.
32 inline bool flush_to_zero() const { return flush_to_zero_; }
33
34 // Input denormals to floating-point operations are treated as 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.
46DenormalState 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.
51bool SetDenormalState(const DenormalState& state);
52
53// Remembers the flush denormal state on construction and restores that same
54// state on destruction.
55class 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.
67class 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.
78class 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 tsl
89
90#endif // TENSORFLOW_TSL_PLATFORM_DENORMAL_H_
91