How iOS Works Support 64-bit

Source: Internet
Author: User

Apple released a message on October 20, 2014: From next year's February 1, apps submitted to the App Store must support 64-bit. The detailed message address is: https://developer.apple.com/news/?id=10202014a

How are we supposed to start getting our app to support 64-bit?

Basic knowledge

The A7 CPU from iphone 5S to the newly released iphone 6 (A8 CPU) has already supported the 64-bit ARM architecture. See Wikipedia for an introduction to 64-bit. There's a lot of talk about Apple using the A7,A8 chip, can you refer to the IPhone 6 Apple A8 chip to compare Apple A7 boost significantly? , what does the IPhone 5s equipped with the A7 processor 64-bit mean?

    • Xcode 5.0.1 started supporting the compilation of binary 32-bit and 64-bit
    • While supporting 32-bit and 64-bit, we need to select the minimum deployment target for IOS 5.1.1
    • 64-bit binary must be running on a CPU that supports 64-bit, and the minimum OS version requirement is 7.0.3
About architectures parameter issues in Xcode "Build Setting"
    • Architectures: The set of instructions you want to support. (The support instruction set is implemented by compiling the corresponding binary packets, and if there are multiple supported instruction sets, the packets containing multiple instruction set codes will be compiled, resulting in a large package that is eventually compiled.) )
    • Valid architectures: The set of instructions that will be compiled. (Valid architectures and architecture the intersection of two collections is the version generated by the final compilation)
    • Build Active Architecture only: Compiles only the set of instructions that are applicable to the current device (if this parameter is set to Yes, using iphone 6 debugging, then a binary that is eventually generated that supports the ARM64 instruction set. General in debug mode set to Yes,release to No)

The following reference is given to the instruction set:

ARMv8/ARM64: iPhone 6(Plus), iPhone 5s, iPad Air(2), Retina iPad Mini(2,3)ARMv7s: iPhone 5, iPhone 5c, iPad 4 ARMv7: iPhone 3GS, iPhone 4, iPhone 4S, iPod 3G/4G/5G, iPad, iPad 2, iPad 3, iPad Mini   

For support 64-bit, we can set architectures as standard architectures, on the latest Xcode 6, which includes ARMV7 and arm64.

