From 74d48f7cb6d03ff1cfad85c4c15527b8f5ca1861 Mon Sep 17 00:00:00 2001
From: Justine Tunney <jtunney@gmail.com>
Date: Thu, 4 Feb 2021 16:55:56 -0800
Subject: [PATCH] Definen LLP64 data model for code completion

This change should not be interpreted as an intent to support MSVC which
appears to live on in VSCode IntelliSense for what's dead will never die
---
 .vscode/vscode.h            |  1 -
 libc/bits/atomic_load.c     | 21 ++++++++--
 libc/bits/atomic_store.c    | 18 ++++++++-
 libc/integral/llp64.inc     | 79 +++++++++++++++++++++++++++++++++++++
 libc/integral/normalize.inc |  2 +
 5 files changed, 114 insertions(+), 7 deletions(-)
 create mode 100644 libc/integral/llp64.inc

diff --git a/.vscode/vscode.h b/.vscode/vscode.h
index 570eebc6c..129587b68 100644
--- a/.vscode/vscode.h
+++ b/.vscode/vscode.h
@@ -1,5 +1,4 @@
 #define __VSCODE_INTELLISENSE__ 1
-#define __LP64__                /* TODO: this is a lazy kludge */
 #include "libc/integral/normalize.inc"
 
 #if 0
diff --git a/libc/bits/atomic_load.c b/libc/bits/atomic_load.c
index 11922c52d..fbfcae7ff 100644
--- a/libc/bits/atomic_load.c
+++ b/libc/bits/atomic_load.c
@@ -27,8 +27,21 @@
  * optimizations.
  */
 intptr_t(atomic_load)(void *p, size_t n) {
-  intptr_t x;
-  x = 0;
-  memcpy(&x, p, MAX(n, sizeof(x)));
-  return x;
+  intptr_t x = 0;
+  switch (n) {
+    case 1:
+      __builtin_memcpy(&x, p, 1);
+      return x;
+    case 2:
+      __builtin_memcpy(&x, p, 2);
+      return x;
+    case 4:
+      __builtin_memcpy(&x, p, 4);
+      return x;
+    case 8:
+      __builtin_memcpy(&x, p, 8);
+      return x;
+    default:
+      return 0;
+  }
 }
diff --git a/libc/bits/atomic_store.c b/libc/bits/atomic_store.c
index c84821b40..8075f8b89 100644
--- a/libc/bits/atomic_store.c
+++ b/libc/bits/atomic_store.c
@@ -27,6 +27,20 @@
  * optimizations.
  */
 intptr_t(atomic_store)(void *p, intptr_t x, size_t n) {
-  memcpy(p, &x, MAX(n, sizeof(x)));
-  return x;
+  switch (n) {
+    case 1:
+      __builtin_memcpy(p, &x, 1);
+      return x;
+    case 2:
+      __builtin_memcpy(p, &x, 2);
+      return x;
+    case 4:
+      __builtin_memcpy(p, &x, 4);
+      return x;
+    case 8:
+      __builtin_memcpy(p, &x, 8);
+      return x;
+    default:
+      return 0;
+  }
 }
