]>
Commit | Line | Data |
---|---|---|
a054608c | 1 | // Copyright (C) 2020-2021 Free Software Foundation, Inc. |
02e32295 MK |
2 | // |
3 | // This file is part of the GNU ISO C++ Library. This library is free | |
4 | // software; you can redistribute it and/or modify it under the | |
5 | // terms of the GNU General Public License as published by the | |
6 | // Free Software Foundation; either version 3, or (at your option) | |
7 | // any later version. | |
8 | // | |
9 | // This library is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License along | |
15 | // with this library; see the file COPYING3. If not see | |
16 | // <http://www.gnu.org/licenses/>. | |
17 | ||
18 | #include <tuple> | |
19 | #include <utility> | |
20 | #include <cstdio> | |
21 | ||
22 | template <typename T> | |
23 | struct SincosReference | |
24 | { | |
25 | T x, s, c; | |
26 | ||
27 | std::tuple<const T &, const T &, const T &> | |
28 | as_tuple() const | |
29 | { return std::tie(x, s, c); } | |
30 | }; | |
31 | ||
32 | template <typename T> | |
33 | struct Reference { | |
34 | T x, ref; | |
35 | ||
36 | std::tuple<const T &, const T &> | |
37 | as_tuple() const | |
38 | { return std::tie(x, ref); } | |
39 | }; | |
40 | ||
41 | template <typename T> | |
42 | struct Array | |
43 | { | |
44 | std::size_t size_; | |
45 | const T *data_; | |
46 | ||
47 | Array() | |
48 | : size_(0), data_(nullptr) {} | |
49 | ||
50 | Array(size_t s, const T *p) | |
51 | : size_(s), data_(p) {} | |
52 | ||
53 | const T* | |
54 | begin() const | |
55 | { return data_; } | |
56 | ||
57 | const T* | |
58 | end() const | |
59 | { return data_ + size_; } | |
60 | ||
61 | std::size_t | |
62 | size() const | |
63 | { return size_; } | |
64 | }; | |
65 | ||
66 | namespace function { | |
67 | struct sincos{ static constexpr const char *const str = "sincos"; }; | |
68 | struct atan { static constexpr const char *const str = "atan"; }; | |
69 | struct asin { static constexpr const char *const str = "asin"; }; | |
70 | struct acos { static constexpr const char *const str = "acos"; }; | |
71 | struct log { static constexpr const char *const str = "ln"; }; | |
72 | struct log2 { static constexpr const char *const str = "log2"; }; | |
73 | struct log10 { static constexpr const char *const str = "log10"; }; | |
74 | } | |
75 | ||
76 | template <class F> | |
77 | struct testdatatype_for_function | |
78 | { | |
79 | template <class T> | |
80 | using type = Reference<T>; | |
81 | }; | |
82 | ||
83 | template <> | |
84 | struct testdatatype_for_function<function::sincos> | |
85 | { | |
86 | template <class T> | |
87 | using type = SincosReference<T>; | |
88 | }; | |
89 | ||
90 | template <class F, class T> | |
91 | using testdatatype_for_function_t | |
92 | = typename testdatatype_for_function<F>::template type<T>; | |
93 | ||
94 | template<typename T> | |
95 | struct StaticDeleter | |
96 | { | |
97 | const T *ptr; | |
98 | ||
99 | StaticDeleter(const T *p) | |
100 | : ptr(p) {} | |
101 | ||
102 | ~StaticDeleter() | |
103 | { delete[] ptr; } | |
104 | }; | |
105 | ||
106 | template <class F, class T> | |
107 | inline std::string filename() | |
108 | { | |
109 | static_assert(std::is_floating_point<T>::value, ""); | |
110 | static const auto cache | |
111 | = std::string("reference-") + F::str | |
112 | + (sizeof(T) == 4 && std::__digits_v<T> == 24 | |
113 | && std::__max_exponent_v<T> == 128 | |
114 | ? "-sp" | |
115 | : (sizeof(T) == 8 | |
116 | && std::__digits_v<T> == 53 | |
117 | && std::__max_exponent_v<T> == 1024 | |
118 | ? "-dp" | |
119 | : (sizeof(T) == 16 && std::__digits_v<T> == 64 | |
120 | && std::__max_exponent_v<T> == 16384 | |
121 | ? "-ep" | |
122 | : (sizeof(T) == 16 && std::__digits_v<T> == 113 | |
123 | && std::__max_exponent_v<T> == 16384 | |
124 | ? "-qp" | |
125 | : "-unknown")))) | |
126 | + ".dat"; | |
127 | return cache; | |
128 | } | |
129 | ||
130 | template <class Fun, class T, class Ref = testdatatype_for_function_t<Fun, T>> | |
131 | Array<Ref> | |
132 | referenceData() | |
133 | { | |
134 | static Array<Ref> data; | |
135 | if (data.data_ == nullptr) | |
136 | { | |
137 | FILE* file = std::fopen(filename<Fun, T>().c_str(), "rb"); | |
138 | if (file) | |
139 | { | |
140 | std::fseek(file, 0, SEEK_END); | |
141 | const size_t size = std::ftell(file) / sizeof(Ref); | |
142 | std::rewind(file); | |
143 | auto mem = new Ref[size]; | |
144 | static StaticDeleter<Ref> _cleanup(data.data_); | |
145 | data.size_ = std::fread(mem, sizeof(Ref), size, file); | |
146 | data.data_ = mem; | |
147 | std::fclose(file); | |
148 | } | |
149 | else | |
150 | { | |
151 | __builtin_fprintf( | |
152 | stderr, | |
153 | "%s:%d: the reference data %s does not exist in the current " | |
154 | "working directory.\n", | |
155 | __FILE__, __LINE__, filename<Fun, T>().c_str()); | |
156 | __builtin_abort(); | |
157 | } | |
158 | } | |
159 | return data; | |
160 | } |