shithub: hj264

Download patch

ref: 920df845198f6d995ddeaab586526ab1ec3b9ccc
parent: 6b949925658e01e06a52c95d58d16bfc31d11912
author: Sigrid Solveig Haflínudóttir <[email protected]>
date: Wed Jul 14 08:39:37 EDT 2021

add clz and bswap32 builtins

--- a/builtins.amd64.s
+++ b/builtins.amd64.s
@@ -5,3 +5,13 @@
 	MOVL n+16(FP), CX
 	CLD; REP; MOVSB
 	RET
+	
+TEXT __builtin_clz(SB),1,$0
+	BYTE $0x0F; BYTE $0xBD; BYTE $0xC5 /* BSRL RARG, AX */
+	XORL $31, AX
+	RET
+
+TEXT __builtin_bswap32(SB),1,$0
+	MOVQ RARG, AX
+	BYTE $0x0F; BYTE $0xC8 /* BSWAP32 AX */
+	RET
--- a/builtins.c
+++ b/builtins.c
@@ -1,0 +1,15 @@
+int
+__builtin_clz(unsigned int x)
+{
+	unsigned int r;
+	if(x == 0)
+		return 32;
+	for(r = 0; (x & (1UL<<31)) == 0; x <<= 1, r++);
+	return r;
+}
+
+unsigned int
+__builtin_bswap32(unsigned int x)
+{
+	return ((x >> 24) & 0xff) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | ((x & 0xff) << 24);
+}
--- a/hj264.c
+++ b/hj264.c
@@ -1,8 +1,6 @@
+#include "builtins.h"
 #define MINIH264_IMPLEMENTATION
 #define H264E_MAX_THREADS 7
-#ifdef __amd64__
-#define memcpy memcpyf
-#endif
 #include "minih264e.h"
 #include <thread.h>
 #include <bio.h>
--- a/minih264e.h
+++ b/minih264e.h
@@ -8111,6 +8111,9 @@
 /************************************************************************/
 
 #ifndef __arm__
+#if defined(__GNUC__) || defined(__clang__) || defined(__aarch64__)
+#define __clz(v) __builtin_clz(v)
+#else
 /**
 *   Count of leading zeroes
 */
@@ -8120,8 +8123,6 @@
     unsigned long nbit;
     _BitScanReverse(&nbit, v);
     return 31 - nbit;
-#elif defined(__GNUC__) || defined(__clang__) || defined(__aarch64__)
-    return __builtin_clz(v);
 #else
     unsigned clz = 32;
     assert(v);
@@ -8132,6 +8133,7 @@
     return clz;
 #endif
 }
+#endif
 #endif
 
 /**
--- a/mkfile
+++ b/mkfile
@@ -1,6 +1,6 @@
 </$objtype/mkfile
 
-CFLAGS=$CFLAGS -p -I/sys/include/npe -D__plan9__ -D__${objtype}__
+CFLAGS=$CFLAGS -p -I/sys/include/npe -D__plan9__ -D__${objtype}__ -D__clang__
 BIN=/$objtype/bin/video
 TARG=hj264