From 1081e52ad96cfa8f9c0dfae600562f2d6c364026 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 22 Mar 2024 12:36:37 +0100 Subject: [PATCH] zad7 --- zad7/.ccls | 21 +++++++++++ zad7/Cargo.lock | 7 ++++ zad7/Cargo.toml | 14 +++++++ zad7/Makefile | 37 ++++++++++++++++++ zad7/c/main.c | 21 +++++++++++ zad7/c/wrapper.h | 18 +++++++++ zad7/src/iter.rs | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ zad7/src/lib.rs | 2 + zad7/src/rec.rs | 69 ++++++++++++++++++++++++++++++++++ 9 files changed, 287 insertions(+) create mode 100644 zad7/.ccls create mode 100644 zad7/Cargo.lock create mode 100644 zad7/Cargo.toml create mode 100644 zad7/Makefile create mode 100644 zad7/c/main.c create mode 100644 zad7/c/wrapper.h create mode 100644 zad7/src/iter.rs create mode 100644 zad7/src/lib.rs create mode 100644 zad7/src/rec.rs diff --git a/zad7/.ccls b/zad7/.ccls new file mode 100644 index 0000000..9f91bd0 --- /dev/null +++ b/zad7/.ccls @@ -0,0 +1,21 @@ +gcc +%c -std=c99 +%h +-Iinclude +-Wall +-Wextra +-Wpedantic +-Wstrict-aliasing +-Wfloat-equal +-Wundef +-Wshadow +-Wpointer-arith +-Wcast-align +-Wstrict-prototypes +-Wstrict-overflow=5 +-Wwrite-strings +-Wcast-qual +-Wswitch-default +-Wswitch-enum +-Wconversion +-Wunreachable-code diff --git a/zad7/Cargo.lock b/zad7/Cargo.lock new file mode 100644 index 0000000..9195ef4 --- /dev/null +++ b/zad7/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "zad7" +version = "0.1.0" diff --git a/zad7/Cargo.toml b/zad7/Cargo.toml new file mode 100644 index 0000000..c13faf8 --- /dev/null +++ b/zad7/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "zad7" +version = "0.1.0" +edition = "2021" + +[lib] +name = "zad7" +crate-type = ["staticlib", "cdylib"] + +[dependencies] + +[features] +iter = [] +rec = [] diff --git a/zad7/Makefile b/zad7/Makefile new file mode 100644 index 0000000..3fed236 --- /dev/null +++ b/zad7/Makefile @@ -0,0 +1,37 @@ +CC = gcc +CFLAGS = -std=c99 -O3 -Wall -Wextra -Wpedantic -Wstrict-aliasing +CFLAGS += -Wfloat-equal -Wundef -Wshadow -Wpointer-arith -Wcast-align +CFLAGS += -Wstrict-prototypes -Wstrict-overflow=5 -Wwrite-strings +CFLAGS += -Wcast-qual -Wswitch-default -Wswitch-enum +CFLAGS += -Wconversion -Wunreachable-code +CFLAGS += -Ic +LDFLAGS = + +CARGO = cargo + +NAME = z7 + +SRC = c +BIN = target + +_PROG = main.c +PROG = $(addprefix $(SRC)/, $(_PROG)) + +OBJ = $(_PROG:.c=.o) + +.PHONY: iter rec clean + +iter: dirs + $(CARGO) build --features iter + $(CC) -o $(BIN)/$(NAME) $(PROG) $(CFLAGS) -L. -l:target/debug/libzad7.so + +rec: dirs + $(CARGO) build --features rec + $(CC) -o $(BIN)/$(NAME) $(PROG) $(CFLAGS) -L. -l:target/debug/libzad7.so + +dirs: + mkdir -p $(BIN) + +clean: + rm -rf $(addprefix $(BIN)/, $(OBJ)) + rm -rf $(BIN)/$(NAME) diff --git a/zad7/c/main.c b/zad7/c/main.c new file mode 100644 index 0000000..a63b857 --- /dev/null +++ b/zad7/c/main.c @@ -0,0 +1,21 @@ +#include + +#include +#include +#include + +int main(int argc, char *argv[]) { + if (argc != 4) { + fprintf(stderr, "usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + printf("gcd(a, b): %u\n", gcd(atol(argv[1]), atol(argv[2]))); + printf("factorial(a): %u\n", factorial(atol(argv[1]))); + Result r = diophantine(atol(argv[1]), atol(argv[2]), atol(argv[3])); + if (!r.resultExists) + printf("dupa\n"); + else + printf("diophantine(a, b, c): %d, %d\n", r.n, r.m); + + return 0; +} diff --git a/zad7/c/wrapper.h b/zad7/c/wrapper.h new file mode 100644 index 0000000..1e45105 --- /dev/null +++ b/zad7/c/wrapper.h @@ -0,0 +1,18 @@ +#ifndef _JPP_L1_Z7_WRAPPER_H +#define _JPP_L1_Z7_WRAPPER_H + +#include + +extern unsigned int gcd(unsigned int a, unsigned int b); + +extern unsigned int factorial(unsigned int n); + +typedef struct { + bool resultExists; + int n; + int m; +} Result; + +extern Result diophantine(int a, int b, int c); + +#endif // _JPP_L1_Z7_WRAPPER_H diff --git a/zad7/src/iter.rs b/zad7/src/iter.rs new file mode 100644 index 0000000..e51fabd --- /dev/null +++ b/zad7/src/iter.rs @@ -0,0 +1,98 @@ +fn extended_gcd(a: i32, b: i32, x: &mut i32, y: &mut i32) -> i32 { + *x = 1; + *y = 0; + let mut x1 = 0i32; + let mut y1 = 1i32; + let mut q: i32; + let mut temp_x: i32; + let mut temp_y: i32; + let mut temp_b: i32; + let mut _a = a; + let mut _b = b; + + while _b != 0 { + q = _a / _b; + + temp_x = *x - q * x1; + temp_y = *y - q * y1; + *x = x1; + *y = y1; + x1 = temp_x; + y1 = temp_y; + + temp_b = _a - q * _b; + _a = _b; + _b = temp_b; + } + + _a +} + +#[no_mangle] +pub extern fn factorial(n: u32) -> u32 { + let mut result = 1u32; + + for i in 1..=n { + result *= i; + } + + result +} + +#[no_mangle] +pub extern fn gcd(a: u32, b: u32) -> u32 { + if a == 0 { + return b; + } + + let mut _a = a; + let mut _b = b; + + let mut r: u32; + + while _b > 0 { + r = _a % _b; + + _a = _b; + _b = r; + } + + _a +} + +#[repr(C)] +pub struct Result { + pub resultExists: bool, + pub n: i32, + pub m: i32, +} + +#[no_mangle] +pub extern fn diophantine(a: i32, b: i32, c: i32) -> Result { + let mut r = Result { + n: 0, + m: 0, + resultExists: false, + }; + if a == 0 && b == 0 { + return r; + } + let mut x0 = 0i32; + let mut y0 = 0i32; + let g = extended_gcd(a.abs(), b.abs(), &mut x0, &mut y0); + + if c % g != 0 { + return r; + } + + x0 *= c / g; + y0 *= c / g; + if a < 0 { x0 = -x0; } + if b < 0 { y0 = -y0; } + + r.resultExists = true; + r.n = x0; + r.m = y0; + + r +} diff --git a/zad7/src/lib.rs b/zad7/src/lib.rs new file mode 100644 index 0000000..7ba1bf6 --- /dev/null +++ b/zad7/src/lib.rs @@ -0,0 +1,2 @@ +#[cfg(feature="iter")] mod iter; +#[cfg(feature="rec")] mod rec; diff --git a/zad7/src/rec.rs b/zad7/src/rec.rs new file mode 100644 index 0000000..97fed9d --- /dev/null +++ b/zad7/src/rec.rs @@ -0,0 +1,69 @@ +fn extended_gcd(a: i32, b: i32, x: &mut i32, y: &mut i32) -> i32 { + if b == 0 { + *x = 1; + *y = 0; + return a; + } + let mut x1 = 0i32; + let mut y1 = 0i32; + let d = extended_gcd(b, a % b, &mut x1, &mut y1); + *x = y1; + *y = x1 - y1 * (a / b); + + d +} + +#[no_mangle] +pub extern fn factorial(n: u32) -> u32 { + if n == 0 || n == 1 { + return 1; + } + + n * factorial(n - 1) +} + +#[no_mangle] +pub extern fn gcd(a: u32, b: u32) -> u32 { + if b == 0 { + return a; + } + + gcd(b, a % b) +} + +#[repr(C)] +pub struct Result { + pub resultExists: bool, + pub n: i32, + pub m: i32, +} + +#[no_mangle] +pub extern fn diophantine(a: i32, b: i32, c: i32) -> Result { + let mut r = Result { + n: 0, + m: 0, + resultExists: false, + }; + if a == 0 && b == 0 { + return r; + } + let mut x0 = 0i32; + let mut y0 = 0i32; + let g = extended_gcd(a.abs(), b.abs(), &mut x0, &mut y0); + + if c % g != 0 { + return r; + } + + x0 *= c / g; + y0 *= c / g; + if a < 0 { x0 = -x0; } + if b < 0 { y0 = -y0; } + + r.resultExists = true; + r.n = x0; + r.m = y0; + + r +}