History

add c_builtins*

Sigrid Solveig Haflínudóttir committed 5 years ago

Commit 7192dd81 parent 78e765a3 files · +43 −0Browse files

Unified · Split

3 files
  1. README.md +1 −0
  2. c_builtins.c +29 −0
  3. c_builtins_amd64.s +13 −0
README.md+1−0
@@ -3,6 +3,7 @@ · 2 unmodified lines
3Random snippets of code that are in public domain.3Random snippets of code that are in public domain.
4Just copy and use for whatever you want.4Just copy and use for whatever you want.
5 5
6* `c_builtins*` a few __builtin_* for Plan 9, useful for porting other software
6* `lrint.c` lrint* implementation7* `lrint.c` lrint* implementation
7* `msr.c` MSR reading tool8* `msr.c` MSR reading tool
8* `nanosec.c` nanosec(), a replacement for (way more expensive) nsec()9* `nanosec.c` nanosec(), a replacement for (way more expensive) nsec()
c_builtins.c+29−0
@@ -0,0 +1,29 @@
1int
2__builtin_ctz(unsigned int x)
3{
4 unsigned int r;
5 if(x == 0)
6 return 32;
7 for(r = 0; (x & 1) == 0; x >>= 1, r++);
8 return r;
9}
10
11int
12__builtin_clz(unsigned int x)
13{
14 unsigned int r;
15 if(x == 0)
16 return 32;
17 for(r = 0; (x & (1UL<<31)) == 0; x <<= 1, r++);
18 return r;
19}
20
21int
22__builtin_clzll(unsigned long long x)
23{
24 unsigned long long r;
25 if(x == 0)
26 return 64;
27 for(r = 0; (x & (1ULL<<63)) == 0; x <<= 1, r++);
28 return r;
29}
c_builtins_amd64.s+13−0
@@ -0,0 +1,13 @@
1TEXT __builtin_ctz(SB),1,$0
2 BYTE $0x0F; BYTE $0xBC; BYTE $0xC5 /* BSFL RARG, AX */
3 RET
4
5TEXT __builtin_clz(SB),1,$0
6 BYTE $0x0F; BYTE $0xBD; BYTE $0xC5 /* BSRL RARG, AX */
7 XORL $31, AX
8 RET
9
10TEXT __builtin_clzll(SB),1,$0
11 BYTE $0x48; BYTE $0x0F; BYTE $0xBD; BYTE $0xC5 /* BSRQ RARG, AX */
12 XORL $63, AX
13 RET

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t