Skip to content
Snippets Groups Projects
Commit 1b4653c6 authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Notizen und Beispiele 22.6.2023

parent 01cd748b
No related branches found
No related tags found
No related merge requests found
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.
#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");
}
}
}
#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;
}
#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;
}
#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;
}
#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;
}
#include <stdio.h>
#include <stdint.h>
int main (void)
{
volatile int test = 64;
printf ("rshift %lx\n", ((uint64_t) 5) >> test);
return 0;
}
.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
#include <stdio.h>
#include <stdint.h>
int main (void)
{
int test = 64;
printf ("rshift %lx\n", ((uint64_t) 5) >> test);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment