Debian Codegraph @

himbaechel/uarch/gatemate/gatemate_util.h

raw
1 /*
2 * nextpnr -- Next Generation Place and Route
3 *
4 * Copyright (C) 2024 The Project Peppercorn Authors.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 */
19
20 #ifndef GATEMATE_UTIL_H
21 #define GATEMATE_UTIL_H
22
23 #include <map>
24 #include <set>
25 #include <string>
26 #include "nextpnr.h"
27
28 #include "log.h"
29
30 NEXTPNR_NAMESPACE_BEGIN
31
32 template <typename KeyType>
33 double double_or_default(const dict<KeyType, Property> &ct, const KeyType &key, double def = 0)
34 {
35 auto found = ct.find(key);
36 if (found == ct.end())
37 return def;
38 else {
39 if (found->second.is_string) {
40 try {
41 return std::stod(found->second.as_string());
42 } catch (std::invalid_argument &e) {
43 log_error("Expecting numeric value but got '%s'.\n", found->second.as_string().c_str());
44 }
45 } else
46 return double(found->second.as_int64());
47 }
48 };
49
50 template <typename KeyType>
51 int extract_bits(const dict<KeyType, Property> &ct, const KeyType &key, int start, int bits, int def = 0)
52 {
53 Property extr = get_or_default(ct, key, Property()).extract(start, bits);
54
55 if (extr.is_string) {
56 try {
57 return std::stoi(extr.as_string());
58 } catch (std::invalid_argument &e) {
59 log_error("Expecting numeric value but got '%s'.\n", extr.as_string().c_str());
60 }
61 } else
62 return extr.as_int64();
63 }
64
65 template <typename T>
66 std::vector<std::vector<T>> splitNestedVector(const std::vector<std::vector<T>> &input, size_t maxSize = 64)
67 {
68 std::vector<std::vector<T>> result;
69
70 for (const auto &inner : input) {
71 size_t i = 0;
72 while (i < inner.size()) {
73 size_t end = std::min(i + maxSize, inner.size());
74 result.emplace_back(inner.begin() + i, inner.begin() + end);
75 i = end;
76 if (i < inner.size())
77 log_warning("Carry chain has been split, expect timing penalty.\n");
78 }
79 }
80
81 return result;
82 }
83
84 NEXTPNR_NAMESPACE_END
85
86 #endif