Utkarsh Singh's Blog Emacs webfeeder.el https://utkarshsingh.xyz/blog/atom.xml 2022-06-12T15:59:08+05:30 Integer Arithmetic Utkarsh Singh

# Integer Arithmetic

2022-06-12

## Introduction

In this blog post we will try to analyse Integer Arithmetic from the perspective of an programmer. Additional, we are going to see the facilities provided by the underlying instruction set for its error detection.

## Representation

There are two different ways of integer representation, one that can only represent positive numbers and another can represent negative, zero and positive numbers.

The former is called unsigned representation, which can represent values from 0 to 2 n - 1, where `n` is the number of bits in the binary representation. For example: 12 and 15 are represented as `1100` and `1111` respectively.

The later is called signed representation. There are multiple encoding for signed representation, most common being Two's Complement representation. It is defined by interpreting the most significant bit as sign bit, having a negative weight of -2 n-1, where `n` is the number of bits in the binary representation. For example: -5 and 5 are represented as `1011` and `0111` respectively 1.

Hexadecimals numbers (or Hex) are base-16 numbers which uses digits through 0 to 9 and characters A to F to represent 16 possible values. Additionally, 4 binary numbers makes up 1 hex, here's the conversion chart:

 Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 Hex 0 1 2 3 4 5 6 7 8 9 A B C D E F

Remember this for the entirety of this post.

## Arithmetic Errors

Due to finite nature of computer arithmetic, its results are sometimes erroneous. It's two possible causes are:

1. Invalid Carry. It happens when either addition or subtraction of two numbers cause carry into or borrow from the most significant bit respectively. For example, an invalid carry looks like:

```uint8_t x = 0xff, y = 1;
printf ("0x%hhx\n", x + y);	/* Prints 0x0 */
```

Here we are declaring two 8-bit variables `x` and `y` with values Oxff (or 255) and 1 respectively. Now, on adding them we get 256, which requires at least 9-bit for its representation, but since we are using 8-bit representation we are left with 0.

2. Overflow. It happens when addition of two numbers with sign bits ON and OFF results a numbers with sign bit OFF and ON respectively. For example, consider the addition of these negative numbers:

```int8_t x = -128, y = -1;
printf ("0x%hhx\n", x + y);	/* Prints 0x7f */
```

Again, we have two 8-bit variables. On addition, we get 0x7f (or 127), which surprisingly is an positive value because -129 requires at least 9 bits in Two's complement representation which looks like: `1 0111 1111`.

Additionally, Invalid Carry leads to erroneous arithmetic of unsigned numbers whereas Overflow only affects signed numbers 2.

## Detecting Errors

Based on the above definitions of the errors, its possible to detect invalid arithmetic in C. But, in my experience, its much more effective to fallback to the capabilities of the underlying assembly language for "these" kinds of problem. Here, we will use Carry Flag and Overflow Flag, which as the name suggest, will be used to detect Invalid Carry and Overflow respectively.

For example, consider the function prototype below:

```int is_uadd_valid (unsigned long, unsigned long);
```

Function `is_uadd_valid` return 1 if unsigned addition of its arguments is valid, and 0 otherwise. It's definition in x86 assembly language looks like:

```	.globl is_uadd_valid
setnc	%al
movzbl	%al, %eax
ret
```

Here, function arguments are stored in register `%rsi` and `%rdi`. Then instruction `addq`, adds the values stored in these registers, and saves it to register `%rdi`. Next, we check the carry flag, and set register `%al` if it's NOT set. At last, we move this value to register `%eax`, the return value of our function.

Similarly, for signed addition in Two's complement form, we can use:

```	.globl is_tadd_valid	# int is_tadd_valid (long, long)
setno	%al		# Changed
movzbl	%al, %eax
ret
```

Since this is signed arithmetic, we are going to check Overflow flag, and set register `%al` if it's NOT set.

## Conclusion

The world of computer arithmetic is bit more nuanced than presented in this post, but I hope you found this useful. Next, read Computer Systems: A Programmer's Perspective, a introductory book for computer architecture for an detailed analysis of Interger Arithmetic and further advanced topics.

## Footnotes:

1

See Wikipedia's entry on Two's Complement representation.

2

Prof. Ian D. Allen notes on Carry flag and Overflow flag.

]]>
https://utkarshsingh.xyz/blog/~/Documents/project/website/blog/int-arth.html 2022-06-12T00:00:00+05:30
Symbolic Differentiation with Lisp Utkarsh Singh

