# LLP64 targets and integral types

**URL:** <https://forums.swift.org/t/llp64-targets-and-integral-types/18253>\
**Category:** Core Libraries\
**Tags:** windows, foundation\
**Created:** [November 25, 2018, 12:03am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253 "2018-11-25T00:03:05Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 25, 2018, 12:03am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/1 "2018-11-25T00:03:05Z")

</div>

Hello from the Windows world!

Windows x86\_64 is a LLP64 environment, that is `long` is 32-bits, and `long long` and pointers are 64-bits. This has created a small snag for me now that the Windows port is far enough along to start doing interesting things. In particular, I am porting Foundation to Windows, and hitting cases where we create `CFOptionSet`s with `UInt`, which actually ends up being 32-bits instead of 64-bits. This occurs due to the way that `CLong` is imported IIRC. How do we want to handle this to make code be easier to port across targets? It seems unfortunate that swift did not opt to standardize on size-specific integral types.

CC: @tkremenek @jrose @Mike_Ash @millenomi @Torust

---

<div class="post-metadata">

**Author:** ![Torust](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Torust](https://forums.swift.org/u/Torust)\
**Post date:** [November 25, 2018, 12:45am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/2 "2018-11-25T00:45:49Z")

</div>

When I encountered this I handled it by just casting the value to the inferred `RawValue` type using the `init(bitPattern:)` initialiser. It's a little verbose, but isn't _too_ terrible of a solution.

I do remember it getting particularly ugly with enums. I think that constants (e.g. enum cases) are imported as `Int32`, whereas the `RawValue` type for the enum is imported as `UInt32` (or possibly the other way around), meaning I had to do this sort of dance:

`SDLModifiers(rawValue: SDLModifiers.RawValue(bitPattern: KMOD_RALT.rawValue))`

to get things to work.

> [@compnerd](#):
>
> We create `CFOptionSet` s with `UInt` , which actually ends up being 32-bits instead of 64-bits.

Can we modify the Foundation API for Windows to use `size_t` or `intptr_t` as the base types rather than `long`s?

* * *

As reference for others, this is how it's currently set up:

- On Win64, `unsigned long` gets mapped to `UInt32` in Swift and `long` to `Int32`.
- On Win32, `unsigned long` gets mapped to `UInt` in Swift and `long` to `Int`.
- On both, `UInt` or `Int` in Swift code gets mapped to `uintptr_t` or `intptr_t` in C code.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 25, 2018, 1:51am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/3 "2018-11-25T01:51:51Z")

</div>

The enumeration case makes sense. Enumerations are signed by default on Windows where as they are unsigned on Linux and macOS. This is going to be more interesting later I think.

I'm afraid I don't follow what you are suggesting for the cast. That is one of the more annoying thing to fix. The hashing breaks due to the type mismatch for the `__CFHash` (the return type `unsigned long` is not treated as conforming to `_Pointer` (since there is no matching type).

Well, it isn't the Foundation API that would need to change there, but the CoreFoundation APIs. But, even that won't help too much I am afraid. The importing issues will come up due to the behavioural differences due to the ABI. We need to figure out how to deal with the signedness conversions as well as the violation of the assumption that `Int` and `UInt` are really `intptr_t` and `uintptr_t` equivalent.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 25, 2018, 1:59am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/4 "2018-11-25T01:59:02Z")

</div>

> [@compnerd](#):
>
> The importing issues will come up due to the behavioural differences due to the ABI. We need to figure out how to deal with the signedness conversions as well as the violation of the assumption that `Int` and `UInt` are really `intptr_t` and `uintptr_t` equivalent.

I'm not well versed in this area of Swift, so forgive the basic question: why isn't `CLong` simply an alias for a different type for LLP64 environments? That is, preserve the relationship that `long` is imported as `CLong` and that `intptr_t` is equivalent to `Int`; for LLP64 environments, make `typealias CLong = Int32`.

---

<div class="post-metadata">

**Author:** ![Torust](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Torust](https://forums.swift.org/u/Torust)\
**Post date:** [November 25, 2018, 2:04am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/5 "2018-11-25T02:04:02Z")

</div>

> [@compnerd](#):
>
> I'm afraid I don't follow what you are suggesting for the cast.

I think I misunderstood what the problem was. What I was meaning is that you can use the `.RawValue` on `RawRepresentable` types rather than hard-coding the type in Swift code to work around differences in how those types are imported to Swift code.

> [@compnerd](#):
>
> ... violation of the assumption that `Int` and `UInt` are really `intptr_t` and `uintptr_t` equivalent.

That assumption should hold even on Windows x64. `intptr_t` should be equivalent to `Int` and `uintptr_t` to `UInt` on every platform – is that not the case?

> [@xwu](#):
>
> ... preserve the relationship that `long` is imported as `CLong` and that `intptr_t` is equivalent to `Int` ; for LLP64 environments, make `typealias CLong = Int32`, make `typealias CLong = Int32` .

That's exactly what the current setup is. Most of it was set up in [this PR](https://github.com/apple/swift/pull/14575), and there's some relevant discussion on it as to the best approach for consistency between Win64 and Win32.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 25, 2018, 2:08am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/6 "2018-11-25T02:08:13Z")

</div>

> [@Torust](#):
>
> That's exactly what the current setup is. Most of it was set up in [this PR](https://github.com/apple/swift/pull/14575), and there's some relevant discussion on it as to the best approach for consistency between Win64 and Win32.

That makes sense to me. I don't understand, then, how @compnerd's scenario below is possible:

> [@compnerd](#):
>
> I am porting Foundation to Windows, and hitting cases where we create `CFOptionSet` s with `UInt` , which actually ends up being 32-bits[...]

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 25, 2018, 2:12am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/7 "2018-11-25T02:12:53Z")

</div>

Hmm, maybe I misinterpreted something? But, I am definitely seeing a mismatch between `Int` and `Int64` when targeting `x86_64-unknown-windows-msvc`. This makes me suspicious of the mapping that we have currently.

---

<div class="post-metadata">

**Author:** ![Torust](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Torust](https://forums.swift.org/u/Torust)\
**Post date:** [November 25, 2018, 2:17am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/8 "2018-11-25T02:17:06Z")

</div>

What exactly is the mismatch? If the `unsigned long` return type for `CFHash` is switched to be `uintptr_t`, what else breaks?

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 25, 2018, 2:20am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/9 "2018-11-25T02:20:21Z")

</div>

`CFHash` is an `unsigned long long` c.f. [Base.subproj/CFBase.h#452](https://github.com/apple/swift-corelibs-foundation/blob/007f527c445aa2b51bf8417b1c97dcfeb08f65b4/CoreFoundation/Base.subproj/CFBase.h#L452). However, use in swift will fail, e.g.

```swift
C:/Users/user/source/repos/swift-corelibs-foundation/Foundation/NSSwiftRuntime.swift:125:53: error: cannot convert value of type 'Int' to expected argument type 'Int64'
    return CFHashCode(bitPattern: (cf as! NSObject).hash)
                                  ~~~~~~~~~~~~~~~~~~^~~~
                                  Int64( )

```

---

<div class="post-metadata">

**Author:** ![Torust](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Torust](https://forums.swift.org/u/Torust)\
**Post date:** [November 25, 2018, 2:31am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/10 "2018-11-25T02:31:55Z")

</div>

Usually `CFHashCode` would be defined as `unsigned long` and therefore imported as `UInt`. `UInt` has an `init(bitPattern: Int)`, and since `hash` is defined as `var hash : Int`, that normally works.

On Windows, `CFHashCode` is imported at UInt64 since `unsigned long long` maps to `UInt64`. `UInt64` has a `bitPattern` init only for `Int64`, and not for `Int` (and `Int` and `Int64` aren't implicitly convertible/are different types in Swift, even though they have the same layout). That's why it's prompting to convert the `Int` to an `Int64` (which would be a no-op).

If `CFHashCode` were instead defined as `uintptr_t` in C things would (hopefully) work as expected.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [November 25, 2018, 2:33am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/11 "2018-11-25T02:33:45Z")

</div>

Right and why I was suggesting that perhaps we want to import `unsigned long long` as `UInt` and `long long` as `Int` for Windows 64-bit.

---

<div class="post-metadata">

**Author:** ![Torust](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Torust](https://forums.swift.org/u/Torust)\
**Post date:** [November 25, 2018, 2:40am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/12 "2018-11-25T02:40:15Z")

</div>

That doesn't seem like it would be better to me – it's just trading off one sort of breakage for another. The current implementation effectively means "use machine-width types if you want them to be exposed to Swift as machine-width types". If `long long` mapped to `Int`, that means that explicit 64-bit types in other libraries would be `Int` on Win64 and `Int64` everywhere else. That seems like a worse outcome IMO, but I'll let others weigh in.

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [November 25, 2018, 3:45am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/13 "2018-11-25T03:45:30Z")

</div>

For Apple platforms, the Foundation APIs use NSInteger which is a pointer-sized integer type, and I suspect that’s the intent for these CF interfaces as well, so they ought to import as Int/UInt independent of the C type system for the host platform. The Clang importer has a table of special typedef-importing rules that we could add the CF typedefs to so that they import consistently with how they do on LP64 platforms. It’s also worth asking whether the corelibs version of CF can be modified if necessary to use consistently sized integer types on Win64—it probably wasn’t written with LLP64 in mind and, if it’s using long, really means pointer-width.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [November 25, 2018, 5:07am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/14 "2018-11-25T05:07:17Z")

</div>

> [@Torust](#):
>
> If `CFHashCode` were instead defined as `uintptr_t` in C things would (hopefully) work as expected.

> [@Joe\_Groff](#):
>
> It’s also worth asking whether the corelibs version of CF can be modified if necessary to use consistently sized integer types on Win64—it probably wasn’t written with LLP64 in mind and, if it’s using long, really means pointer-width.

It seems you and @Torust are saying the same thing, then? The relevant code reads as follows:

```c
#if __LLP64__
typedef unsigned long long CFTypeID;
typedef unsigned long long CFOptionFlags;
typedef unsigned long long CFHashCode;
typedef signed long long CFIndex;
#else
typedef unsigned long CFTypeID;
typedef unsigned long CFOptionFlags;
typedef unsigned long CFHashCode;
typedef signed long CFIndex;
#endif

```

...and you're both saying the problem could be addressed if the LLP64-specific typedefs would simply be changed?

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [November 25, 2018, 5:10am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/15 "2018-11-25T05:10:27Z")

</div>

Those typedefs look correct. We should have the Clang importer recognize these typedefs and map them to Int/UInt, like it does for NSInteger, size\_t and other typedefs that are intended to be pointer sized. It’s likely we haven’t had to for these CF types yet only because nobody’s tried to use CF on an LLP64 platform with Swift yet.

---

<div class="post-metadata">

**Author:** ![Torust](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Torust](https://forums.swift.org/u/Torust)\
**Post date:** [November 25, 2018, 7:05am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/16 "2018-11-25T07:05:49Z")

</div>

PR for that approach is up [here](https://github.com/apple/swift/pull/20717). I created it through the web interface so haven't been able to run tests yet.

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [December 19, 2018, 4:57am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/17 "2018-12-19T04:57:13Z")

</div>

So, this certainly did help a lot. However, I seem to have come across an interesting behavioural change. Trying to import `CF_ENUM` types of `CFIndex` now do not import properly as the imported type is not believed to conform to `BinaryInteger` which means that certain conversions cannot be performed. This is a problem, for example, in `Stream.swift` in `Foundation` where they do:

```swift
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(__stream)))!

```

Where the return type of `CFReadStreamGetStatus` is `CFStreamStatus` and the conversion fails as `(init_:)` requires that `T` conforms to `BinaryInteger`.

---

<div class="post-metadata">

**Author:** ![Torust](https://avatars.discourse-cdn.com/v4/letter/t/f17d59/32.png) [@Torust](https://forums.swift.org/u/Torust)\
**Post date:** [December 19, 2018, 5:14am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/18 "2018-12-19T05:14:49Z")

</div>

I'm not sure why that would ever work – is there some sort of implicit conversion from a C enum to that C enum's RawValue type that's not kicking in? I wouldn't expect to be able to do that for enums defined in Swift.

Regardless, does writing it as:

```swift
return Stream.Status(rawValue: UInt(CFReadStreamGetStatus(__stream).rawValue))!

```

work?

---

<div class="post-metadata">

**Author:** ![compnerd](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/compnerd/32/2165_2.png) [@compnerd](https://forums.swift.org/u/compnerd)\
**Post date:** [December 19, 2018, 5:18am UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/19 "2018-12-19T05:18:42Z")

</div>

Yes, there is a conversion that is supposed to kick in for `CF_ENUM`. Yes, that does work, and that is what I had, but, it really would be better if that just mapped as expected. It avoids unnecessary changes in swift-corelibs-foundation.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [December 19, 2018, 4:48pm UTC](https://forums.swift.org/t/llp64-targets-and-integral-types/18253/20 "2018-12-19T16:48:28Z")

</div>

I'm not sure what you mean by "there is a conversion that is supposed to kick in for `CF_ENUM`". This really does seem like it shouldn't work. `CF_ENUM`s don't conform to BinaryInteger on Linux or Apple platforms either. It'd be worth doing a `-debug-constraints` run on Linux to see what overload it's picking for that initializer.

[Next page](https://forums.swift.org/t/llp64-targets-and-integral-types/18253.md?page=2)
