iOS support arm64

Source: Internet
Author: User
Tags truncated

Apple requires that packages submitted after 2015/2/1 must contain arm64 or be rejected. As a result, support for 64-bit is imminent, especially for projects that have a lot of legacy code to start early.

How to support arm64

To support the ARM64 structure, several conditions need to be met:

    • Add the arm64 entry in the architectures setting, if you are using more than 6.0 of the Xcode version, use the default configuration item.
    • Add the arm64 entry in the valid architectures setting.
    • The deployment target is changed to be greater than or equal to 5.1.1, because the ARM64 supports the 5.1.1 system minimum.
    • Support for the 64-bit runtime environment, which opens compiler warnings and errors for 64-bit, can help you migrate smoothly to arm64. If you do not obviously prompt for an upgrade of Xcode compile configuration items after the setup is complete, you can build code first, which is recommended to support the 64-bit runtime environment and enable this setting.

After you complete these steps, you can build an IPA file that contains both 32-bit and 64-bit. After the build is complete you will find hundreds of warnings and errors, and this is the beginning of the real job.

In addition, after the above steps, you will find that the current project is not able to use Xcode in iOS6 below the system uplink, or itunes and so directly install the packaged IPA file. However, the packaging of the IPA file via AppStore can be installed on the iOS5.1.1 , it was inferred that AppStore will do some magic things to the IPA file submitted. If you want to iOS6 below the system uplink, you need to valid architectures to remove the arm64.

ARM64 adaptation

According to Apple's official presentation, ARM64 will make the following changes:

    • Data Type.
    • Function calling.
    • ARM instruction.

The specific changes in these three areas are described below.

Data Type

The biggest change that arm64 brings is that the address space and register grow from 32-bit to 64-bit, and the system can provide more memory and a larger register space. The following two graphs list the changes to the data type after transfer to 64-bit.

ILP32 represents the 32-bit system, LP64 represents the 64-bit system. The bold in the figure indicates that the 64-bit is different from the 32-bit, and its change can be summed up in the following points:

    • The size of the pointer grows from 4Byte to 8Byte.
    • The long,nsiteger,size_t,time_t,cfindex,cgfloat has grown from 4Byte to 8Byte.
    • The alignment of long long,fpos_t,off_t,double is increased from 4Byte to 8Byte.