# Symbolic Differentiation with Lisp

2021-08-19

Full Source Code: Exercise 2.58

## Introduction

In Mathematics, differentiation is a process of calculating a derivative, which is instantaneous rate of change of a function with respect to a input value. In Computer Science, symbolic computation refers to development of programs for manipulating mathematical expressions 1. In this post I will try to cover my symbolic differentiation program written as a part of SICP exercises 2.

## Differentiation

In this section I will try to cover the program on the basis of fundamental mathematical operations. Each sub-section will be divided into three parts: Derivative Rule, Lisp Code and REPL (Read Eval Print Loop) session. REPL itself is divided into two parts: input (denoted by `>`) and output.

Since differentiation is a reduction problem its a perfect place for recursion to work, which means, we will only have to exploit those rules to reduce problems into sub-problems. Procedure `deriv` will play a central role in our differentiation, which takes expression EXPR and variable VAR with respect to which differentiation has to be performed.

• Rule for addition: d(u+x)/dx = du/dx + dv/dx
• Rule for subtraction: d(u-x)/dx = du/dx - dv/dx
```(make-sum (deriv (addend expr) var)
(deriv (augend expr) var))

(make-difference (deriv (minuend expr) var)
(deriv (subtrahend expr) var))
```
```> (deriv '(x + 2) 'x)
1

> (deriv '(10 - x) 'x)
'(- 1)
```

### Multiplication

• Rule for multiplication: d(uv)/dx = u*dv/dx + v*du/dx
```(let ((m1 (multiplier expr))
(m2 (multiplicand expr)))
(make-sum
(make-product m1 (deriv m2 var))
(make-product m2 (deriv m1 var))))
```
```> (deriv '(10 * x + (20 - 30) * x) 'x)
0

> (deriv '(10 * x * x) 'x)
'(+ (* 10 x) (* x 10))

> (deriv '(x * x * x * x) 'x)           ; Don't worry!
'(+ (* (* x x) x) (* x (+ (* x x) (* x (+ x x)))))
```

### Exponentiation

• Rule for exponentiation: d(u n)/dx = n*u (n-1) * du/dx
```(let ((b (base expr))
(e (exponent expr)))
(make-product
(make-product e (make-exponentiation b (make-sum e -1)))
(deriv b var)))
```
```> (deriv '(x ^ 4) 'x)
'(* 4 (^ x 3))

> (deriv '(x ^ (3 + 2)) 'x)
'(* 5 (^ x 4))

> (deriv '((x ^ (3 + 2)) + 3 * x + (x ^ 10)) 'x)
'(+ (* 5 (^ x 4)) (+ 3 (* 10 (^ x 9))))
```

### Division

• Rule for division: d(u/v)/dx = (v*du/dx - u*dv/dx) / v 2
```(let ((d1 (dividend expr))
(d2 (divisor expr)))
(make-ratio
(make-difference (make-product d2 (deriv d1 var))
(make-product d1 (deriv d2 var)))
(make-exponentiation d2 2)))
```
```> (deriv '(2 / x) 'x)
'(/ (- 2) (^ x 2))

> (deriv '(2 / (x + 1)) 'x)
'(/ (- 2) (^ (+ x 1) 2))

> (deriv '( (x ^ 2) / (3 * x - 1)) 'x)
'(/ (- (* (- (* 3 x) 1) (* 2 x)) (* (^ x 2) 3)) (^ (- (* 3 x) 1) 2))
```

### Unary operations

• Chain rule: d(f(g(x)))/dx = df/dx * d(g(x))/dx
```(make-product
(deriv-unary expr)
(deriv-prefix (argument expr) var))
```
```> (deriv '(sin x) 'x)
'(cos x)

> (deriv '(sin (2 * x + 1)) 'x)
'(* (cos (+ (* 2 x) 1)) 2)

> (deriv '(sin (3 * x + 1) ^ (1 / 2)) 'x)
'(* (cos (^ (+ (* 3 x) 1) (/ 1 2))) (* (* (/ 1 2) (^ (+ (* 3 x) 1) (+ (/ 1 2) -1))) 3))
```

## Conclusion

This program is prefect in no-regards such as it cannot transform ```x * x * x``` into `x ^ 3`, `0 / x` into 0, etc. But anyway, I really enjoyed writing the program and Lisp was really helpful in the process.

Thank you!

## Footnotes:

