Skip to content

Commit 3604755

Browse files
committed
snapshot of cl/603397699
1 parent 0a7cab1 commit 3604755

9 files changed

+136
-1
lines changed

clif/pybind11/type_casters.h

+32
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,38 @@ struct type_caster<absl::uint128> {
6868
}
6969
};
7070

71+
#ifdef ABSL_HAVE_INTRINSIC_INT128
72+
template <>
73+
struct type_caster<__int128> {
74+
public:
75+
PYBIND11_TYPE_CASTER(__int128, _("int128"));
76+
bool load(handle src, bool convert) {
77+
using ::clif::Clif_PyObjAs;
78+
return Clif_PyObjAs(src.ptr(), &value);
79+
}
80+
81+
static handle cast(__int128 src, return_value_policy, handle) {
82+
using ::clif::Clif_PyObjFrom;
83+
return Clif_PyObjFrom(src, {});
84+
}
85+
};
86+
87+
template <>
88+
struct type_caster<unsigned __int128> {
89+
public:
90+
PYBIND11_TYPE_CASTER(unsigned __int128, _("uint128"));
91+
bool load(handle src, bool convert) {
92+
using ::clif::Clif_PyObjAs;
93+
return Clif_PyObjAs(src.ptr(), &value);
94+
}
95+
96+
static handle cast(unsigned __int128 src, return_value_policy, handle) {
97+
using ::clif::Clif_PyObjFrom;
98+
return Clif_PyObjFrom(src, {});
99+
}
100+
};
101+
#endif
102+
71103
template <typename ValueAndHolder, typename Value>
72104
class smart_pointer_vector_caster {
73105
using value_conv = make_caster<Value>;

clif/python/types.cc

+18
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,24 @@ bool Clif_PyObjAs(PyObject* py, absl::uint128* c) { // NOLINT: runtime/int
272272
}
273273
#endif // HAVE_LONG_LONG
274274

275+
#ifdef ABSL_HAVE_INTRINSIC_INT128
276+
bool Clif_PyObjAs(PyObject* py, __int128* c) { // NOLINT: runtime/int
277+
CHECK(c != nullptr);
278+
absl::int128 tmp;
279+
if (!Clif_PyObjAs(py, &tmp)) return false;
280+
*c = __int128{tmp};
281+
return true;
282+
}
283+
284+
bool Clif_PyObjAs(PyObject* py, unsigned __int128* c) { // NOLINT: runtime/int
285+
CHECK(c != nullptr);
286+
absl::uint128 tmp;
287+
if (!Clif_PyObjAs(py, &tmp)) return false;
288+
*c = (unsigned __int128){tmp};
289+
return true;
290+
}
291+
#endif // ABSL_HAVE_INTRINSIC_INT128
292+
275293
// float (double)
276294
bool Clif_PyObjAs(PyObject* py, double* c) {
277295
CHECK(c != nullptr);

clif/python/types.h

+17
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,19 @@ inline PyObject* Clif_PyObjFrom(absl::uint128 c, const py::PostConv& pc) {
129129
return pc.Apply(PyNumber_Add(hi, lo));
130130
}
131131
#endif
132+
133+
#ifdef ABSL_HAVE_INTRINSIC_INT128
134+
// CLIF use2 `__int128` as int
135+
inline PyObject* Clif_PyObjFrom(__int128 c, const py::PostConv& pc) {
136+
return Clif_PyObjFrom(absl::int128{c}, pc);
137+
}
138+
139+
// CLIF use2 `unsigned __int128` as int
140+
inline PyObject* Clif_PyObjFrom(unsigned __int128 c, const py::PostConv& pc) {
141+
return Clif_PyObjFrom(absl::uint128{c}, pc);
142+
}
143+
#endif
144+
132145
// CLIF use `unsigned char` as uint8
133146
inline PyObject* Clif_PyObjFrom(unsigned char c, const py::PostConv& pc) {
134147
return pc.Apply(PyLong_FromLong(c));
@@ -188,6 +201,10 @@ bool Clif_PyObjAs(PyObject*, long long*); // NOLINT runtime/int
188201
bool Clif_PyObjAs(PyObject*, absl::int128*); // NOLINT runtime/int
189202
bool Clif_PyObjAs(PyObject*, absl::uint128*); // NOLINT runtime/int
190203
#endif
204+
#ifdef ABSL_HAVE_INTRINSIC_INT128
205+
bool Clif_PyObjAs(PyObject*, __int128*);
206+
bool Clif_PyObjAs(PyObject*, unsigned __int128*);
207+
#endif
191208
bool Clif_PyObjAs(PyObject*, short*); // NOLINT runtime/int
192209
bool Clif_PyObjAs(PyObject*, int*);
193210
bool Clif_PyObjAs(PyObject*, long*); // NOLINT runtime/int // Py_ssize_t on x64

clif/testing/absl_int128.h

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
#ifndef CLIF_TESTING_ABSL_INT128_H_
22
#define CLIF_TESTING_ABSL_INT128_H_
33

4-
#include <cstdint>
4+
#include <limits>
55

6+
#include "absl/base/config.h"
67
#include "absl/numeric/int128.h"
78

89
namespace clif_test_absl_int128 {
@@ -16,6 +17,22 @@ inline absl::int128 Min() { return std::numeric_limits<absl::int128>::min(); }
1617
inline absl::int128 Max() { return std::numeric_limits<absl::int128>::max(); }
1718
inline absl::int128 AddInt128(absl::int128 a, absl::int128 b) { return a + b; }
1819

20+
#ifdef ABSL_HAVE_INTRINSIC_INT128
21+
22+
inline constexpr bool kHasIntrinsicInt128 = true;
23+
24+
inline __int128 FromAbsl(absl::int128 value) { return __int128{value}; }
25+
inline absl::int128 ToAbsl(__int128 value) { return absl::int128{value}; }
26+
27+
#else
28+
29+
inline constexpr bool kHasIntrinsicInt128 = false;
30+
31+
inline absl::int128 FromAbsl(absl::int128 /*value*/) { return 0; }
32+
inline absl::int128 ToAbsl(absl::int128 /*value*/) { return 0; }
33+
34+
#endif
35+
1936
} // namespace clif_test_absl_int128
2037

2138
#endif // CLIF_TESTING_ABSL_INT128_H_

clif/testing/absl_uint128.h

+21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <cstdint>
55

6+
#include "absl/base/config.h"
67
#include "absl/numeric/int128.h"
78

89
namespace clif_test_absl_uint128 {
@@ -17,6 +18,26 @@ inline absl::uint128 AddUint128(absl::uint128 a, absl::uint128 b) {
1718
return a + b;
1819
}
1920

21+
#ifdef ABSL_HAVE_INTRINSIC_INT128
22+
23+
inline constexpr bool kHasIntrinsicUint128 = true;
24+
25+
inline unsigned __int128 FromAbsl(absl::uint128 value) {
26+
return (unsigned __int128){value};
27+
}
28+
inline absl::uint128 ToAbsl(unsigned __int128 value) {
29+
return absl::uint128{value};
30+
}
31+
32+
#else
33+
34+
inline constexpr bool kHasIntrinsicUint128 = false;
35+
36+
inline absl::uint128 FromAbsl(absl::uint128 /*value*/) { return 0; }
37+
inline absl::uint128 ToAbsl(absl::uint128 /*value*/) { return 0; }
38+
39+
#endif
40+
2041
} // namespace clif_test_absl_uint128
2142

2243
#endif // CLIF_TESTING_ABSL_UINT128_H_

clif/testing/python/absl_int128.clif

+3
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ from "clif/testing/absl_int128.h":
88
def Min() -> int
99
def Max() -> int
1010
def AddInt128(a: int, b: int) -> int
11+
const `kHasIntrinsicInt128` as HAS_INTRINSIC_INT_128: bool
12+
def FromAbsl(value: int) -> int
13+
def ToAbsl(value: int) -> int

clif/testing/python/absl_int128_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ def testAddNegativeInt128Long(self):
4848
def testAddInt128NegativeLong(self):
4949
self.assertEqual(absl_int128.AddInt128(HIGH_LSB, -MAX64), 1)
5050

51+
def testIntrinsicFromAbsl(self):
52+
if absl_int128.HAS_INTRINSIC_INT_128:
53+
self.assertEqual(absl_int128.FromAbsl(123), 123)
54+
else:
55+
self.assertEqual(absl_int128.FromAbsl(123), 0) # Dummy implementation.
56+
57+
def testIntrinsicToAbsl(self):
58+
if absl_int128.HAS_INTRINSIC_INT_128:
59+
self.assertEqual(absl_int128.ToAbsl(123), 123)
60+
else:
61+
self.assertEqual(absl_int128.ToAbsl(123), 0) # Dummy implementation.
62+
5163

5264
if __name__ == '__main__':
5365
absltest.main()

clif/testing/python/absl_uint128.clif

+3
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ from "clif/testing/absl_uint128.h":
55
def SetHighLsb() -> int
66
def Max() -> int
77
def AddUint128(a: int, b: int) -> int
8+
const `kHasIntrinsicUint128` as HAS_INTRINSIC_UINT_128: bool
9+
def FromAbsl(value: int) -> int
10+
def ToAbsl(value: int) -> int

clif/testing/python/absl_uint128_test.py

+12
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,18 @@ def testAddUint128Int(self):
2929
def testAddUint128Long(self):
3030
self.assertEqual(absl_uint128.AddUint128(MAX64, 1), HIGH_LSB)
3131

32+
def testIntrinsicFromAbsl(self):
33+
if absl_uint128.HAS_INTRINSIC_UINT_128:
34+
self.assertEqual(absl_uint128.FromAbsl(123), 123)
35+
else:
36+
self.assertEqual(absl_uint128.FromAbsl(123), 0) # Dummy implementation.
37+
38+
def testIntrinsicToAbsl(self):
39+
if absl_uint128.HAS_INTRINSIC_UINT_128:
40+
self.assertEqual(absl_uint128.ToAbsl(123), 123)
41+
else:
42+
self.assertEqual(absl_uint128.ToAbsl(123), 0) # Dummy implementation.
43+
3244

3345
if __name__ == '__main__':
3446
absltest.main()

0 commit comments

Comments
 (0)