divider.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #ifndef _PICO_DIVIDER_H
8 #define _PICO_DIVIDER_H
9 
10 #include "pico.h"
11 #include "hardware/divider.h"
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
41 int32_t div_s32s32(int32_t a, int32_t b);
42 
52 static inline int32_t divmod_s32s32_rem(int32_t a, int32_t b, int32_t *rem) {
53  divmod_result_t r = hw_divider_divmod_s32(a, b);
54  *rem = to_remainder_s32(r);
55  return to_quotient_s32(r);
56 }
57 
66 divmod_result_t divmod_s32s32(int32_t a, int32_t b);
67 
76 uint32_t div_u32u32(uint32_t a, uint32_t b);
77 
87 static inline uint32_t divmod_u32u32_rem(uint32_t a, uint32_t b, uint32_t *rem) {
88  divmod_result_t r = hw_divider_divmod_u32(a, b);
89  *rem = to_remainder_u32(r);
90  return to_quotient_u32(r);
91 }
92 
101 divmod_result_t divmod_u32u32(uint32_t a, uint32_t b);
102 
111 int64_t div_s64s64(int64_t a, int64_t b);
112 
122 int64_t divmod_s64s64_rem(int64_t a, int64_t b, int64_t *rem);
123 
132 int64_t divmod_s64s64(int64_t a, int64_t b);
133 
142 uint64_t div_u64u64(uint64_t a, uint64_t b);
143 
153 uint64_t divmod_u64u64_rem(uint64_t a, uint64_t b, uint64_t *rem);
154 
155 
164 uint64_t divmod_u64u64(uint64_t a, uint64_t b);
165 
166 // -----------------------------------------------------------------------
167 // these "unsafe" functions are slightly faster, but do not save the divider state,
168 // so are not generally safe to be called from interrupts
169 // -----------------------------------------------------------------------
170 
181 int32_t div_s32s32_unsafe(int32_t a, int32_t b);
182 
194 int32_t divmod_s32s32_rem_unsafe(int32_t a, int32_t b, int32_t *rem);
195 
206 int64_t divmod_s32s32_unsafe(int32_t a, int32_t b);
207 
218 uint32_t div_u32u32_unsafe(uint32_t a, uint32_t b);
219 
231 uint32_t divmod_u32u32_rem_unsafe(uint32_t a, uint32_t b, uint32_t *rem);
232 
243 uint64_t divmod_u32u32_unsafe(uint32_t a, uint32_t b);
244 
255 int64_t div_s64s64_unsafe(int64_t a, int64_t b);
256 
268 int64_t divmod_s64s64_rem_unsafe(int64_t a, int64_t b, int64_t *rem);
269 
280 int64_t divmod_s64s64_unsafe(int64_t a, int64_t b);
281 
292 uint64_t div_u64u64_unsafe(uint64_t a, uint64_t b);
293 
305 uint64_t divmod_u64u64_rem_unsafe(uint64_t a, uint64_t b, uint64_t *rem);
306 
317 uint64_t divmod_u64u64_unsafe(uint64_t a, uint64_t b);
318 
319 #ifdef __cplusplus
320 }
321 #endif
322 #endif
divmod_s64s64
int64_t divmod_s64s64(int64_t a, int64_t b)
Integer divide of two signed 64-bit values.
divmod_s32s32_rem
static int32_t divmod_s32s32_rem(int32_t a, int32_t b, int32_t *rem)
Integer divide of two signed 32-bit values, with remainder.
Definition: divider.h:52
divmod_s32s32_unsafe
int64_t divmod_s32s32_unsafe(int32_t a, int32_t b)
Unsafe integer divide of two unsigned 32-bit values.
divmod_u32u32
divmod_result_t divmod_u32u32(uint32_t a, uint32_t b)
Integer divide of two unsigned 32-bit values.
divmod_s32s32
divmod_result_t divmod_s32s32(int32_t a, int32_t b)
Integer divide of two signed 32-bit values.
divmod_s64s64_rem_unsafe
int64_t divmod_s64s64_rem_unsafe(int64_t a, int64_t b, int64_t *rem)
Unsafe integer divide of two signed 64-bit values, with remainder.
to_remainder_s32
static int32_t to_remainder_s32(divmod_result_t r)
Efficient extraction of signed remainder from 32p32 fixed point.
Definition: divider.h:238
div_u32u32
uint32_t div_u32u32(uint32_t a, uint32_t b)
Integer divide of two unsigned 32-bit values.
divmod_u64u64_unsafe
uint64_t divmod_u64u64_unsafe(uint64_t a, uint64_t b)
Unsafe integer divide of two signed 64-bit values.
to_quotient_u32
static uint32_t to_quotient_u32(divmod_result_t r)
Efficient extraction of unsigned quotient from 32p32 fixed point.
Definition: divider.h:204
divmod_u64u64
uint64_t divmod_u64u64(uint64_t a, uint64_t b)
Integer divide of two signed 64-bit values.
to_quotient_s32
static int32_t to_quotient_s32(divmod_result_t r)
Efficient extraction of signed quotient from 32p32 fixed point.
Definition: divider.h:214
divmod_u64u64_rem_unsafe
uint64_t divmod_u64u64_rem_unsafe(uint64_t a, uint64_t b, uint64_t *rem)
Unsafe integer divide of two unsigned 64-bit values, with remainder.
divider.h
div_u64u64
uint64_t div_u64u64(uint64_t a, uint64_t b)
Integer divide of two unsigned 64-bit values.
to_remainder_u32
static uint32_t to_remainder_u32(divmod_result_t r)
Efficient extraction of unsigned remainder from 32p32 fixed point.
Definition: divider.h:226
div_s32s32_unsafe
int32_t div_s32s32_unsafe(int32_t a, int32_t b)
Unsafe integer divide of two signed 32-bit values.
divmod_s64s64_unsafe
int64_t divmod_s64s64_unsafe(int64_t a, int64_t b)
Unsafe integer divide of two signed 64-bit values.
divmod_u32u32_rem_unsafe
uint32_t divmod_u32u32_rem_unsafe(uint32_t a, uint32_t b, uint32_t *rem)
Unsafe integer divide of two unsigned 32-bit values, with remainder.
hw_divider_divmod_u32
divmod_result_t hw_divider_divmod_u32(uint32_t a, uint32_t b)
Do an unsigned HW divide and wait for result.
divmod_s32s32_rem_unsafe
int32_t divmod_s32s32_rem_unsafe(int32_t a, int32_t b, int32_t *rem)
Unsafe integer divide of two signed 32-bit values, with remainder.
pico.h
div_u32u32_unsafe
uint32_t div_u32u32_unsafe(uint32_t a, uint32_t b)
Unsafe integer divide of two unsigned 32-bit values.
div_s64s64
int64_t div_s64s64(int64_t a, int64_t b)
Integer divide of two signed 64-bit values.
divmod_s64s64_rem
int64_t divmod_s64s64_rem(int64_t a, int64_t b, int64_t *rem)
Integer divide of two signed 64-bit values, with remainder.
div_s32s32
int32_t div_s32s32(int32_t a, int32_t b)
Integer divide of two signed 32-bit values.
divmod_u32u32_unsafe
uint64_t divmod_u32u32_unsafe(uint32_t a, uint32_t b)
Unsafe integer divide of two unsigned 32-bit values.
divmod_u32u32_rem
static uint32_t divmod_u32u32_rem(uint32_t a, uint32_t b, uint32_t *rem)
Integer divide of two unsigned 32-bit values, with remainder.
Definition: divider.h:87
divmod_u64u64_rem
uint64_t divmod_u64u64_rem(uint64_t a, uint64_t b, uint64_t *rem)
Integer divide of two unsigned 64-bit values, with remainder.
div_u64u64_unsafe
uint64_t div_u64u64_unsafe(uint64_t a, uint64_t b)
Unsafe integer divide of two unsigned 64-bit values.
hw_divider_divmod_s32
divmod_result_t hw_divider_divmod_s32(int32_t a, int32_t b)
Do a signed HW divide and wait for result.
div_s64s64_unsafe
int64_t div_s64s64_unsafe(int64_t a, int64_t b)
Unsafe integer divide of two signed 64-bit values.