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
33 Random snippets of code that are in public domain.
44 Just copy and use for whatever you want.
55
6+* `c_builtins*` a few __builtin_* for Plan 9, useful for porting other software
67 * `lrint.c` lrint* implementation
78 * `msr.c` MSR reading tool
89 * `nanosec.c` nanosec(), a replacement for (way more expensive) nsec()
c_builtins.c+29−0
@@ -0,0 +1,29 @@
1+int
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+
11+int
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+
21+int
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 @@
1+TEXT __builtin_ctz(SB),1,$0
2+ BYTE $0x0F; BYTE $0xBC; BYTE $0xC5 /* BSFL RARG, AX */
3+ RET
4+
5+TEXT __builtin_clz(SB),1,$0
6+ BYTE $0x0F; BYTE $0xBD; BYTE $0xC5 /* BSRL RARG, AX */
7+ XORL $31, AX
8+ RET
9+
10+TEXT __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