Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions system/lib/libc/musl/arch/emscripten/bits/alltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

#define __BYTE_ORDER __LITTLE_ENDIAN

// Can't use __LONG_MAX__ here since musl's libs does pre-processor comparison
// with 0x7fffffffL directly.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparing in the pre-processor should work fine even with #define LONG_MAX __LONG_MAX__? https://godbolt.org/z/zonffPn6o

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, in that case there must be some other reason.. I just assumed that was it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#if __LP64__
#define LONG_MAX 0x7fffffffffffffffL
#else
#define LONG_MAX 0x7fffffffL
#endif

#define _Addr __PTRDIFF_TYPE__
#define _Int64 __INT64_TYPE__
#define _Reg __PTRDIFF_TYPE__
Expand Down
11 changes: 0 additions & 11 deletions system/lib/libc/musl/arch/emscripten/bits/limits.h
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
|| defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
#define PAGE_SIZE 65536
#define LONG_BIT 32
#endif

#if __LP64__
#define LONG_MAX 0x7fffffffffffffffL
#else
#define LONG_MAX 0x7fffffffL
#endif
#define LLONG_MAX 0x7fffffffffffffffLL
Copy link
Member

@kripken kripken Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need to define this one in the other header? I tried to find where it was defined elsewhere and saw this:

$ grep -U2 -r LLONG_MAX system/lib/libc/musl/include/
system/lib/libc/musl/include/limits.h-#define LONG_MAX __LONG_MAX
system/lib/libc/musl/include/limits.h-#define ULONG_MAX (2UL*LONG_MAX+1)
system/lib/libc/musl/include/limits.h:#define LLONG_MIN (-LLONG_MAX-1)
system/lib/libc/musl/include/limits.h:#define LLONG_MAX  0x7fffffffffffffffLL
system/lib/libc/musl/include/limits.h:#define ULLONG_MAX (2ULL*LLONG_MAX+1)
system/lib/libc/musl/include/limits.h-
system/lib/libc/musl/include/limits.h-#define MB_LEN_MAX 4

Does that work? It uses LLONG_MAX before it defines it...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

otherwise lgtm

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems to work fine yes. At least this is the same as upstream musl.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this is just how the C pre-processor works, macros get a expanded at the point of use:

#include <stdio.h>

// BAR is not defined at the point of defining FOO
#define FOO BAR

int main() {
#define BAR 12
  printf("FOO=%d\n", FOO); // prints 12
#define BAR 13
  printf("FOO=%d\n", FOO); // prints 13
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, really... very interesting. I did not know that about C macros!

13 changes: 13 additions & 0 deletions test/other/test_stdint_limits.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
#include <limits.h>
#include <stdio.h>
#include <stdint.h>

int main () {
printf("INT_MIN: %d\n", INT_MIN);
printf("INT_MAX: %d\n", INT_MAX);

printf("LONG_MIN: %ld\n", LONG_MIN);
printf("LONG_MAX: %ld\n", LONG_MAX);

printf("LLONG_MIN: %lld\n", LLONG_MIN);
printf("LLONG_MAX: %lld\n", LLONG_MAX);

printf("INTPTR_MIN: %ld\n", INTPTR_MIN);
printf("INTPTR_MAX: %ld\n", INTPTR_MAX);

printf("PTRDIFF_MIN: %ti\n", PTRDIFF_MIN);
printf("PTRDIFF_MAX: %ti\n", PTRDIFF_MAX);

Expand Down
8 changes: 8 additions & 0 deletions test/other/test_stdint_limits.out
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
INT_MIN: -2147483648
INT_MAX: 2147483647
LONG_MIN: -2147483648
LONG_MAX: 2147483647
LLONG_MIN: -9223372036854775808
LLONG_MAX: 9223372036854775807
INTPTR_MIN: -2147483648
INTPTR_MAX: 2147483647
PTRDIFF_MIN: -2147483648
PTRDIFF_MAX: 2147483647
INTPTR_MIN: -2147483648
Expand Down
3 changes: 3 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -14198,3 +14198,6 @@ def test_no_pthread(self):
self.do_runf('hello_world.c', emcc_args=['-pthread', '-no-pthread'])
self.assertExists('hello_world.js')
self.assertNotExists('hello_world.worker.js')

def test_sysroot_includes_first(self):
self.do_other_test('test_stdint_limits.c', emcc_args=['-std=c11', '-iwithsysroot/include'])