1

See Wikipedia's page on Computer Algebra.

2

SICP is an introductory computer science textbook by professors Harold Abelson and Gerald Jay Sussman with Julie Sussman. Also see its offical website.

]]>
https://utkarshsingh.xyz/blog/~/Documents/project/website/blog/sdiff.html 2021-08-19T00:00:00+05:30
Book review: "Programming Principles And Practice Using C++" Utkarsh Singh

# Book review: "Programming Principles And Practice Using C++"

2021-07-13

## Introduction

Bjarne Stroustrup is well know for his work on C++ and OO programming. PPP (this book) works as a introductory material for learning practical programming skills using C++. After learning C (first year programming course) and Python (second year programming course) I badly wanted a language which focuses on performance and utility and C++ surely seals the deal.

## Review

PPP starts with a overview of C++ which was indeed simple and concise but the fun part really begins with Chapter 6 "Writing A Program" where authors takes a programming project of implementing a calculator.

But the author didn't stop here, he questions the quality of code and takes on the task to improve it. Then author takes on the task to explain the concepts of OO programming following with the "display model" GUI library (FLTK based) by going through the example of a Graph application.

Then the book touches on the topic of history of programming languages, testing and The C Programming language which helps to understand the incompatibility's in C/C++ programs.

As summarised above the book tries to give details of all major programming concepts and hence leads to a very comprehensive material.

## Conclusion

This book is highly recommend for a beginner C++ programmer because unlike C, C++ is a much bigger language and it will definitely help to decrease the gap between beginner/expert C++ programmer.

]]>
https://utkarshsingh.xyz/blog/~/Documents/project/website/blog/ppp.html 2021-07-13T00:00:00+05:30
Shell scripts are irreplaceable! Utkarsh Singh

# Shell scripts are irreplaceable!

2021-04-11

## Introduction

One line of shell script is worth 10,000 lines of C code.

Shell scripts are one the most important but at the same time most under appreciated aspect of Unix programming. After writing substantial about of code to imitate and replace shell scripts with other programming languages I have indeed realised that they are irreplaceable. Most programming language tries to imitate programs like `cd`, `mkdir`, `ls`, etc but fails to identify it's greatest strength, pipelines. The flow of data using pipelines feels much more natural and efficient.

Now, I will try to present the difficulties I faced in replacing shell scripts with a 'proper' programming language.

Note: Here we are referring to POSIX Shell scripts as they are easier to wrap your head around.

## Python

Even though Python is easy to use, fast to get things done and have batteries included philosophy it still fails to be a good replacements for shell scripts.

I decided to write a python program to install Arch Linux and configure it to my liking (basically bootstrap it). At first I was really happy with python modules like `os`, `pwd` and `getpass` but as time went on these abstraction proved more costly than useful.

Now here is a snippet that I used to create a Unix user account and it's equivalent version in shell script.

Note: Any of mistakes done in implementing these programs are unintentional and if you find any them please contact me.

Python version:

```  # add repodir = directory to clone all repo's
def set_repodir(home):
repodir = home + '/.local/src'
os.mkdir(repodir)
os.chown(repodir, user_info.pw_uid, pw_gid)

try:
home = '/home/' + user
'-s', '/bin/bash', user]
# make bash as default shell
set_repodir(home)
# if users exist, then modify user account
except subprocess.CalledProcessError:
usermod = ['usermod', '-a', '-G', 'wheel', user]
subprocess.run(usermod, capture_output=True, check=True)
os.mkdir(home)
os.chown(home, user_info.pw_uid, pw_gid)
set_repodir(home)
```

Shell script version (taken from LARBS):

```  adduserandpass() { \
useradd -m -g wheel -s /bin/bash "\$name" >/dev/null 2>&1 ||
usermod -a -G wheel "\$name" && mkdir -p /home/"\$name" && chown "\$name":wheel /home/"\$name"
repodir="/home/\$name/.local/src"; mkdir -p "\$repodir"; chown -R "\$name":wheel "\$(dirname "\$repodir")" ;}
```

As we can see the python version is not only more lengthy but also more error prone compared to shell script version. What will happen when `usermod` command will fail?

Now there comes some command which cannot be scripted without pipelines and these require us to use a different subroutine called `getstatusoutput` to invoke shell, execute command and get status and output back.

```  # add user's password
chpass= 'echo ' + user + ':' + password + ' | chpasswd'
output = subprocess.getstatusoutput(chpass)
# exit if output status output is not zero
if output != 0:
err_msg(output.chpass)
exit(1)
```

