/root/doris/contrib/faiss/faiss/impl/FaissAssert.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) Meta Platforms, Inc. and affiliates. |
3 | | * |
4 | | * This source code is licensed under the MIT license found in the |
5 | | * LICENSE file in the root directory of this source tree. |
6 | | */ |
7 | | |
8 | | // -*- c++ -*- |
9 | | |
10 | | #ifndef FAISS_ASSERT_INCLUDED |
11 | | #define FAISS_ASSERT_INCLUDED |
12 | | |
13 | | #include <faiss/impl/FaissException.h> |
14 | | #include <faiss/impl/platform_macros.h> |
15 | | #include <cstdio> |
16 | | #include <cstdlib> |
17 | | #include <string> |
18 | | |
19 | | /// |
20 | | /// Assertions |
21 | | /// |
22 | | |
23 | | #define FAISS_ASSERT(X) \ |
24 | 35.8k | do { \ |
25 | 35.8k | if (!(X)) { \ |
26 | 0 | fprintf(stderr, \ |
27 | 0 | "Faiss assertion '%s' failed in %s " \ |
28 | 0 | "at %s:%d\n", \ |
29 | 0 | #X, \ |
30 | 0 | __PRETTY_FUNCTION__, \ |
31 | 0 | __FILE__, \ |
32 | 0 | __LINE__); \ |
33 | 0 | abort(); \ |
34 | 0 | } \ |
35 | 35.8k | } while (false) |
36 | | |
37 | | #define FAISS_ASSERT_MSG(X, MSG) \ |
38 | 35.7k | do { \ |
39 | 35.7k | if (!(X)) { \ |
40 | 0 | fprintf(stderr, \ |
41 | 0 | "Faiss assertion '%s' failed in %s " \ |
42 | 0 | "at %s:%d; details: " MSG "\n", \ |
43 | 0 | #X, \ |
44 | 0 | __PRETTY_FUNCTION__, \ |
45 | 0 | __FILE__, \ |
46 | 0 | __LINE__); \ |
47 | 0 | abort(); \ |
48 | 0 | } \ |
49 | 35.7k | } while (false) |
50 | | |
51 | | #define FAISS_ASSERT_FMT(X, FMT, ...) \ |
52 | | do { \ |
53 | | if (!(X)) { \ |
54 | | fprintf(stderr, \ |
55 | | "Faiss assertion '%s' failed in %s " \ |
56 | | "at %s:%d; details: " FMT "\n", \ |
57 | | #X, \ |
58 | | __PRETTY_FUNCTION__, \ |
59 | | __FILE__, \ |
60 | | __LINE__, \ |
61 | | __VA_ARGS__); \ |
62 | | abort(); \ |
63 | | } \ |
64 | | } while (false) |
65 | | |
66 | | /// |
67 | | /// Exceptions for returning user errors |
68 | | /// |
69 | | |
70 | | #define FAISS_THROW_MSG(MSG) \ |
71 | 0 | do { \ |
72 | 0 | throw faiss::FaissException( \ |
73 | 0 | MSG, __PRETTY_FUNCTION__, __FILE__, __LINE__); \ |
74 | 0 | } while (false) |
75 | | |
76 | | #define FAISS_THROW_FMT(FMT, ...) \ |
77 | 0 | do { \ |
78 | 0 | std::string __s; \ |
79 | 0 | int __size = snprintf(nullptr, 0, FMT, __VA_ARGS__); \ |
80 | 0 | __s.resize(__size + 1); \ |
81 | 0 | snprintf(&__s[0], __s.size(), FMT, __VA_ARGS__); \ |
82 | 0 | throw faiss::FaissException( \ |
83 | 0 | __s, __PRETTY_FUNCTION__, __FILE__, __LINE__); \ |
84 | 0 | } while (false) |
85 | | |
86 | | /// |
87 | | /// Exceptions thrown upon a conditional failure |
88 | | /// |
89 | | |
90 | | #define FAISS_THROW_IF_NOT(X) \ |
91 | 64.2k | do { \ |
92 | 64.2k | if (!(X)) { \ |
93 | 0 | FAISS_THROW_FMT("Error: '%s' failed", #X); \ |
94 | 0 | } \ |
95 | 64.2k | } while (false) |
96 | | |
97 | | #define FAISS_THROW_IF_MSG(X, MSG) \ |
98 | 17.9k | do { \ |
99 | 17.9k | if (X) { \ |
100 | 0 | FAISS_THROW_FMT("Error: '%s' failed: " MSG, #X); \ |
101 | 0 | } \ |
102 | 17.9k | } while (false) |
103 | | |
104 | 17.9k | #define FAISS_THROW_IF_NOT_MSG(X, MSG) FAISS_THROW_IF_MSG(!(X), MSG) |
105 | | |
106 | | #define FAISS_THROW_IF_NOT_FMT(X, FMT, ...) \ |
107 | 1.11k | do { \ |
108 | 1.11k | if (!(X)) { \ |
109 | 0 | FAISS_THROW_FMT("Error: '%s' failed: " FMT, #X, __VA_ARGS__); \ |
110 | 0 | } \ |
111 | 1.11k | } while (false) |
112 | | |
113 | | #endif |