From the above summary, if the system development software for 32-bit and 64-bit at the same time, it will inevitably generate operation and assignment between the data of 32-bit and 64-bit. The main risks are as follows:

  • The operation between 32-bit and 64-bit data.

    The operations here include operations such as mathematical operations and assignment, which may encounter data truncation, data overflow, and some unique boundary conditions. There are several main situations:

      • The assignment between 32-bit and 64-bit data. Assign a 64-bit data to a 32-bit variable, such as:

        int intValue = NSItegerMax;

        This will result in data truncation and will not get the desired result. Similarly, if you assign a 32-bit data to a 64-bit variable, you will get unexpected results, such as:

        NSUInteger biggerIntValue = -1;
      • Pointer variables that point to 32-bit data and pointer variables to 64-bit data are assigned to each other. Do the following:

        int *pointerToInt = pointerToLong;pointerToLong = pointerToInt;

        Since Pointertoint + 1 is actually +4, and Pointertolong + 1 is actually +8, the result of the pointer operation after the conversion is wrong.

      • The assignment between the pointer and the variable. As follows:

        int currentAddress = pointerToLong;NSInteger *pointerToNSInteger = currentAddress + 1;

        The pointer address here is not only truncated, and the +1 operation will not get the desired result.

      • An implicit enumeration type conversion. The compiler assigns an appropriate storage type for each enumeration type, either int or nsinteger, as it needs to be allocated. Therefore, when you implicitly assign an enumeration type to a variable of another type, it can cause data to be truncated or incorrectly promoted.
      • System-related data types. Nsintegermax The values expressed in 32-bit and 64-bit are not the same as the Earth. There are also problems with common hardcode in the code, such as 32,24, which is common in left-shift operations.

    For the potential risks listed above, here are two tips:

      1. Use the same data type. Minimize explicit and implicit type conversions, using the same data type.
      2. Avoid switching between pointers and shaping. Try to avoid assigning pointers to transformation variables or explicitly casting them. If you really need to, use the uintptr_t type.
  • Exchange data between 32-bit and 64-bit software.

    32-bit and 64-bit software is likely to read and write the same file over the network, and even users will use 32-bit software to overwrite the same data under 64-bit software, which can lead to unpredictable errors. For example Nsinteger in 32-bit is 4Byte, and on 64-bit is 8Byte. If the content generated by the 64-bit software is accessed in the 32-bit software at this time as a Nsinteger type of file, the results cannot be predicted.

     struct second {     int milliSecond;     long microSecond; };
    The size of the second in the 32-bit software is 8byte,microsecond with an offset of 4. The size of the second in the 64-bit software is 16Byte, and the microsecond offset is 8. If this is persisted in the 32-bit and 64-bit systems or uploaded to the network for other software access, the correct results will not be obtained. To address this issue, here are some suggestions:
      1. Use a consistent data type.

        It is also recommended to use the same data types in 32-bit and 64-bit if the system-independent data types are used as much as possible, if the software has both 32-bit and 64-bit versions. For example, use int32_t or int64_t as much as possible, whether in 32-bit software or 64-bit software.

      2. Create a consistent data model of the memory model.

        That is, the data model size and the element offset are the same. The following two solutions are available for the second problem mentioned above:

         struct second { int32_t milliSecond; int32_t microSecond; };   #pagram pack(4) struct second { int32_t milliSecond; int64_t microSecond; }; #pragma options align=reset
      3. Use Plist,xml and JSON for serialization and persistence.

        When using Nscoder to encode a nsinteger data on the 64-bit software, 32-bit the decode data on the Nsinteger software, and the integer value is just beyond the 32-bit When an int type can represent a range, an exception is thrown.

  • Consumes more memory.

    From the above analysis, many of the basic types and pointer addresses are increased from 4Byte to 8Byte, which also indicates that the 64-bit software will consume more memory. Not only do some of the basic types consume more memory, but even the most commonly used Foundation Object consumes more memory, due to its powerful features, such as nsarray,nsdictionary. The following are some suggestions for this issue:

    • Select the appropriate foundation Object.

      If you only store a simple object in the Nsarray, and then produce hundreds of these objects, then the memory consumed will be huge. Therefore, try to use the right class for the right situation.

    • Select a compact data model.

      Try to choose a more appropriate data model to represent your data. Assuming you want to represent a date type, the data model used is as follows:

        struct Date {Nsinteger second;      Nsinteger minute;      Nsinteger hour;      Nsinteger Day;      Nsinteger month;  Nsinteger year; };

      The size of date on 32-bit software is 24Byte, and on 64-bit software date is 48Byte, amazing! Simply changing the design, while achieving the goal can save a lot of memory use, the structure is as follows:

        struct Date {long seconds; };
      The

      seconds represents the total number of seconds elapsed and can be year,month,day by simply calculating ...

    • Eliminate excess padding.

      For performance reasons, compilers typically add padding between basic data types to align them and avoid multiple accesses to memory. Like what:

         struct morePadding {    //32-bit       char second;         //offset 0       int  minute;         //offset 4       char hour;           //offset 8       NSInteger day;       //offset 12   };                     //total size 16

      The actual size of the morepadding is 10Byte, while the memory size occupied is 16Byte. Redesigned to change it to the following structure:

         struct morePadding {    //32-bit       int  minute;         //offset 0       NSInteger day;       //offset 4       char second;         //offset 8       char hour;           //offset 9   };                     //total size 10
    • Use as few pointer variables as possible.

      To avoid over-using pointers in the data model, consider the following models:

         struct node{         node        *previous;         node        *next;         uint32_t    value;       };

      In 64-bit software, the total size of node is 20Byte, while valid data only 4byte,80% space is occupied by the pointer, you can consider using other data structures instead.

    • Use smaller data types within the range that can be expressed.

      If you are simply expressing the number thousands of hundreds of, then int will be able to meet the requirements without using Nsinteger. The purpose is to use sufficient data types to represent numbers, and it is not necessary to 64-bit software to use the int64_t type.

    • Only the data that is required by the cache.

      For the purpose of performance optimization, our code often uses the cache mechanism, that is, take the space to change time, the cache can indeed improve the responsiveness of the software in many places, and even save network traffic. For example, cache network pictures to avoid the next Internet, cache the image after the filter processing, to avoid the CPU to perform multiple consent operation. 64-bit software consumes more memory, and if too much of it is cached, it may degrade the overall performance of the software. Therefore, it is recommended that you do not use caching for the following scenarios:

      • Can be easily recalculated to produce.
      • It's easy to get from somewhere else.
      • Re-generated cheaply.
      • Read-only data can be accessed through mmap ().

      Therefore, it should be often tested that the cache does improve performance.

    • Reasonable use of @autoreleasepool.

      Quickly release autorelease objects that are no longer needed, and avoid memory exhaustion forcing the system to issue uiapplicationdidreceivememorywarningnotification notifications. Especially for loops and recursive calls, special attention needs to be given.

    • Handle Uiapplicationdidreceivememorywarningnotification.

      All related objects must handle uiapplicationdidreceivememorywarningnotification notifications, especially for each Controller,cache Manager Need to be notified the first time, avoid causing low memory crash.

Function Calling

If assembly language is not used, the effect of conversion to 64-bit is not very large, only a little, variable parameters of the function of the call rules on the 64-bit software is not the same. Therefore, the following suggestions are recommended for function calls:

    • The argument and the parameter use a consistent data type.
    • Avoid casting between functions that do not have the same function signature.

      The passing of pointers to different function signatures can easily result in the passing of inappropriate arguments when calling a function, thus failing to achieve the desired result, especially between the fixed-parameter function and the variable-parameter function, as follows:

        int MyFunction(int a, int b, ...);  int (*action)(int, int, int) = (int (*)(int, int, int)) MyFunction; action(1,2,3); // Error!
    • Pass the correct parameters to the function of the variable parameter.

      Because the variable parameter list usually does not provide type information, if the wrong parameter value is passed at this point, the correct result will not be obtained. Therefore, you might consider adding a format string to a mutable parameter, providing some type information, such as printf ().

Objective-c Runtime

Do not directly access the OC Object Isa, in the 64-bit software inside Isa is no longer a pointer to class object, it contains some pointer data and some run-time information. If you need to get class object, use the Object_getclass function.

ARM64 instruction

Arm64 directives are vastly different from those of 32-bit, so the assembly code needs to be rewritten. Arm64 's function calling convention is not the same as standard arm, you can refer to the iOS ABI function call Guide.

iOS support arm64

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.