## Elisp (Emacs Lisp)

At first it seems confusing to use a editor's extension language to program your environment but due to extensible nature of LISP it can be even used to do so. Also I generally use Emacs as my interface to UNIX rather than a text editor.

I recently wrote a Elisp library called emount to make use of Emacs completion to mount and umount USB drives and Android using Emacs! At its core it was just using `mount` and `simple-mtpfs` to mount USB drives and Android respectively.

Here is the function I used to mount USB devices with my own `ut-common-shell-command` which is just a wrapper around Emacs `call-process` command.

```  (defun ut-common-shell-command (command &rest args)
"Run COMMAND with ARGS.
Return the exit code and output in a list."
(with-temp-buffer
(list (apply 'call-process command nil (current-buffer) nil args)
(buffer-string))))

(defun emount-mount-usb (usb-name mount-point)
"Mount usb with USB-NAME at MOUNT-POINT."
(interactive (list (emount--select-unmounted-usb (emount--get-drive-list emount--usb-drive))
(emount--select-mount-point)))
(let* ((partition-cmd
(concat "lsblk -no fstype " usb-name))
(partition-type
(string-chop-newline (shell-command-to-string partition-cmd)))
user-group result exit-code output)
(if (string-equal partition-type "vfat")
(progn
(setq result (ut-common-shell-command "sudo" "mount" "-t" "vfat"
(setq exit-code (nth 0 result))
(setq output (nth 1 result))
(if (= exit-code emount--success-code)
(message "%s mounted successfully!" usb-name)
(user-error output)))
(setq result (ut-common-shell-command "sudo" "mount" usb-name mount-point))
(setq exit-code (nth 0 result))
(setq output (nth 1 result))
(if (not (eq exit-code emount--success-code))
(user-error output)
;; TODO: test groups output
(setq user-group (nth 0 (split-string (shell-command-to-string "groups"))))
(setq result (ut-common-shell-command "sudo" "chowm" (user-login-name)
":" user-group mount-point))
(setq exit-code (nth 0 result))
(setq output (nth 1 result))
(if (= exit-code emount--success-code)
(message "%s mounted successfully!" usb-name)
(user-error output))))))
```

Again you can see I am using `(nth 0 result)` to get exit code and printing error using `(user-error output)` if exit code doesn't match `emount--success-code`.

Corresponding shell script version (taken from voidrice):

```  mountusb() { \
chosen="\$(echo "\$usbdrives" | dmenu -i -p "Mount which drive?")" || exit 1
chosen="\$(echo "\$chosen" | awk '{print \$1}')"
sudo -A mount "\$chosen" 2>/dev/null && notify-send "USB mounting" "\$chosen mounted." && exit 0
alreadymounted=\$(lsblk -nrpo "name,type,mountpoint" | awk '\$3!~/\/boot|\/home\$|SWAP/&&length(\$3)>1{printf "-not ( -path *%s -prune ) ",\$3}')
getmount "/mnt /media /mount /home -maxdepth 5 -type d \$alreadymounted"
partitiontype="\$(lsblk -no "fstype" "\$chosen")"
case "\$partitiontype" in
"vfat") sudo -A mount -t vfat "\$chosen" "\$mp" -o rw,umask=0000;;
"exfat") sudo -A mount "\$chosen" "\$mp" -o uid="\$(id -u)",gid="\$(id -g)";;
*) sudo -A mount "\$chosen" "\$mp"; user="\$(whoami)"; ug="\$(groups | awk '{print \$1}')"; sudo -A chown "\$user":"\$ug" "\$mp";;
esac
}
```

## Conclusion

In the end I will let you decide which implementation suits you better but I still think shell literacy is important and it will help you to come up with a better solution. I would also recommend you check what Master Foo wants to say about shell scripting.

]]>
https://utkarshsingh.xyz/blog/~/Documents/project/website/blog/sh.html 2021-04-11T00:00:00+05:30
Timetable with GNU Emacs Utkarsh Singh

# Timetable with GNU Emacs

2021-03-02

## Introduction

It's not a very well kept secret that Emacs is an extensible editor and can be even extended to manage your university timetable. My university distributes time table in MS (Micorsoft) Excel file format, which are pain to work with. So, I decided to use some Elisp facilities to filter, accumulate and interact with timetable's data.

## XML Escape

