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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
/*
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/.
*/
#include <zap/priv.h>
#include <zap/math.h>
#include <stdint.h>
#if zap_priv_fastimpl
__asm__ (
".globl zap_abs_c\n"
".globl zap_abs_i\n"
".globl zap_abs_l\n"
".globl zap_abs_ll\n"
".globl zap_abs_s\n"
"zap_abs_c:\n"
/*
signed char val
*/
#if defined(sus_arch_amd64) || defined(sus_arch_ia32)
"movb %dil,%al\n"
"sarb $0x3F,%al\n"
"xorb %al,%dil\n"
"subb %al,%dil\n"
"movb %dil,%al\n"
"ret\n"
#endif
"zap_abs_i:\n"
/*
int val
*/
#if defined(sus_arch_amd64) || defined(sus_arch_ia32)
"movl %edi,%eax\n"
"sarl $0x3F,%eax\n"
"xorl %eax,%edi\n"
"subl %eax,%edi\n"
"movl %edi,%eax\n"
"ret\n"
#endif
"zap_abs_l:\n"
/*
long val
*/
#if defined(sus_arch_amd64)
"movq %rdi,%rax\n"
"sarq $0x3F,%rax\n"
"xorq %rax,%rdi\n"
"subq %rax,%rdi\n"
"movq %rdi,%rax\n"
"ret\n"
#endif
"zap_abs_ll:\n"
/*
long long val
*/
#if defined(sus_arch_amd64)
"movq %rdi,%rax\n"
"sarq $0x3F,%rax\n"
"xorq %rax,%rdi\n"
"subq %rax,%rdi\n"
"movq %rdi,%rax\n"
"ret\n"
#endif
"zap_abs_s:\n"
/*
short val
*/
#if defined(sus_arch_amd64) || defined(sus_arch_ia32)
"movw %di,%ax\n"
"sarw $0x3F,%ax\n"
"xorw %ax,%di\n"
"subw %ax,%di\n"
"movw %di,%ax\n"
"ret\n"
#endif
);
#else
#define zap_local_abs(_typ,_sufx) \
_typ zap_abs_ ## _sufx (_typ const _val) {return _val > (_typ)0x0 ? _val : -_val;}
zap_local_abs(signed char,c)
zap_local_abs(int,i)
zap_local_abs(long,l)
zap_local_abs(long long,ll)
zap_local_abs(short,s)
#endif
|