Sigrid Solveig Haflínudóttirtruncate: use trunc, fix NaN and Inf casesc47cb9d1 year agoHistory
166 lines · 3.8 KB · C
RawDownload
#pragma once #include <u.h>#include <libc.h>#include <ctype.h>#include <mp.h> #ifdef NDEBUG#undef assert#define assert(x)#define static_assert(a,b)#else#define static_assert_cat(a, b) a##b#define static_assert_name(line) static_assert_cat(static_assert_, line)#define static_assert(must_be_true, message) \	static const void *static_assert_name(__LINE__) \		[must_be_true ? 2 : -1] = { \			message, \			static_assert_name(__LINE__), \		}#endif #define __os_name__ "plan9" int sl_popcount(unsigned int w);int sl_clz(unsigned int x); typedef jmp_buf sl_jmp_buf;#define sl_setjmp(e) setjmp((e))#define sl_longjmp(e, v) longjmp((e), (v)) #define sys_setcallpc(arg) do{sl.cpc = getcallerpc(&arg);}while(0)void sys_setcurrpc(int dummy); extern double D_PNAN, D_PINF; #if defined(__amd64__) || \    defined(__arm64__) || \    defined(__mips64__) || \    defined(__power64__) || \    defined(__sparc64__)#define BITS64#define PRIdPTR PRId64#define PRIuPTR PRIu64#define PRIxPTR PRIux64typedef long long ssize;#else#define PRIdPTR "ld"#define PRIuPTR "lud"#define PRIxPTR "lux"typedef long ssize;#endif #if defined(__386__) || defined(__amd64__) || defined(__arm64__)#define MEM_UNALIGNED_ACCESS#endif int unsetenv(const char *name);#define setenv(name, val, overwrite) putenv(name, val)#define isinf(x) isInf(x, 0)#define isnan(x) isNaN(x) #define getcwd getwd#define vsnprintf vsnprint#define vdprintf vfprint#define snprintf snprint#define strcasecmp cistrcmp#define lseek seek#define towupper toupperrune#define towlower tolowerrune#define iswalpha isalpharune#define signbit(r) ((*(u64int*)&(r)) & (1ULL<<63))#define isfinite(d) (((*(u64int*)&(d))&0x7ff0000000000000ULL) != 0x7ff0000000000000ULL)double trunc(double x); #define PRId32 "d"#define PRIu32 "ud"#define PRIx32 "x"#define PRId64 "lld"#define PRIu64 "llud"#define PRIx64 "llx"#define PRIux64 "llux" #define INT32_MAX 0x7fffffff#define UINT32_MAX 0xffffffffU#define INT32_MIN (-INT32_MAX-1)#define INT64_MIN ((s64int)0x8000000000000000ULL)#define INT64_MAX 0x7fffffffffffffffLL#define UINT64_MAX 0xffffffffffffffffULL#define ULONG_MAX UINT32_MAX#define DBL_MANT_DIG 53#define FLT_MANT_DIG 24 #define PATHSEP '/'#define PATHSEPSTRING "/"#define PATHLISTSEP ':'#define PATHLISTSEPSTRING ":"#define ISPATHSEP(c) ((c) == '/') enum {	SEEK_SET,	SEEK_CUR,	SEEK_END, 	STDIN_FILENO = 0,	STDOUT_FILENO,	STDERR_FILENO,}; #define O_RDWR ORDWR#define O_WRONLY OWRITE#define O_RDONLY OREAD#define O_TRUNC OTRUNC#define F_OK 0 #define LITTLE_ENDIAN 1234#define BIG_ENDIAN 4321 #if defined(__mips__) || \    defined(__power__) || defined(__power64__) || \    defined(__sparc__) || defined(__sparc64__)#define BYTE_ORDER BIG_ENDIAN#else#define BYTE_ORDER LITTLE_ENDIAN#endif typedef vlong soffset;typedef enum { false, true } bool; int ftruncate(int f, soffset sz); #define sl_unlikely(x) (x)#define sl_likely(x) (x)#define sl_printfmt(x, y)#define sl_thread(x) x#define sl_prefetch(x)#define sl_constfn#define sl_purefn#define sl_hotfn#define sl_returns_nonnull#define sl_nonnull(...)#define sl_aligned(x) /* FIXME(sigrid): s*_overflow_* can be more optimal */#define sadd_overflow_64(a, b, c) ( \  (b < 1) ? \  ((INT64_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \  ((INT64_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \)#define smul_overflow_64(a, b, c) ( \	((a)>0 ? ((b)>0 ? (a)>INT64_MAX/(b) : (b)<INT64_MIN/(a)) \	       : ((b)>0 ? (a)<INT64_MIN/(b) : ((a)!=0 && (b)<INT64_MAX/(a)))) \	? 1 \	: ((*(c)=(a)*(b)), 0) \)#if defined(BITS64)#define sadd_overflow(a, b, c) sadd_overflow_64(a, b, c)#else#define sadd_overflow(a, b, c) ( \  (b < 1) ? \  ((INT32_MAX-(b) <= (a)) ? ((*(c)=(a)+(b)), 0) : 1) : \  ((INT32_MAX-(b) >= (a)) ? ((*(c)=(a)+(b)), 0) : 1) \)#endif #include "mem.h"

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