2

I am displaying to the user whether their device is 32-bit or 64-bit. Currently, I am determining that based on the value of UInt.max:

let cpuRegisterSize = count(String(UInt.max, radix: 2))

It feels a little hacky, so I was wondering if there was an API in Swift that returns that value instead. UIDevice doesn't seem to hold that information, from what I can tell.

mdaigle
  • 303
  • 3
  • 8
  • Other than by a "what's the size of UInt", there probably is no test other than "if device is one of these I know to be 32 bit then it's 32 bit; otherwise it's 64". Or really hacky stuff like don't build for ARMv7s and time integer divide, which no sane person would ever implement. Luckily I guess those few users that care are likely to be technical enough to be forgiving of edge case incorrect results. – Tommy Aug 06 '15 at 19:03
  • 1
    Do you want to check if the device has a 64-bit processor, or if the app is running in 64-bit mode? That are different questions. Compare [Determine if iOS device is 32- or 64-bit](http://stackoverflow.com/questions/20104403/determine-if-ios-device-is-32-or-64-bit), which is the same question for (Objective-)C, and has answers for both questions. – Martin R Aug 06 '15 at 19:05
  • I am performing some bitwise operations and am checking to see if the result will cause an overflow before executing it. So I guess the size of UInt, which is determined by the platform, is what matters. Thanks for that link, Martin. That didn't show up in my searches on here and google. – mdaigle Aug 07 '15 at 01:21

2 Answers2

2
MemoryLayout<Int>.size == MemoryLayout<Int32>.size ? 32 : 64
Floern
  • 33,559
  • 24
  • 104
  • 119
  • Of course, now it barely matters -- Apple has signalled that they will probably be dropping all support for 32-bit apps in iOS 11. –  Apr 07 '17 at 20:56
  • This looks like it should work, but for the future (seeing as you're new to the site) you should try to back up code that you post in an answer with an explanation of why it works. At least in this case, it's pretty self-explanatory, but you should still try to post at least a sentence or two. – ostrichofevil Apr 07 '17 at 21:07
  • 1
    Or let size = MemoryLayout.size * 8 – PhoneyDeveloper Apr 12 '17 at 14:56
  • Of course, this still matters a lot -- Apple is still shipping 32-bit devices, namely the Apple Watch. Let alone other architectures, like Raspi. – hnh May 02 '18 at 13:41
1

On 32-bit devices CGFloat is Float. On 64-bit devices CGFloat is Double. So you can use CGFLOAT_IS_DOUBLE to detect current architecture.

let bit = 32 * (CGFLOAT_IS_DOUBLE + 1)

You can also use sizeof(Int):

let bit = sizeof(Int) == sizeof(Int64) ? 64 : 32
egor.zhdan
  • 4,555
  • 6
  • 39
  • 53
  • 1
    This checks if the app is *compiled* in 32-bit or 64-bit mode. 64-bit devices can run 32-bit code. – Martin R Aug 06 '15 at 19:02
  • 64 bit iOS devices require 64 bit code for anything submitted to the app store and will run that code on a 64 bit device, so if newly written 32 bit code is running on a device, then the device is 32 bit. – gnasher729 Aug 07 '15 at 04:44