From 1b4653c63fb6dd9677776a5f83c262d30dd593d2 Mon Sep 17 00:00:00 2001 From: Peter Gerwinski <peter.gerwinski@hs-bochum.de> Date: Thu, 22 Jun 2023 16:40:39 +0200 Subject: [PATCH] Notizen und Beispiele 22.6.2023 --- 20230622/ad-20230622.txt | 102 +++++++++++++++++++++++++++++++++++++++ 20230622/product-01.c | 40 +++++++++++++++ 20230622/product-02.c | 44 +++++++++++++++++ 20230622/product-03.c | 44 +++++++++++++++++ 20230622/shift-01.c | 11 +++++ 20230622/shift-02.c | 11 +++++ 20230622/shift-03.c | 9 ++++ 20230622/shift-03.s | 29 +++++++++++ 20230622/shift-04.c | 9 ++++ 9 files changed, 299 insertions(+) create mode 100644 20230622/ad-20230622.txt create mode 100644 20230622/product-01.c create mode 100644 20230622/product-02.c create mode 100644 20230622/product-03.c create mode 100644 20230622/shift-01.c create mode 100644 20230622/shift-02.c create mode 100644 20230622/shift-03.c create mode 100644 20230622/shift-03.s create mode 100644 20230622/shift-04.c diff --git a/20230622/ad-20230622.txt b/20230622/ad-20230622.txt new file mode 100644 index 0000000..fb190fb --- /dev/null +++ b/20230622/ad-20230622.txt @@ -0,0 +1,102 @@ +Langzahlarithmetik, 22.06.2023, 14:10:33 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +:) Langzahl-Addition +:o Langzahl-Vergleich + o Langzahl-Subtraktion --> Wie Addition (mit Übertrag) + o Langzahl-Modulo-Addition + - Langzahl-Multiplikation + - Langzahl-Modulo-Multiplikation + - Langzahl-Modulo-Subtraktion + - Langzahl-Division mit Rest + +Algorithmus für Langzahl-Modulo-Addition? 22.06.2023, 14:14:03 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Beispiel: modulo 5 + 4 + 1 = 0 + 4 + 2 = 1 + 4 + 4 = 3 + +Geht immer: (a + b) % N + +Algorithmus: + + c = a + b; + if (c >= N) // "if" genügt, weil a + b höchstens 2 · (N - 1) sein kann + c -= N; + +Algorithmus für Langzahl-Vergleich? 22.06.2023, 14:19:15 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Aufgabe: Ist a <= b? + + b positiv b negativ + + a positiv s.u. a > b + + a negativ a < b Ist -a >= -b? + +Ohne Beschränkung der Allgemeinheit (o.B.d.A.): Alle Zahlen sind positiv. + + Welche Zahl hat das höhere Bit gesetzt? + Diese Zahl ist die größere. + + Wenn beide gleich: + + Binäre Subtraktion, + prüfen, ob das oberste Bit gesetzt ist + + oder: + + Von der höchsten Ziffer bis zur niedrigsten durchgehen. + Sobald eine Ziffer größer ist, ist die Zahl größer. + +Algorithmus für Langzahl-Multiplikation? 22.06.2023, 14:26:33 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Zähler-Variable, immer wieder addieren ... + + c = a * b: + + c = 0; + for (int i = 0; i < b; i++) + c += a; + +--> O(b) + +Beispiel: + + 1234513247519832645912938749631409478172389649182351 * 123406123785012784618723058163 + = 152346494637670594525194299058889621869556846777469948747518602026934923166081213 + +Stattdessen: schriftlich. + +--> O(log b) + +Beispiel: + + 14 * 6 = 84 + + 1110 * 110 + ---------- + 1110 2 Additionen + 1110 = 1 weniger, als b Binärziffern hat + ---111---- + 101010 + 0 + ---------- + 1010100 + +Speicherplatzbedarf: log c = log a + log b + +--> Ich benötige für c höchstens so viele Ziffern, wie a und b gemeinsam haben. + +Beispiel: 4-Bit-Multiplikation: höchstens 1111 * 1111 = 11100001 = 225_10 < 11111111 = 255_10 + +Aufgabe: Implementieren Sie einen Algorithmus für Langzahl-Multiplikation. +Hinweis: Rechnen Sie b binär. Ein- und Ausgabe hexadezimal. + +Beobachtung: Bit-Shifting in C, 22.06.2023, 16:38:51 +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +https://www.gnu.org/software/gnu-c-manual/gnu-c-manual.html#Bit-Shifting + + For both << and >>, if the second operand is greater than the bit-width + of the first operand, or the second operand is negative, the behavior is + undefined. diff --git a/20230622/product-01.c b/20230622/product-01.c new file mode 100644 index 0000000..71cf5d1 --- /dev/null +++ b/20230622/product-01.c @@ -0,0 +1,40 @@ +#include "product.h" +#include "sum.h" +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> + +void product(uintN_t a, uintN_t b, uintN_t result){ + uintN_t tmp; + for (int i = 0; i < N; i++) { + tmp[i] = 0; + } + volatile int test = 0; + for(uint64_t i = 0; i < N * 64; i++){ + if(b[i / 64] & ((uint64_t) 1 << (i & 0x3F))){ + tmp[(i / 64)] = a[0] << (i & 0x3F); + printf("weewoo %lx\n",(i & 0x3F)); + printf("a0 %lx\n", a[0]); + printf("differnez %ld\n", (64 - (i & 0x3F))); + printf("rshift %lx\n", (uint64_t) a[0] >> (64 - (i & 0x3F))); + int tomp = 64 - (i & 0x3F); + printf("topm: %d\n", tomp); + printf("rshift %lx\n", ((uint64_t) 5) >> (64 - (test))); + printf("rshift %lx\n", ((uint64_t) 5) >> tomp); + printf("rshift_sanity %lx\n", ((uint64_t) 5) >> (64 - 0)); + for (int j = 1; j < N; j++) { + if(j + (i / 64) >= N) break; + tmp[j + (i / 64)] = a[j] << (i & 0x3F)| a[j-1] >> (64 - (i & 0x3F)); + } + sum(tmp, result, result); + for (int i = 0; i < N; i++) { + tmp[i] = 0; + } + printf("sum\n"); + for(int i = N-1; i >= 0; i--){ + printf("%016lX ", result[i]); + } + printf("\n"); + } + } +} diff --git a/20230622/product-02.c b/20230622/product-02.c new file mode 100644 index 0000000..5473210 --- /dev/null +++ b/20230622/product-02.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdint.h> + +#define N 2 + +int main (void) +{ + uint64_t a[N] = { 5, 0 }; + uint64_t b[N] = { 1, 1 }; +// uint64_t result[N] = { 0, 0 }; + uint64_t tmp[N]; + for (int i = 0; i < N; i++) { + tmp[i] = 0; + } + volatile int test = 0; + for(uint64_t i = 0; i < N * 64; i++){ + if(b[i / 64] & ((uint64_t) 1 << (i & 0x3F))){ + tmp[(i / 64)] = a[0] << (i & 0x3F); + printf("weewoo %lx\n",(i & 0x3F)); + printf("a0 %lx\n", a[0]); + printf("differnez %ld\n", (64 - (i & 0x3F))); + printf("rshift %lx\n", (uint64_t) a[0] >> (64 - (i & 0x3F))); + int tomp = 64 - (i & 0x3F); + printf("topm: %d\n", tomp); + printf("rshift %lx\n", ((uint64_t) 5) >> (64 - (test))); + printf("rshift %lx\n", ((uint64_t) 5) >> tomp); + printf("rshift_sanity %lx\n", ((uint64_t) 5) >> (64 - 0)); + for (int j = 1; j < N; j++) { + if(j + (i / 64) >= N) break; + tmp[j + (i / 64)] = a[j] << (i & 0x3F)| a[j-1] >> (64 - (i & 0x3F)); + } +// sum(tmp, result, result); +// for (int i = 0; i < N; i++) { +// tmp[i] = 0; +// } +// printf("sum\n"); +// for(int i = N-1; i >= 0; i--){ +// printf("%016lX ", result[i]); +// } + printf("\n"); + } + } + return 0; +} diff --git a/20230622/product-03.c b/20230622/product-03.c new file mode 100644 index 0000000..a6c5d3e --- /dev/null +++ b/20230622/product-03.c @@ -0,0 +1,44 @@ +#include <stdio.h> +#include <stdint.h> + +#define N 2 + +int main (void) +{ + uint64_t a[N] = { 5, 0 }; + uint64_t b[N] = { 1, 1 }; +// uint64_t result[N] = { 0, 0 }; + uint64_t tmp[N]; + for (int i = 0; i < N; i++) { + tmp[i] = 0; + } + volatile int test = 64; + for(uint64_t i = 0; i < N * 64; i++){ + if(b[i / 64] & ((uint64_t) 1 << (i & 0x3F))){ + tmp[(i / 64)] = a[0] << (i & 0x3F); + printf("weewoo %lx\n",(i & 0x3F)); + printf("a0 %lx\n", a[0]); + printf("differnez %ld\n", (64 - (i & 0x3F))); + printf("rshift %lx\n", (uint64_t) a[0] >> (64 - (i & 0x3F))); + int tomp = 64 - (i & 0x3F); + printf("topm: %d\n", tomp); + printf("rshift %lx\n", ((uint64_t) 5) >> test); + printf("rshift %lx\n", ((uint64_t) 5) >> tomp); + printf("rshift_sanity %lx\n", ((uint64_t) 5) >> (64 - 0)); + for (int j = 1; j < N; j++) { + if(j + (i / 64) >= N) break; + tmp[j + (i / 64)] = a[j] << (i & 0x3F)| a[j-1] >> (64 - (i & 0x3F)); + } +// sum(tmp, result, result); +// for (int i = 0; i < N; i++) { +// tmp[i] = 0; +// } +// printf("sum\n"); +// for(int i = N-1; i >= 0; i--){ +// printf("%016lX ", result[i]); +// } + printf("\n"); + } + } + return 0; +} diff --git a/20230622/shift-01.c b/20230622/shift-01.c new file mode 100644 index 0000000..fd70db3 --- /dev/null +++ b/20230622/shift-01.c @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +int main (void) +{ + uint64_t a0 = 5; + int tomp = 64; + printf ("%" PRId64 "\n", a0 >> tomp); + return 0; +} diff --git a/20230622/shift-02.c b/20230622/shift-02.c new file mode 100644 index 0000000..59244b2 --- /dev/null +++ b/20230622/shift-02.c @@ -0,0 +1,11 @@ +#include <stdio.h> +#include <stdint.h> +#include <inttypes.h> + +int main (void) +{ + uint64_t a0 = 5; + int tomp = 0; + printf ("%" PRId64 "\n", a0 >> (64 - tomp)); + return 0; +} diff --git a/20230622/shift-03.c b/20230622/shift-03.c new file mode 100644 index 0000000..060db1e --- /dev/null +++ b/20230622/shift-03.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <stdint.h> + +int main (void) +{ + volatile int test = 64; + printf ("rshift %lx\n", ((uint64_t) 5) >> test); + return 0; +} diff --git a/20230622/shift-03.s b/20230622/shift-03.s new file mode 100644 index 0000000..dce8724 --- /dev/null +++ b/20230622/shift-03.s @@ -0,0 +1,29 @@ + .file "shift-03.c" + .text + .section .rodata.str1.1,"aMS",@progbits,1 +.LC0: + .string "rshift %lx\n" + .text + .globl main + .type main, @function +main: +.LFB11: + .cfi_startproc + subq $24, %rsp + .cfi_def_cfa_offset 32 ; #include <stdio.h> + movl $64, 12(%rsp) ; #include <stdint.h> + movl 12(%rsp), %ecx ; + movl $5, %esi ; int main (void) + shrq %cl, %rsi ; { + leaq .LC0(%rip), %rdi ; volatile int test = 64; + movl $0, %eax ; printf ("rshift %lx\n", ((uint64_t) 5) >> test); + call printf@PLT ; return 0; + movl $0, %eax ; } + addq $24, %rsp + .cfi_def_cfa_offset 8 + ret + .cfi_endproc +.LFE11: + .size main, .-main + .ident "GCC: (Debian 8.3.0-6) 8.3.0" + .section .note.GNU-stack,"",@progbits diff --git a/20230622/shift-04.c b/20230622/shift-04.c new file mode 100644 index 0000000..a44c44d --- /dev/null +++ b/20230622/shift-04.c @@ -0,0 +1,9 @@ +#include <stdio.h> +#include <stdint.h> + +int main (void) +{ + int test = 64; + printf ("rshift %lx\n", ((uint64_t) 5) >> test); + return 0; +} -- GitLab