-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
40 lines (36 loc) · 793 Bytes
/
test.c
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
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
// uintX_t __absX(intX_t v) (pseudocode):
// {
// uintX_t r;
// const intX_t mask = v >> (sizeof(intX_t) * 8 - 1);
// r = (v ^ mask) - mask;
// return r;
// }
// (use SyS-V ABI)
asm (
".section .text\n"
".global __abs8\n"
".type __abs8, @function\n"
".balign 16\n"
"__abs8:\n"
" mov %edi, %edx\n"
" sar $0x7, %dl\n"
" xor %dl, %dil\n"
);
extern uint8_t __abs8(int8_t v);
extern uint8_t __abs8_gcc(int8_t v)
{
uint8_t r;
const int8_t mask = v >> 7;
r = (v ^ mask) - mask;
return r;
}
int main(int argc, char const *argv[])
{
int8_t v = -128;
printf("abs(%d) = %u\n", v, __abs8(v));
printf("abs_gcc(%d) = %u\n", v, __abs8_gcc(v));
return 0;
}