2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
4 * SPDX-License-Identifier: BSD-3-Clause
7 #include "pico/divider.h"
9 // These functions save/restore divider state, so are safe to call from interrupts
10 int32_t div_s32s32(int32_t a
, int32_t b
) {
11 return hw_divider_quotient_s32(a
, b
);
14 divmod_result_t
divmod_s32s32(int32_t a
, int32_t b
) {
15 return hw_divider_divmod_s32(a
, b
);
18 uint32_t div_u32u32(uint32_t a
, uint32_t b
) {
19 return hw_divider_u32_quotient(a
, b
);
22 divmod_result_t
divmod_u32u32(uint32_t a
, uint32_t b
) {
23 return hw_divider_divmod_u32(a
, b
);
26 static inline int __sign_of_64(int32_t v
) {
27 return v
> 0 ? 1 : (v
< 0 ? -1 : 0);
40 // divides unsigned values a by b... (a/b) returned in low 32 bits, (a%b) in high 32 bits... results undefined for b==0
41 static inline qr_u64
udiv64(uint64_t a
, uint64_t b
) {
44 rc
.quotient
= (uint64_t)-1; // todo check this
53 // divides signed values a by b... (a/b) returned in low 32 bits, (a%b) in high 32 bits... results undefined for b==0
54 static inline qr_s64
div64(int64_t a
, int64_t b
) {
57 rc
.quotient
= (uint64_t)(-__sign_of_64(a
));
66 int64_t div_s64s64(int64_t a
, int64_t b
) {
67 qr_s64 qr
= div64(a
, b
);
71 int64_t divmod_s64s64_rem(int64_t a
, int64_t b
, int64_t *rem
) {
72 qr_s64 qr
= div64(a
, b
);
77 int64_t divmod_s64s64(int64_t a
, int64_t b
) {
78 qr_s64 qr
= div64(a
, b
);
82 uint64_t div_u64u64(uint64_t a
, uint64_t b
) {
83 qr_u64 qr
= udiv64(a
, b
);
87 uint64_t divmod_u64u64_rem(uint64_t a
, uint64_t b
, uint64_t *rem
) {
88 qr_u64 qr
= udiv64(a
, b
);
93 uint64_t divmod_u64u64(uint64_t a
, uint64_t b
) {
94 qr_u64 qr
= udiv64(a
, b
);
98 // these functions are slightly faster, but unsafe the divider state, so are not generally safe to be called from interrupts
100 int32_t div_s32s32_unsafe(int32_t a
, int32_t b
) { return div_s32s32(a
,b
); }
101 int32_t divmod_s32s32_rem_unsafe(int32_t a
, int32_t b
, int32_t *rem
) { return divmod_s32s32_rem(a
, b
, rem
); }
102 divmod_result_t
divmod_s32s32_unsafe(int32_t a
, int32_t b
) { return divmod_s32s32(a
, b
); }
104 uint32_t div_u32u32_unsafe(uint32_t a
, uint32_t b
) { return div_u32u32(a
, b
); }
105 uint32_t divmod_u32u32_rem_unsafe(uint32_t a
, uint32_t b
, uint32_t *rem
) { return divmod_u32u32_rem(a
, b
, rem
); }
106 uint64_t divmod_u32u32_unsafe(uint32_t a
, uint32_t b
) { return divmod_u32u32(a
, b
); }
108 int64_t div_s64s64_unsafe(int64_t a
, int64_t b
) { return div_s64s64(a
, b
); }
109 int64_t divmod_s64s64_rem_unsafe(int64_t a
, int64_t b
, int64_t *rem
) { return divmod_s64s64_rem(a
, b
, rem
); }
110 int64_t divmod_s64s64_unsafe(int64_t a
, int64_t b
) { return divmod_s64s64(a
, b
); }
112 uint64_t div_u64u64_unsafe(uint64_t a
, uint64_t b
) { return div_u64u64(a
, b
); }
113 uint64_t divmod_u64u64_rem_unsafe(uint64_t a
, uint64_t b
, uint64_t *rem
) { return divmod_u64u64_rem(a
, b
, rem
); }
114 uint64_t divmod_u64u64_unsafe(uint64_t a
, uint64_t b
) { return divmod_u64u64(a
, b
); }