Loading...
Searching...
No Matches
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
14extern "C" {
15#endif
16
23
33
42int32_t div_s32s32(int32_t a, int32_t b);
43
53static inline int32_t divmod_s32s32_rem(int32_t a, int32_t b, int32_t *rem) {
54 divmod_result_t r = hw_divider_divmod_s32(a, b);
55 *rem = to_remainder_s32(r);
56 return to_quotient_s32(r);
57}
58
67divmod_result_t divmod_s32s32(int32_t a, int32_t b);
68
77uint32_t div_u32u32(uint32_t a, uint32_t b);
78
88static inline uint32_t divmod_u32u32_rem(uint32_t a, uint32_t b, uint32_t *rem) {
89 divmod_result_t r = hw_divider_divmod_u32(a, b);
90 *rem = to_remainder_u32(r);
91 return to_quotient_u32(r);
92}
93
102divmod_result_t divmod_u32u32(uint32_t a, uint32_t b);
103
112int64_t div_s64s64(int64_t a, int64_t b);
113
123int64_t divmod_s64s64_rem(int64_t a, int64_t b, int64_t *rem);
124
125#if PICO_RP2040 && !LIB_PICO_DIVIDER_COMPILER
134int64_t divmod_s64s64(int64_t a, int64_t b);
135#endif
136
145uint64_t div_u64u64(uint64_t a, uint64_t b);
146
156uint64_t divmod_u64u64_rem(uint64_t a, uint64_t b, uint64_t *rem);
157
158#if PICO_RP2040 && !LIB_PICO_DIVIDER_COMPILER
167uint64_t divmod_u64u64(uint64_t a, uint64_t b);
168#endif
169
170// -----------------------------------------------------------------------
171// these "unsafe" functions are slightly faster, but do not save the divider state,
172// so are not generally safe to be called from interrupts
173// -----------------------------------------------------------------------
174
185int32_t div_s32s32_unsafe(int32_t a, int32_t b);
186
198int32_t divmod_s32s32_rem_unsafe(int32_t a, int32_t b, int32_t *rem);
199
210divmod_result_t divmod_s32s32_unsafe(int32_t a, int32_t b);
211
222uint32_t div_u32u32_unsafe(uint32_t a, uint32_t b);
223
235uint32_t divmod_u32u32_rem_unsafe(uint32_t a, uint32_t b, uint32_t *rem);
236
247divmod_result_t divmod_u32u32_unsafe(uint32_t a, uint32_t b);
248
259int64_t div_s64s64_unsafe(int64_t a, int64_t b);
260
272int64_t divmod_s64s64_rem_unsafe(int64_t a, int64_t b, int64_t *rem);
273
284int64_t divmod_s64s64_unsafe(int64_t a, int64_t b);
285
296uint64_t div_u64u64_unsafe(uint64_t a, uint64_t b);
297
309uint64_t divmod_u64u64_rem_unsafe(uint64_t a, uint64_t b, uint64_t *rem);
310
321uint64_t divmod_u64u64_unsafe(uint64_t a, uint64_t b);
322
323#ifdef __cplusplus
324}
325#endif
326#endif
static uint32_t to_quotient_u32(divmod_result_t r)
Efficient extraction of unsigned quotient from 32p32 fixed point.
Definition divider.h:198
static int32_t to_quotient_s32(divmod_result_t r)
Efficient extraction of signed quotient from 32p32 fixed point.
Definition divider.h:208
static int32_t to_remainder_s32(divmod_result_t r)
Efficient extraction of signed remainder from 32p32 fixed point.
Definition divider.h:232
static divmod_result_t hw_divider_divmod_s32(int32_t a, int32_t b)
Do a signed HW divide and wait for result.
Definition divider.h:74
static divmod_result_t hw_divider_divmod_u32(uint32_t a, uint32_t b)
Do an unsigned HW divide and wait for result.
Definition divider.h:92
static uint32_t to_remainder_u32(divmod_result_t r)
Efficient extraction of unsigned remainder from 32p32 fixed point.
Definition divider.h:220
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:88
int64_t div_s64s64(int64_t a, int64_t b)
Integer divide of two signed 64-bit values.
Definition divider_compiler.c:66
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.
Definition divider_compiler.c:105
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.
Definition divider_compiler.c:101
uint64_t divmod_u64u64_unsafe(uint64_t a, uint64_t b)
Unsafe integer divide of two unsigned 64-bit values, with remainder.
uint64_t div_u64u64(uint64_t a, uint64_t b)
Integer divide of two unsigned 64-bit values.
Definition divider_compiler.c:82
int64_t divmod_s64s64_unsafe(int64_t a, int64_t b)
Unsafe integer divide of two signed 64-bit values, with remainder.
uint64_t div_u64u64_unsafe(uint64_t a, uint64_t b)
Unsafe integer divide of two unsigned 64-bit values.
Definition divider_compiler.c:113
int32_t div_s32s32(int32_t a, int32_t b)
Integer divide of two signed 32-bit values.
Definition divider_compiler.c:10
divmod_result_t divmod_s32s32_unsafe(int32_t a, int32_t b)
Unsafe integer divide of two signed 32-bit values, with remainder.
Definition divider_compiler.c:102
divmod_result_t divmod_u32u32(uint32_t a, uint32_t b)
Integer divide of two unsigned 32-bit values, with remainder.
Definition divider_compiler.c:22
int64_t divmod_s64s64_rem(int64_t a, int64_t b, int64_t *rem)
Integer divide of two signed 64-bit values, with remainder.
Definition divider_compiler.c:71
int32_t div_s32s32_unsafe(int32_t a, int32_t b)
Unsafe integer divide of two signed 32-bit values.
Definition divider_compiler.c:100
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.
Definition divider_compiler.c:114
uint32_t div_u32u32(uint32_t a, uint32_t b)
Integer divide of two unsigned 32-bit values.
Definition divider_compiler.c:18
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:53
divmod_result_t divmod_s32s32(int32_t a, int32_t b)
Integer divide of two signed 32-bit values, with remainder.
Definition divider_compiler.c:14
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.
Definition divider_compiler.c:109
uint64_t divmod_u64u64_rem(uint64_t a, uint64_t b, uint64_t *rem)
Integer divide of two unsigned 64-bit values, with remainder.
Definition divider_compiler.c:87
divmod_result_t divmod_u32u32_unsafe(uint32_t a, uint32_t b)
Unsafe integer divide of two unsigned 32-bit values, with remainder.
Definition divider_compiler.c:106
uint32_t div_u32u32_unsafe(uint32_t a, uint32_t b)
Unsafe integer divide of two unsigned 32-bit values.
Definition divider_compiler.c:104
int64_t div_s64s64_unsafe(int64_t a, int64_t b)
Unsafe integer divide of two signed 64-bit values.
Definition divider_compiler.c:108