feat: add display UI with splash screen, dashboard, and RSSI sparkline
Implement full SSD1306 OLED display support: - Splash screen on boot (1.5s) with project name, version, board - Dashboard showing radio state, config, counters, RSSI/SNR - RSSI history sparkline (64-sample ring buffer, 2px-wide bars) - DisplayOn/DisplayOff host commands routed via dedicated channel - Pure render functions against generic DrawTarget (no hardware coupling) - Display on/off control bypasses radio task entirely Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
17624 files changed
+4564928
-38
No files matched your search
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"files":{".cargo_vcs_info.json":"71e8dc7872a53941fd9216c16d47b2dfc06f34d0fc31297db807ce1eb3df22d8",".github/workflows/rust.yml":"a81627d57273ba912af495d18a70926aece632f7012fa285ecd85785bdd38c03","Cargo.toml":"001b75339958d68d98b98d3dfc713e4b16bb4c88c68182a8d7a1ce1af39784b3","Cargo.toml.orig":"aa3dc11f44de18e8faa0ded54b5aada6d50e51e169bf37bbe4a53f2b58dc588f","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"4c9222d8d8ec233ffc1b317729878430f2db2a49cb2324c856a02e9848a3c185","README.md":"bfef5e168b0f02e5a3b9d614297b9053e1faf110a0c168842329af4ad56190bf","benches/gcd_benchmark.rs":"468a4e3fd278b8fb0e19c44cd683c2aac882030573a28e99749970834a075a51","src/lib.rs":"05ba771ddc913fe3a0841d1cc2d860a29cf48ca515a0ad9216f7438b1b7ecf7a"},"package":"1d758ba1b47b00caf47f24925c0074ecb20d6dfcffe7f6d53395c0465674841a"}
|
||||
Vendored
+6
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "df6ba81825b2cb4f1e328f2283fb0fda8f8bda27"
|
||||
},
|
||||
"path_in_vcs": ""
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
name: Rust
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ master ]
|
||||
pull_request:
|
||||
branches: [ master ]
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
|
||||
jobs:
|
||||
check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Check
|
||||
run: cargo check --verbose
|
||||
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run tests
|
||||
run: cargo test --verbose
|
||||
|
||||
bench:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Run Benches
|
||||
run: cargo bench
|
||||
Vendored
+37
@@ -0,0 +1,37 @@
|
||||
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
|
||||
#
|
||||
# When uploading crates to the registry Cargo will automatically
|
||||
# "normalize" Cargo.toml files for maximal compatibility
|
||||
# with all versions of Cargo and also rewrite `path` dependencies
|
||||
# to registry (e.g., crates.io) dependencies.
|
||||
#
|
||||
# If you are reading this file be aware that the original Cargo.toml
|
||||
# will likely look very different (and much more reasonable).
|
||||
# See Cargo.toml.orig for the original contents.
|
||||
|
||||
[package]
|
||||
edition = "2021"
|
||||
name = "gcd"
|
||||
version = "2.3.0"
|
||||
authors = ["Corey Farwell <coreyf@rwell.org>"]
|
||||
description = "Calculate the greatest common divisor"
|
||||
documentation = "https://docs.rs/gcd/"
|
||||
readme = "README.md"
|
||||
categories = ["algorithms"]
|
||||
license = "MIT/Apache-2.0"
|
||||
repository = "https://github.com/frewsxcv/rust-gcd"
|
||||
|
||||
[[bench]]
|
||||
name = "gcd_benchmark"
|
||||
harness = false
|
||||
|
||||
[dependencies]
|
||||
|
||||
[dev-dependencies.criterion]
|
||||
version = "0.3"
|
||||
|
||||
[dev-dependencies.rand]
|
||||
version = "0.7"
|
||||
|
||||
[dev-dependencies.rand_chacha]
|
||||
version = "0.2"
|
||||
Vendored
+201
@@ -0,0 +1,201 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
Copyright (c) 2016 Corey Farwell
|
||||
|
||||
Permission is hereby granted, free of charge, to any
|
||||
person obtaining a copy of this software and associated
|
||||
documentation files (the "Software"), to deal in the
|
||||
Software without restriction, including without
|
||||
limitation the rights to use, copy, modify, merge,
|
||||
publish, distribute, sublicense, and/or sell copies of
|
||||
the Software, and to permit persons to whom the Software
|
||||
is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions
|
||||
of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
||||
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
||||
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
||||
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
||||
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
||||
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
DEALINGS IN THE SOFTWARE.
|
||||
Vendored
+18
@@ -0,0 +1,18 @@
|
||||
# rust-gcd
|
||||
|
||||
[Documentation](https://docs.rs/gcd/)
|
||||
|
||||
Small Rust library for calculating the greatest common divisor of two numbers
|
||||
|
||||
## License
|
||||
|
||||
Licensed under either of
|
||||
|
||||
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
|
||||
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
|
||||
|
||||
at your option.
|
||||
|
||||
### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
|
||||
Vendored
+42
@@ -0,0 +1,42 @@
|
||||
extern crate criterion;
|
||||
extern crate gcd;
|
||||
extern crate rand;
|
||||
extern crate rand_chacha;
|
||||
|
||||
use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
||||
use gcd::Gcd;
|
||||
use rand::{Rng, SeedableRng};
|
||||
|
||||
const SEED: u64 = 314;
|
||||
|
||||
fn criterion_benchmark(c: &mut Criterion) {
|
||||
macro_rules! bench_function {
|
||||
($name:expr, $type:ty, $func:ident) => {
|
||||
c.bench_function($name, |b| {
|
||||
let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(SEED);
|
||||
let values: Vec<$type> = (0..1000).map(|_| rng.gen()).collect();
|
||||
b.iter(|| {
|
||||
for i in 0..values.len() - 1 {
|
||||
let (a, b) = (values[i], values[i + 1]);
|
||||
black_box(a.$func(b));
|
||||
}
|
||||
})
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
bench_function!("gcd euclid u8", u8, gcd_euclid);
|
||||
bench_function!("gcd euclid u16", u16, gcd_euclid);
|
||||
bench_function!("gcd euclid u32", u32, gcd_euclid);
|
||||
bench_function!("gcd euclid u64", u64, gcd_euclid);
|
||||
bench_function!("gcd euclid u128", u128, gcd_euclid);
|
||||
|
||||
bench_function!("gcd binary u8", u8, gcd_binary);
|
||||
bench_function!("gcd binary u16", u16, gcd_binary);
|
||||
bench_function!("gcd binary u32", u32, gcd_binary);
|
||||
bench_function!("gcd binary u64", u64, gcd_binary);
|
||||
bench_function!("gcd binary u128", u128, gcd_binary);
|
||||
}
|
||||
|
||||
criterion_group!(benches, criterion_benchmark);
|
||||
criterion_main!(benches);
|
||||
Vendored
+294
@@ -0,0 +1,294 @@
|
||||
#![no_std]
|
||||
use core::num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize};
|
||||
|
||||
pub trait Gcd {
|
||||
/// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
|
||||
/// using [`gcd_binary`].
|
||||
///
|
||||
/// [`gcd_binary`]: #method.gcd_binary
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use gcd::Gcd;
|
||||
///
|
||||
/// assert_eq!(0, 0u8.gcd(0));
|
||||
/// assert_eq!(10, 10u8.gcd(0));
|
||||
/// assert_eq!(10, 0u8.gcd(10));
|
||||
/// assert_eq!(10, 10u8.gcd(20));
|
||||
/// assert_eq!(44, 2024u32.gcd(748));
|
||||
/// ```
|
||||
fn gcd(self, other: Self) -> Self;
|
||||
|
||||
/// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
|
||||
/// using the [Binary GCD algorithm](https://en.wikipedia.org/wiki/Binary_GCD_algorithm).
|
||||
fn gcd_binary(self, other: Self) -> Self;
|
||||
|
||||
/// Determine [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor)
|
||||
/// using the [Euclidean algorithm](https://en.wikipedia.org/wiki/Euclidean_algorithm).
|
||||
fn gcd_euclid(self, other: Self) -> Self;
|
||||
}
|
||||
|
||||
macro_rules! gcd_impl {
|
||||
($(($T:ty) $binary:ident $euclid:ident),*) => {$(
|
||||
#[doc = concat!("Const binary GCD implementation for `", stringify!($T), "`.")]
|
||||
pub const fn $binary(mut u: $T, mut v: $T) -> $T
|
||||
{
|
||||
if u == 0 { return v; }
|
||||
if v == 0 { return u; }
|
||||
|
||||
let shift = (u | v).trailing_zeros();
|
||||
u >>= shift;
|
||||
v >>= shift;
|
||||
u >>= u.trailing_zeros();
|
||||
|
||||
loop {
|
||||
v >>= v.trailing_zeros();
|
||||
|
||||
#[allow(clippy::manual_swap)]
|
||||
if u > v {
|
||||
// mem::swap(&mut u, &mut v);
|
||||
let temp = u;
|
||||
u = v;
|
||||
v = temp;
|
||||
}
|
||||
|
||||
v -= u; // here v >= u
|
||||
|
||||
if v == 0 { break; }
|
||||
}
|
||||
|
||||
u << shift
|
||||
}
|
||||
|
||||
#[doc = concat!("Const euclid GCD implementation for `", stringify!($T), "`.")]
|
||||
pub const fn $euclid(a: $T, b: $T) -> $T
|
||||
{
|
||||
// variable names based off euclidean division equation: a = b · q + r
|
||||
let (mut a, mut b) = if a > b {
|
||||
(a, b)
|
||||
} else {
|
||||
(b, a)
|
||||
};
|
||||
|
||||
#[allow(clippy::manual_swap)]
|
||||
while b != 0 {
|
||||
// mem::swap(&mut a, &mut b);
|
||||
let temp = a;
|
||||
a = b;
|
||||
b = temp;
|
||||
|
||||
b %= a;
|
||||
}
|
||||
|
||||
a
|
||||
}
|
||||
|
||||
impl Gcd for $T {
|
||||
#[inline]
|
||||
fn gcd(self, other: $T) -> $T {
|
||||
self.gcd_binary(other)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn gcd_binary(self, v: $T) -> $T {
|
||||
$binary(self, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn gcd_euclid(self, other: $T) -> $T {
|
||||
$euclid(self, other)
|
||||
}
|
||||
}
|
||||
)*};
|
||||
}
|
||||
|
||||
gcd_impl! {
|
||||
(u8) binary_u8 euclid_u8,
|
||||
(u16) binary_u16 euclid_u16,
|
||||
(u32) binary_u32 euclid_u32,
|
||||
(u64) binary_u64 euclid_u64,
|
||||
(u128) binary_u128 euclid_u128,
|
||||
(usize) binary_usize euclid_usize
|
||||
}
|
||||
|
||||
macro_rules! gcd_impl_nonzero {
|
||||
($(($T:ty) $binary_nonzero:ident/$binary:ident $euclid_nonzero:ident/$euclid:ident),*) => {$(
|
||||
#[doc = concat!("Const binary GCD implementation for `", stringify!($T), "`.")]
|
||||
pub const fn $binary_nonzero(u: $T, v: $T) -> $T
|
||||
{
|
||||
match <$T>::new($binary(u.get(), v.get())) {
|
||||
Some(x) => x,
|
||||
None => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
#[doc = concat!("Const euclid GCD implementation for `", stringify!($T), "`.")]
|
||||
pub const fn $euclid_nonzero(a: $T, b: $T) -> $T
|
||||
{
|
||||
match <$T>::new($euclid(a.get(), b.get())) {
|
||||
Some(x) => x,
|
||||
None => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
impl Gcd for $T {
|
||||
#[inline]
|
||||
fn gcd(self, other: $T) -> $T {
|
||||
self.gcd_binary(other)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn gcd_binary(self, v: $T) -> $T {
|
||||
$binary_nonzero(self, v)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn gcd_euclid(self, other: $T) -> $T {
|
||||
$euclid_nonzero(self, other)
|
||||
}
|
||||
}
|
||||
)*}
|
||||
}
|
||||
|
||||
gcd_impl_nonzero! {
|
||||
(NonZeroU8) binary_nonzero_u8/binary_u8 euclid_nonzero_u8/euclid_u8,
|
||||
(NonZeroU16) binary_nonzero_u16/binary_u16 euclid_nonzero_u16/euclid_u16,
|
||||
(NonZeroU32) binary_nonzero_u32/binary_u32 euclid_nonzero_u32/euclid_u32,
|
||||
(NonZeroU64) binary_nonzero_u64/binary_u64 euclid_nonzero_u64/euclid_u64,
|
||||
(NonZeroU128) binary_nonzero_u128/binary_u128 euclid_nonzero_u128/euclid_u128,
|
||||
(NonZeroUsize) binary_nonzero_usize/binary_usize euclid_nonzero_usize/euclid_usize
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use core::fmt::Debug;
|
||||
|
||||
const U8_GCD_A: [u8; 5] = [140, 1, 140, 33, 225];
|
||||
const U8_GCD_B: [u8; 5] = [136, 123, 203, 252, 153];
|
||||
const U8_GCD_R: [u8; 5] = [4, 1, 7, 3, 9];
|
||||
|
||||
const U16_GCD_A: [u16; 5] = [53144, 44062, 65054, 60568, 11932];
|
||||
const U16_GCD_B: [u16; 5] = [41105, 5088, 35332, 19184, 54004];
|
||||
const U16_GCD_R: [u16; 5] = [1, 2, 22, 8, 4];
|
||||
|
||||
const U32_GCD_A: [u32; 5] = [3392079986, 273672341, 1353048788, 1491301950, 3569727686];
|
||||
const U32_GCD_B: [u32; 5] = [2080089626, 3912533700, 1969135932, 1356732645, 58056677];
|
||||
const U32_GCD_R: [u32; 5] = [2, 1, 4, 15, 7];
|
||||
|
||||
const U64_GCD_A: [u64; 5] = [
|
||||
190266297176832000,
|
||||
2040134905096275968,
|
||||
16611311494648745984,
|
||||
14863931409971066880,
|
||||
11777713923171739648,
|
||||
];
|
||||
const U64_GCD_B: [u64; 5] = [
|
||||
10430732356495263744,
|
||||
5701159354248194048,
|
||||
7514969329383038976,
|
||||
7911906750992527360,
|
||||
1994469765110767616,
|
||||
];
|
||||
const U64_GCD_R: [u64; 5] = [6144, 2048, 4096, 10240, 14336];
|
||||
|
||||
const U128_GCD_A: [u128; 5] = [
|
||||
183222947567111613556380400704880115712,
|
||||
115621006611964852903362423926779019264,
|
||||
50724538437787115589243518273596686336,
|
||||
18298803717624646317403958239767298048,
|
||||
196929845599653749349770751890136498176,
|
||||
];
|
||||
const U128_GCD_B: [u128; 5] = [
|
||||
283620717889381409474181015983148236800,
|
||||
152390035351551984363917166384150216704,
|
||||
74996138554240857099554660445327458304,
|
||||
245604784002268488089190010796573196288,
|
||||
194671916188106984823441978656659865600,
|
||||
];
|
||||
const U128_GCD_R: [u128; 5] = [
|
||||
37778931862957161709568,
|
||||
75557863725914323419136,
|
||||
113336795588871485128704,
|
||||
151115727451828646838272,
|
||||
302231454903657293676544,
|
||||
];
|
||||
|
||||
const USIZE_GCD_A: [usize; 5] = [335286345, 3125888386, 3550412466, 924335944, 2870209473];
|
||||
const USIZE_GCD_B: [usize; 5] = [1843742025, 2080426243, 16052620, 1587387560, 24708111];
|
||||
const USIZE_GCD_R: [usize; 5] = [15, 1, 2, 8, 3];
|
||||
|
||||
#[test]
|
||||
fn test_gcd_basic() {
|
||||
// some base cases
|
||||
assert_eq!(0, 0u8.gcd(0));
|
||||
assert_eq!(10, 10u8.gcd(0));
|
||||
assert_eq!(10, 0u8.gcd(10));
|
||||
}
|
||||
|
||||
fn verify_gcd<T>(a: T, b: T, r: T)
|
||||
where
|
||||
T: Gcd + Copy + PartialEq + Debug,
|
||||
{
|
||||
let gcd = a.gcd(b);
|
||||
let egcd = a.gcd_euclid(b);
|
||||
let bgcd = a.gcd_binary(b);
|
||||
assert_eq!(r, gcd, "{:?}.gcd({:?})", a, b);
|
||||
assert_eq!(r, egcd, "{:?}.gcd_euclid({:?})", a, b);
|
||||
assert_eq!(r, bgcd, "{:?}.gcd_binary({:?})", a, b);
|
||||
}
|
||||
|
||||
fn test_gcd_ty<T, NZ, const N: usize>(
|
||||
new: impl Fn(T) -> Option<NZ>,
|
||||
zero: T,
|
||||
a: &[T; N],
|
||||
b: &[T; N],
|
||||
r: &[T; N],
|
||||
) where
|
||||
T: Gcd + Copy + PartialEq + Debug,
|
||||
NZ: Gcd + Copy + PartialEq + Debug,
|
||||
{
|
||||
for ind in 0..N {
|
||||
let a = new(a[ind]).unwrap();
|
||||
let b = new(b[ind]).unwrap();
|
||||
let r = new(r[ind]).unwrap();
|
||||
verify_gcd(a, b, r);
|
||||
}
|
||||
|
||||
let a = a[0];
|
||||
verify_gcd(zero, a, a);
|
||||
verify_gcd(a, zero, a);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_gcd() {
|
||||
test_gcd_ty(NonZeroU8::new, 0, &U8_GCD_A, &U8_GCD_B, &U8_GCD_R);
|
||||
test_gcd_ty(NonZeroU16::new, 0, &U16_GCD_A, &U16_GCD_B, &U16_GCD_R);
|
||||
test_gcd_ty(NonZeroU32::new, 0, &U32_GCD_A, &U32_GCD_B, &U32_GCD_R);
|
||||
test_gcd_ty(NonZeroU64::new, 0, &U64_GCD_A, &U64_GCD_B, &U64_GCD_R);
|
||||
test_gcd_ty(NonZeroU128::new, 0, &U128_GCD_A, &U128_GCD_B, &U128_GCD_R);
|
||||
test_gcd_ty(
|
||||
NonZeroUsize::new,
|
||||
0,
|
||||
&USIZE_GCD_A,
|
||||
&USIZE_GCD_B,
|
||||
&USIZE_GCD_R,
|
||||
);
|
||||
}
|
||||
|
||||
const U32_GCD_R_0: u32 = binary_u32(U32_GCD_A[0], U32_GCD_B[0]);
|
||||
const U32_GCD_R_1: u32 = euclid_u32(U32_GCD_A[1], U32_GCD_B[1]);
|
||||
const U32_GCD_R_2: u32 = binary_u32(U32_GCD_A[2], U32_GCD_B[2]);
|
||||
const U32_GCD_R_3: u32 = euclid_u32(U32_GCD_A[3], U32_GCD_B[3]);
|
||||
const U32_GCD_R_4: u32 = binary_u32(U32_GCD_A[4], U32_GCD_B[4]);
|
||||
|
||||
#[test]
|
||||
fn test_const_gcd() {
|
||||
assert_eq!(U32_GCD_R[0], U32_GCD_R_0);
|
||||
assert_eq!(U32_GCD_R[1], U32_GCD_R_1);
|
||||
assert_eq!(U32_GCD_R[2], U32_GCD_R_2);
|
||||
assert_eq!(U32_GCD_R[3], U32_GCD_R_3);
|
||||
assert_eq!(U32_GCD_R[4], U32_GCD_R_4);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user