This commit is contained in:
jacekpoz 2024-03-22 12:36:37 +01:00
parent d1249eb38b
commit 1081e52ad9
Signed by: poz
SSH key fingerprint: SHA256:JyLeVWE4bF3tDnFeUpUaJsPsNlJyBldDGV/dIKSLyN8
9 changed files with 287 additions and 0 deletions

21
zad7/.ccls Normal file
View file

@ -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

7
zad7/Cargo.lock generated Normal file
View file

@ -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"

14
zad7/Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[package]
name = "zad7"
version = "0.1.0"
edition = "2021"
[lib]
name = "zad7"
crate-type = ["staticlib", "cdylib"]
[dependencies]
[features]
iter = []
rec = []

37
zad7/Makefile Normal file
View file

@ -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)

21
zad7/c/main.c Normal file
View file

@ -0,0 +1,21 @@
#include <wrapper.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc != 4) {
fprintf(stderr, "usage: %s <a> <b> <c>\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;
}

18
zad7/c/wrapper.h Normal file
View file

@ -0,0 +1,18 @@
#ifndef _JPP_L1_Z7_WRAPPER_H
#define _JPP_L1_Z7_WRAPPER_H
#include <stdbool.h>
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

98
zad7/src/iter.rs Normal file
View file

@ -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
}

2
zad7/src/lib.rs Normal file
View file

@ -0,0 +1,2 @@
#[cfg(feature="iter")] mod iter;
#[cfg(feature="rec")] mod rec;

69
zad7/src/rec.rs Normal file
View file

@ -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
}