In order to get started, we have to first export all our data into Comma Separated Value (CSV) file format. MS Excel files can be easily converted to CSV file by a word processor such as Libreoffice.

Start by opening your timetable file with Libreoffice and by pressing `C-s` you will be prompted with a dialog, choose CSV as file extension and then exit your Libreoffice session.

## Importing CSV file

Out the box Emacs comes with several methods to import and display CSV file as plain text tables. But in this case, we will make a subtle combination of importing and filtering as the file I am going to import contains timetable for all the available batches.

Here is the section presenting all the class scheduled for Monday:

```Mon,L-18B11BI312 BI41 (RST) CR9,L-18B11CI415 BI41 (MBI) TR1,L-18B11BT313 BT41 (PNS) CR11,
,"L-18B11CI413 CS41-CS48,IT41-42 (RSU) TR1","L-18B11MA313 CS41-CS48,IT41-42 (SST) ECL9",
,,,P-18B17EC373 EC42 (MSD) ECL8,P-18B17EC373 EC42 (MSD) ECL8,,P-18B17EC371 EC41 (EMP) CL10,P-18B17EC371 EC41 (EMP) CL10,,
,,,P-18B17CI372 IT41 (PDG) CL1,P-18B17CI372 IT41 (PDG) CL1,,P-18B17BI473 BI41 (RJK) BIL,P-18B17BI473 BI41 (RJK) BIL,,
,,,P-18B17CI372 CS48 (NFK) CL3_1,P-18B17CI372 CS48 (NFK) CL3_1,,P-18B11CE413 CE41 (TNM) WORK2,P-18B11CE413 CE41 (TNM) WORK2,,
,,,P-18B17CI372 IT42 (PKJ) CL3_2,P-18B17CI372 IT42 (PKJ) CL3_2,,,P-18B17BT373 BT41 (PNS) UG2,P-18B17BT373 BT41 (PNS) UG2,
,,,P-18B17CI473 CS46 (RSU) CL4,P-18B17CI473 CS46 (RSU) CL4,,,,P-18B17CI372 CS42 (SLD) CL1,P-18B17CI372 CS42 (SLD) CL1
,,,P-18B17CI373 CS41 (HRI) CL5_1,P-18B17CI373 CS41 (HRI) CL5_1,,,,P-18B17CI372 CS41 (VKB) CL3_1,P-18B17CI372 CS41 (VKB) CL3_1
,,,P-18B17CI373 CS42 (PTK) CL5_2,P-18B17CI373 CS42 (PTK) CL5_2,,,,P-18B17CI473 CS44 (PKG) CL3_2,P-18B17CI473 CS44 (PKG) CL3_2
,,,P-18B17CI373 CS43 (EGA) CL6,P-18B17CI373 CS43 (EGA) CL6,,,,P-18B17CI473 CS43 (RKI) CL4,P-18B17CI473 CS43 (RKI) CL4
,,,P-18B17CI373 CS44 (HES) CL7,P-18B17CI373 CS44 (HES) CL7,,,,P-18B17CI373 CS46 (PMI) CL5_1,P-18B17CI373 CS46 (PMI) CL5_1
,,,P-18B17CI373 CS45 (MBI) CL8,P-18B17CI373 CS45 (MBI) CL8,,,,P-18B17CI373 CS47 (AMN) CL5_2,P-18B17CI373 CS47 (AMN) CL5_2
,,,P-18B17CI473 CS47 (MSH) CL9_1,P-18B17CI473 CS47 (MSH) CL9_1,,,,P-18B17CI373 CS48 (JAG) CL6,P-18B17CI373 CS48 (JAG) CL6
,,,P-18B11CI474 BI41 (SJT) CL11,P-18B11CI474 BI41 (SJT) CL11,,,,P-18B17CI373 IT41 (HJL) CL7,P-18B17CI373 IT41 (HJL) CL7
,,,P-18B17CE472 CE41 (SUB) ENVLAB,P-18B17CE472 CE41 (SUB) ENVLAB,,,,P-18B17CI373 IT42 (AKJ) CL8,P-18B17CI373 IT42 (AKJ) CL8
,,,,,,,,P-18B17CI473 CS45 (ARV) CL9_1,P-18B17CI473 CS45 (ARV) CL9_1
```

### Ttable.el

Ttable is timetable editing utilies for Org mode. It takes CSV file and current batch as inputs and output Org mode tables. Here is a screenshot: