summaryrefslogtreecommitdiff
path: root/zap/source/amd64/mth/abs.S
blob: 4c9ed017d422a4109c04abec5b5803c88ac05156 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright 2022 Gabriel Jensen.
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

.globl zap_abs_c
.globl zap_abs_i
.globl zap_abs_l
.globl zap_abs_ll
.globl zap_abs_s
.globl zap_abs_sc

zap_abs_i:
# The input value:
#define val %edi
# Inverted value of the input:
#define inv %eax
	movl val,inv   # inv = val
	negl inv       # inv = -inv     // Invert the copy of the input value. This also tests the sign of the value.
	               # . if (inv > 0x0)
	cmovsl val,inv # val = inv      // If it was positive, just return the unmodified input.
	ret            # return val     // Otherwise, return the inverted value.

#undef val
#undef inv

zap_abs_l:
zap_abs_ll:
# The input value:
#define val %rdi
# Inverted value of the input:
#define inv %rax
	movq val,inv   # inv = val
	negq inv       # inv = -inv
	               # . if (inv > 0x0)
	cmovsq val,inv # val = inv
	ret            # return val

#undef val
#undef inv

zap_abs_s:
# The input value:
#define val %di
# Inverted value of the input:
#define inv %ax
	movw val,inv   # inv = val
	negw inv       # inv = -inv
	               # . if (inv > 0x0)
	cmovsw val,inv # val = inv
	ret            # return val

#undef val
#undef inv

zap_abs_c:
zap_abs_sc:
# The input value:
#define val  %dil
#define val2 %di
# Inverted value of the input:
#define inv  %al
#define inv2 %ax
	movb val,inv     # inv = val
	negb inv         # inv = -inv
	                 # . if (inv > 0x0)
	cmovsw val2,inv2 # val = inv
	ret              # return val

#undef val
#undef inv