From c056e12b7ffdfb91c5ae01aabb41ce72c2085cf5 Mon Sep 17 00:00:00 2001 From: fredrikr79 Date: Tue, 26 Aug 2025 16:59:37 +0200 Subject: [PATCH] ex0: theory document --- .gitignore | 1 + exercise0/Makefile | 3 ++ exercise0/main.typ | 126 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 exercise0/main.typ diff --git a/.gitignore b/.gitignore index 990496c..b903888 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,4 @@ dkms.conf *.zip *.pdf +*.png diff --git a/exercise0/Makefile b/exercise0/Makefile index 6c7a5dc..dfe8f0a 100644 --- a/exercise0/Makefile +++ b/exercise0/Makefile @@ -8,3 +8,6 @@ run: show: make run feh after.bmp + +convert: + magick after.bmp after.png # for showing image in pdf diff --git a/exercise0/main.typ b/exercise0/main.typ new file mode 100644 index 0000000..a42a125 --- /dev/null +++ b/exercise0/main.typ @@ -0,0 +1,126 @@ +#import "@preview/cetz:0.3.2"; +#import "@preview/cetz-plot:0.1.1": plot +#import "@preview/physica:0.9.4": * +#import "@preview/plotsy-3d:0.1.0": plot-3d-parametric-surface +#import "@preview/fletcher:0.5.4" as fletcher: diagram, edge, node + +#set page(paper: "a4", margin: (x: 2.6cm, y: 2.8cm), numbering: "1 : 1") +#set par(justify: true, leading: 0.52em) + +#let FONT_SIZE = 18pt; +#set text(font: "FreeSerif", size: FONT_SIZE, lang: "us") +#show math.equation: set text(font: "Euler Math", size: (FONT_SIZE * 1.0), lang: "en") + +#set heading(numbering: none) +#show heading.where(level: 1): it => { + rect(inset: FONT_SIZE / 2)[#it] +} + +// Dracula palette +#let bg = rgb("#282a36") +#let fg = rgb("#f8f8f2") +#let sec = rgb("#44475a") + +// Inline (aligned to surrounding text) +#show raw.where(block: false): it => box( + fill: sec, + inset: (x: 5pt, y: 3pt, top: 3pt), // extra top inset + baseline: 2.75pt, // lowers box + radius: 2.5pt, + text(fill: fg, size: 0.88em, it), +) + +// Block (unchanged) +#show raw.where(block: true): it => block( + fill: bg, + inset: 10pt, + radius: 4pt, + text(fill: fg, font: "Fira Code", it), +) + + +#align(center)[ + #text(size: FONT_SIZE * 2, weight: "bold")[#underline[exercise 0]] +] + +these are my solutions to the first (optional) exercise set of TDT4200. + +i recommend using a PDF-reader like +#link("https://wiki.archlinux.org/title/Zathura")[#text(blue.darken(5%))[zathura]], +for no particular reason other than that it is cool. + +this document was created using +#link("https://typst.app/")[#text(blue.darken(5%))[typst]]. + +#v(42pt) + +#outline(title: none) + +#v(42pt) + + += how to run my code + +the project utilizes +#link("https://nixos.org/")[#text(blue.darken(5%))[nix]] +to manage dependencies. + +with it (and with +#link("https://wiki.nixos.org/wiki/Flakes")[#text(blue.darken(5%))[flakes]] +enabled), you can simply run `nix develop` to enter a development shell with the +necessary dependencies, as specified in `flake.nix` and `flake.lock`. in this +shell you can build the bmp-image using `make run` or to see it immediately, +using +#link("https://wiki.archlinux.org/title/Feh")[#text(blue.darken(5%))[feh]], +you can run `make show`. + +#v(100%) + += theory + +== 1) what is the purpose of a Makefile? + +a Makefile is like a shell-script that performs some pre-defined tasks. it is +particularly useful as a *build tool* for complex compilation pipelines in +development environments relying on such compiled languages like C. + +`make show` is an example of a neat short-hand to compile and run my program, +but also finally displaying the image result to me using the lightweight CLI +feh. + +== 2) what is a pointer in C? + +a *pointer* in C is a memory address equipped with a primitive type, determining +the semantics of *pointer arithmetic*, i.e. when you increment the pointer. for +example, if you increment a pointer with a type of size 8 bytes, it will jump to +the memory address 8 bytes forward. a *void pointer* is a true wildcard pointer, +having 'no type', thus being able to point to any memory, disregarding +alignment. + +== 3) what does the compile flag `-O3` do? + +GCC, like most compilers, have lots of flags that alter their behavior. given +the age of C and the amount of clever people in the world that have dedicated +time to crafting a perfect compiler, GCC is capable of analyzing the +code to perform optimizations. `gcc -O3 source.c` means to compile a source file +with *aggressive optimizations*. examples of this could be unrolling loops, +recognizing patterns or using more specialized (and faster) instructions (if +available). + +note that there have been examples of compiler optimizations introducing weird +bugs (i think), due to slight alterings of internal semantics. + +== 4) how do you pass a value by reference? + +passing a value by reference is the preferred way of handling data transfer in +C, as opposed to other languages, where data may be passed to a function as +a *pure copy*. copying memory is expensive, both time-wise and space-wise. it +scales linearly, so thus if you have millions of data points, and you wish to +pass it around lots, copying it each time could be considered unoptimal. that's +why you wish to refer to your data using pointers, also known as *references*. + += じゃああ、またね。 + +#align(center)[ + #image("after.png", width: 80%) +]