Skip to content

Commit be9c8f5

Browse files
committed
Fix GCC specific implementation
Fix optimized addc version Fix optimized subb version
1 parent 12fe933 commit be9c8f5

File tree

1 file changed

+15
-57
lines changed

1 file changed

+15
-57
lines changed

include/slimcpplib/long_math_gcc.h

Lines changed: 15 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,11 @@ uint_t nlz(uint64_t value) noexcept;
5353

5454
// add with carry
5555

56-
uint8_t addc(uint8_t value1, uint8_t value2, bool& carry) noexcept;
57-
uint16_t addc(uint16_t value1, uint16_t value2, bool& carry) noexcept;
5856
uint32_t addc(uint32_t value1, uint32_t value2, bool& carry) noexcept;
5957
uint64_t addc(uint64_t value1, uint64_t value2, bool& carry) noexcept;
6058

6159
// subtract with borrow
6260

63-
uint8_t subb(uint8_t value1, uint8_t value2, bool& borrow) noexcept;
64-
uint16_t subb(uint16_t value1, uint16_t value2, bool& borrow) noexcept;
6561
uint32_t subb(uint32_t value1, uint32_t value2, bool& borrow) noexcept;
6662
uint64_t subb(uint64_t value1, uint64_t value2, bool& borrow) noexcept;
6763

@@ -117,33 +113,11 @@ inline uint_t nlz(uint64_t value) noexcept
117113

118114

119115

120-
////////////////////////////////////////////////////////////////////////////////////////////////////
121-
inline uint8_t addc(uint8_t value1, uint8_t value2, bool& carry) noexcept
122-
{
123-
uint8_t result;
124-
carry = __builtin_add_overflow(value1, value2, &result);
125-
126-
return result;
127-
}
128-
129-
130-
131-
////////////////////////////////////////////////////////////////////////////////////////////////////
132-
inline uint16_t addc(uint16_t value1, uint16_t value2, bool& carry) noexcept
133-
{
134-
uint16_t result;
135-
carry = __builtin_add_overflow(value1, value2, &result);
136-
137-
return result;
138-
}
139-
140-
141-
142116
////////////////////////////////////////////////////////////////////////////////////////////////////
143117
inline uint32_t addc(uint32_t value1, uint32_t value2, bool& carry) noexcept
144118
{
145119
uint32_t result;
146-
carry = __builtin_add_overflow(value1, value2, &result);
120+
carry = __builtin_ia32_addcarryx_u32(static_cast<uint8_t>(carry), value1, value2, &result);
147121

148122
return result;
149123
}
@@ -154,8 +128,8 @@ inline uint32_t addc(uint32_t value1, uint32_t value2, bool& carry) noexcept
154128
inline uint64_t addc(uint64_t value1, uint64_t value2, bool& carry) noexcept
155129
{
156130
#ifdef __x86_64__
157-
uint64_t result;
158-
carry = __builtin_add_overflow(value1, value2, &result);
131+
unsigned long long result;
132+
carry = __builtin_ia32_addcarryx_u64(static_cast<uint8_t>(carry), value1, value2, &result);
159133

160134
return result;
161135
#else
@@ -186,34 +160,15 @@ inline void add(std::array<type_t, size>& value1, const std::array<type_t, size>
186160

187161

188162

189-
////////////////////////////////////////////////////////////////////////////////////////////////////
190-
inline uint8_t subb(uint8_t value1, uint8_t value2, bool& borrow) noexcept
191-
{
192-
uint8_t result;
193-
borrow = __builtin_sub_overflow(value1, value2, &result);
194-
195-
return result;
196-
}
197-
198-
199-
200-
////////////////////////////////////////////////////////////////////////////////////////////////////
201-
inline uint16_t subb(uint16_t value1, uint16_t value2, bool& borrow) noexcept
202-
{
203-
uint16_t result;
204-
borrow = __builtin_sub_overflow(value1, value2, &result);
205-
206-
return result;
207-
}
208-
209-
210-
211163
////////////////////////////////////////////////////////////////////////////////////////////////////
212164
inline uint32_t subb(uint32_t value1, uint32_t value2, bool& borrow) noexcept
213165
{
214166
uint32_t result;
215-
borrow = __builtin_sub_overflow(value1, value2, &result);
216-
167+
#ifdef __clang__
168+
borrow = __builtin_ia32_subborrow_u32(static_cast<uint8_t>(borrow), value1, value2, &result);
169+
#else
170+
borrow = __builtin_ia32_sbb_u32(static_cast<uint8_t>(borrow), value1, value2, &result);
171+
#endif // __CLANG__
217172
return result;
218173
}
219174

@@ -223,9 +178,12 @@ inline uint32_t subb(uint32_t value1, uint32_t value2, bool& borrow) noexcept
223178
inline uint64_t subb(uint64_t value1, uint64_t value2, bool& borrow) noexcept
224179
{
225180
#ifdef __x86_64__
226-
uint64_t result;
227-
borrow = __builtin_sub_overflow(value1, value2, &result);
228-
181+
unsigned long long result;
182+
#ifdef __clang__
183+
borrow = __builtin_ia32_subborrow_u64(static_cast<uint8_t>(borrow), value1, value2, &result);
184+
#else
185+
borrow = __builtin_ia32_sbb_u64(static_cast<uint8_t>(borrow), value1, value2, &result);
186+
#endif // __CLANG__
229187
return result;
230188
#else
231189
return subb<uint64_t>(value1, value2, borrow);
@@ -314,4 +272,4 @@ inline uint64_t mulc(uint64_t value1, uint64_t value2, uint64_t& carry) noexcept
314272

315273
////////////////////////////////////////////////////////////////////////////////////////////////////
316274
// End of long_math_gcc.h
317-
////////////////////////////////////////////////////////////////////////////////////////////////////
275+
////////////////////////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)