Get apps to support 32-bit and 64-bit basic steps
    • Make sure that the Xcode version number >=5.0.1
    • Update project settings, minimum deployment target >= 5.1.1
    • Change architectures to standard architectures(include 64-bit)
    • Run the test code, resolve the compilation warnings and errors, and make changes to the appropriate location against this document or the Official document 64-bit Transition Guide for Cocoa touch. (The compiler can't tell us everything)
    • Test on a real 64-bit machine
    • Use instruments to view memory usage issues
Major changes in 64-bit

The 64-bit runtime environment and the 32-bit runtime environment are mainly the following two points different:

    • Changes in data types
    • Changes on the method call
Changes in data types

The integer data type changes as follows:

For the concept of byte alignment, refer to the following link: http://blog.csdn.net/21aspnet/article/details/6729724#comments

The floating-point type is changed as follows:

Changes in data types can have these effects on our programs:

    • Increased memory pressure
    • Conversion of 64-bit to 32-bit data
    • The calculation may produce different results
    • When a value is copied from a large data type to a small data type, the data may be truncated. (Nsinteger-int)
Changes on the method call

32-bit-based CPUs and CPUs based on 64-bit have different numbers of registers, and there are different protocols on the method calls. So 32-bit and 64-bit are different at the assembly level. If we do not use assembler programming in the program, the invocation protocol is seldom encountered.

How to write a robust 64-bit code

According to the above changes, the official document 64-bit Transition Guide for Cocoa Touch gives the following 7 steps:

    • Do not assign long integers to integer int (data loss is caused on 64-bit)
    • Do not assign pointer type pointer to integer int (64-bit causes address data loss)
    • Watch out for numerical calculations (mask calculation, unsigned integer and signed integer simultaneous use, etc.)
    • Pay attention to the changes in the Alignment method
    • Conversion of data between 32-bit to 64-bit (user data passed through the network may exist in both 32-bit and 64-bit environments)
    • Rewrite assembly code
    • Do not force conversions prior to variable parameter methods and immutable parameter methods

在LLVM编译器中,枚举类型也可以定义枚举的大小。我们在使用中,指派枚举值到一个变量时,应该使用适当的数据类型。

Do not assign pointer type pointer to integer int
int a = 5;int *c = &a;/* 32-bit下正常,64-bit下错误。最新的Xcode6.0编译提示警告:‘Cast to int* for smaller integer type int‘*/int *d = (int *)((int)c + 4); /* 正确, 指针可以直接增加*/int *d = c + 1;

If we must convert the pointer to an integer, we can change the above code to:

View uintptr_t defined as typedef unsigned long uintptr_t;

Keep data types consistent

When the method is used, the arguments, parameters, and assignments need to be kept in the same data type. In particular, there are several types of correct use in the iOS app:

    • Long
    • Nsinteger
    • Cfindex
    • size_t

在32-bit和64-bit下,fpos_t和off_t都是64 bits的数据大小,永远不要把它们指向int整型。

long PerformCalculation(void);int c = PerformCalculation(); // 错误 64-bit上数据将被截取long y = PerformCalculation(); // 正确int PerformAnotherCalculation(int input);long i = LONG_MAX;int x = PerformCalculation(i); // 错误int ReturnMax(){    return LONG_MAX; // 错误}
Common data type conversion problems in cocoa

Nsinteger: There are separate definitions under 32-bit and 64-bit:

    #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET_OS_IPHONE) || TARGET_OS_WIN32 || NS_BUILD_32_LIKE_64        typedef long NSInteger;    #else        typedef int NSInteger;    #endif

We should never assume that Nsinteger and int are the same size, and the following examples need to be noted in use:

    • When using NSNumber object conversions
    • When using Nscoder codec, if the Nsinteger code is encoded under the 64-bit device, the Nsinteger is decoded under the 32-bit device. If the size of the value exceeds 32-bit at the time of decoding, there will be an exception
    • Some constants defined in Famework using Nsinteger

CGFloat: As with Nsinteger, there are different definitions.

typedef CGFLOAT_TYPE CGFloat;#if defined(__LP64__) && __LP64__# define CGFLOAT_TYPE double#else# define CGFLOAT_TYPE float#endif

Here's a demonstration of the error:

CGFloat value = 200.0;CFNumberCreate(kCFAllocatorDefault, kCFNumberFloatType, &value); //64-bit下出现错误CGFloat value = 200.0;CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &value); //正确
Numerical calculation of integral type

The sign bit extension for C language can be referenced by: http://blog.163.com/shi_shun/blog/static/237078492010651063936/

We look directly at the example:

    int a = -2;    unsigned int b = 1;    long c = a + b;    long long d = c;    printf("%lld\n", d);

Problem: This code runs under 32-bit as expected, with a output of-1 (0xFFFFFFFF). The result of running under 64-bit is: 4294967295 (0X00000000FFFFFFFF).

Cause: The addition of a signed value and an unsigned value of the same precision is unsigned. This unsigned result is converted to a higher-precision value with a 0 extension.

Solution: Change variable b to grow integer long

Use the appropriate data size when creating data structures

C99 provides a built-in data type that guarantees consistent data size, even if the underlying hardware structure is different. In some cases, we know that the data is a fixed size or that a particular variable has a limited range of values. At this point, we should choose a specific type to avoid wasting memory.

Types are as follows:

Never use malloc to request a specific memory size for a variable, instead use sizeof to get the size of the variable or struct.

Also we need to be aware of modifying the formatted string to support both 32-bit and 64-bit.

Careful handling of methods and method pointers
int fixedFunction(int a, int b);int variadicFunction(int a, ...);int main{    int value2 = fixedFunction(5,5);    int value1 = variadicFunction(5,5);}

In both of these methods, the data of the parameter is read with the same instruction under 32-bit, but on 64-bit, it is compiled with a completely different protocol.

If you pass a method pointer in your code, you should ensure that the protocol for the method invocation is consistent. Never convert a variable parameter method into a fixed parameter.

int MyFunction(int a, int b, ...);int (*action)(int, int, int) = (int (*)(int, int, int)) MyFunction;action(1,2,3); // 错误示范

上述错误的写法,编译器是不会提示警告或者错误的,并且在模拟器中也不会暴露出问题来。在发布自己的App前,一定记得要使用真机去测试。

Summarize

During the support 64-bit process, the project project should be fully checked according to the 7 steps provided in the Apple documentation. If the project involves a lot of C or C + + code, be more cautious in supporting 64-bit.

After writing this note, I think I need to revisit the basics of C. XD, by the way, prayed for a third-party library in the project hurriedly updated to support 64-bit, Amitabha.

ps: 找出不支持arm64的静态库 find . -name *.a -exec lipo -info "{}" \;

How iOS Works Support 64-bit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.