diff --git a/libc/integral/llp64.inc b/libc/integral/llp64.inc
new file mode 100644
index 000000000..36c224199
--- /dev/null
+++ b/libc/integral/llp64.inc
@@ -0,0 +1,79 @@
+#define __INT8_MAX__    0x7f
+#define __UINT8_MAX__   0xff
+#define __INT16_MAX__   0x7fff
+#define __UINT16_MAX__  0xffff
+#define __SHRT_MAX__    0x7fff
+#define __INT_MAX__     0x7fffffff
+#define __INT32_MAX__   0x7fffffff
+#define __UINT32_MAX__  0xffffffffu
+#define __INT64_MAX__   0x7fffffffffffffffl
+#define __UINT64_MAX__  0xffffffffffffffffull
+#define __SIZE_MAX__    0xffffffffffffffffull
+#define __INTPTR_MAX__  0x7fffffffffffffffll
+#define __UINTPTR_MAX__ 0xffffffffffffffffull
+#define __WINT_MAX__    0xffffffffu
+
+#define __SIZEOF_SHORT__       2
+#define __SIZEOF_INT__         4
+#define __SIZEOF_LONG__        4
+#define __SIZEOF_LONG_LONG__   8
+#define __SIZEOF_POINTER__     8
+#define __SIZEOF_PTRDIFF_T__   8
+#define __SIZEOF_SIZE_T__      4
+#define __SIZEOF_WCHAR_T__     4
+#define __SIZEOF_WINT_T__      4
+#define __SIZEOF_FLOAT__       4
+#define __SIZEOF_FLOAT128__    16
+#define __SIZEOF_DOUBLE__      8
+#define __SIZEOF_FLOAT80__     16
+#define __SIZEOF_LONG_DOUBLE__ 16
+
+#define __INT8_C(c)   c
+#define __UINT8_C(c)  c
+#define __INT16_C(c)  c
+#define __UINT16_C(c) c
+#define __INT32_C(c)  c
+#define __UINT32_C(c) c##U
+#define __INT64_C(c)  c##LL
+#define __UINT64_C(c) c##ULL
+
+#if !(__ASSEMBLER__ + __LINKER__ + 0)
+
+#define __INT8_TYPE__    signed char
+#define __UINT8_TYPE__   unsigned char
+#define __INT16_TYPE__   short int
+#define __UINT16_TYPE__  short unsigned int
+#define __INT32_TYPE__   int
+#define __UINT32_TYPE__  unsigned int
+#define __INT64_TYPE__   long long int
+#define __UINT64_TYPE__  long long unsigned int
+#define __INTPTR_TYPE__  long long int
+#define __UINTPTR_TYPE__ long long unsigned int
+#define __PTRDIFF_TYPE__ long long int
+#define __SIZE_TYPE__    unsigned int
+#define __WCHAR_TYPE__   int
+#define __CHAR16_TYPE__  short unsigned int
+#define __CHAR32_TYPE__  unsigned int
+#define __WINT_TYPE__    unsigned int
+#define __CHAR16_TYPE__  short unsigned int
+#define __WCHAR_TYPE__   int
+#define __CHAR32_TYPE__  unsigned int
+
+#define __INT_LEAST8_TYPE__   __INT8_TYPE__
+#define __UINT_LEAST8_TYPE__  __UINT8_TYPE__
+#define __INT_LEAST16_TYPE__  __INT32_TYPE__
+#define __UINT_LEAST16_TYPE__ __UINT16_TYPE__
+#define __INT_LEAST32_TYPE__  __INT16_TYPE__
+#define __UINT_LEAST32_TYPE__ __UINT32_TYPE__
+#define __INT_LEAST64_TYPE__  __INT64_TYPE__
+#define __UINT_LEAST64_TYPE__ __UINT64_TYPE__
+#define __INT_FAST8_TYPE__    __INT8_TYPE__
+#define __UINT_FAST8_TYPE__   __UINT8_TYPE__
+#define __INT_FAST16_TYPE__   __INT32_TYPE__
+#define __UINT_FAST16_TYPE__  __UINT32_TYPE__
+#define __INT_FAST32_TYPE__   __INT32_TYPE__
+#define __UINT_FAST32_TYPE__  __UINT32_TYPE__
+#define __INT_FAST64_TYPE__   __INT64_TYPE__
+#define __UINT_FAST64_TYPE__  __UINT64_TYPE__
+
+#endif
diff --git a/libc/integral/normalize.inc b/libc/integral/normalize.inc
index 22026bbcc..7fd08bed5 100644
--- a/libc/integral/normalize.inc
+++ b/libc/integral/normalize.inc
@@ -80,6 +80,8 @@
 
 #if defined(__LP64__) && !defined(__INT64_TYPE__)
 #include "libc/integral/lp64.inc"
+#elif defined(_MSC_VER) && !defined(__INT64_TYPE__)
+#include "libc/integral/llp64.inc"
 #endif
 
 #if !(__ASSEMBLER__ + __LINKER__ + 0)