Poring to 64 bit

The following is a choice extract from an article -- "Porting to 64 bit", which is written by Andreas Jaeger(SuSE Linux AG).

I put it here just as a summary. Some section just has a title because the constent is farmiliar to me or too complex.


Abstract:

This paper illustrates some problems on porting an application to 64-bit and also shows how use a 64-bit system as development platform for both 32-bit and 64-bit code. It will give hints especially to application and library developers on writing portable code using the GNU Compiler Collection.

 

1Introduction

The following differences can attribute to port software:

1)     Compiler

2)     ABI

3)     CPU

4)     C Library

5)     Kernel

Application developers will mainly have portability problem due to different CPUs and different ABIs.

 

2 Advantages of 64-bit Programs

1)     Larger address space

2)     Improved performance, due to additional features introduced by 64-bit processors

As a example of point 2, AMD Opteron processor has some architectural improvements, like a memory controller integrated into the processor for faster memory access which eliminates high latency memory structure.

 

3 64-bit and 32-bit Programs on One System

In general, 64-bit CPUs can execute 32-bit code natively without any performance penalty, which can be commonly called “biarch support” or “multi-arch support”.

 

This offers some extra challenges for developers:

1)     The kernel must support 32-bit and 64-bit programs;

2)     The system has to be installed in such a way that 32-bit and 64-bit libraries of the same name can exist on one system;

3)     The tool chain should handle development of both 32-bit and 64-bit programs

3.1 Kernel implications

Requirements:

1)     Starting of programs for every architecture supported by the ABI, bot 32-bit and 64-bit;

2)     System calls should be compatible to the corresponding 32-bit platform.

One problem here is the ioctl() system call.

3.2 Libraries: lib and lib64

Because libraries for 32-bit and 64-bit program have the same name, so we need consider where to install them. A example solution is that put 64-bit libraries into another directory like /usr/lib64. For 32-bit programs, no changes need; for 64-bit, the autoconf scripts should specify the library install path. And the ldconfig should handle all libraries too.

3.3 Development for Different ABIs

Compilers like GCC encapsulate almost all of the differences between ABIs.

Toolchains are also needed for development.

3.4 Development

Compiler option and library path.

 

4 64-bit Porting: Hints and Pitfalls

Using the same C library means: the same usage of functions and a different layout of structures.

Different program language has some specific problems.

The sizes of fundamental types on different platforms may be different.

4.1 “Portable” x86/AMD64 Inline Assembler

4.2 Sizes and Alignment of Fundamental Datatypes and  Structure Layout

According to LP64 model, pointers and “long” have a size of 64bits while “int” uses 32 bits in 64-bit platform.

Besides this, different ABIs specify also different alignments. A double variable is aligned on x86 to 4 bytes but aligned to 8 bytes on AMD 64 despite having the same size of 8 bytes. Structures will therefore have a different layout on different platforms.

It is therefore not to hardcode any size and offsets.

4.2.1 int vs. long

1) pointer’s size

2) untyped integral constants are of type int

3) the type of identifiers of an enumeration is implementation defined but all constants get the same type, GCC by default gives them type int, unless any of the enumeration constants needs a larger type

4.3 Function Prototypes

When missing function prototypes, the return value is int and for arguments, integral promotions are performed and arguments of type float are promoted to double.

This may cause some fatal problems, like malloc and memcpy.

4.4 Variable argument Lists

Calling and called function do their job.

Va_lists and some function-like macro like va_copy to manipulate the variable parameters.

4.5 Function Pointers

4.6 Using Bitwidth-dependent types portably

4.7 Using printf and scanf

4.8 Unsigned and Signed Chars

4.9 Evaluation of Floating-point Arithmetic

4.10 Shared Libraries

-fPIC—compiler options forced by some archits

4.11 How to Check for 64-bit

4.12 Optimized Functions, Macros, and Builtins

4.13 Useful Compiler Flags

 

 

读取shape文件可以使用开源库GDAL(Geospatial Data Abstraction Library),GDAL支持读取shape文件并提供了丰富的API。 以下是使用GDAL读取shape文件中线和面的示例代码: ```c++ #include "gdal_priv.h" #include "ogr_api.h" #include "ogr_geometry.h" int main() { // 打开shape文件 GDALAllRegister(); GDALDataset* poDS = (GDALDataset*)GDALOpenEx("path/to/shapefile.shp", GDAL_OF_VECTOR, NULL, NULL, NULL); // 获取图层 OGRLayer* poLayer = poDS->GetLayer(0); // 读取线 poLayer->ResetReading(); OGRFeature* poFeature = poLayer->GetNextFeature(); while(poFeature) { OGRGeometry* poGeometry = poFeature->GetGeometryRef(); if(poGeometry->getGeometryType() == wkbLineString) { OGRLineString* poLine = (OGRLineString*)poGeometry; int nPoints = poLine->getNumPoints(); for(int i = 0; i < nPoints; i++) { double x = poLine->getX(i); double y = poLine->getY(i); // 处理线上的点 } } poFeature->Destroy(); poFeature = poLayer->GetNextFeature(); } // 读取面 poLayer->ResetReading(); poFeature = poLayer->GetNextFeature(); while(poFeature) { OGRGeometry* poGeometry = poFeature->GetGeometryRef(); if(poGeometry->getGeometryType() == wkbPolygon) { OGRPolygon* poPolygon = (OGRPolygon*)poGeometry; int nRings = poPolygon->getNumInteriorRings() + 1; for(int i = 0; i < nRings; i++) { OGRLinearRing* poRing = i == 0 ? poPolygon->getExteriorRing() : poPolygon->getInteriorRing(i - 1); int nPoints = poRing->getNumPoints(); for(int j = 0; j < nPoints; j++) { double x = poRing->getX(j); double y = poRing->getY(j); // 处理面上的点 } } } poFeature->Destroy(); poFeature = poLayer->GetNextFeature(); } // 关闭文件 GDALClose(poDS); return 0; } ``` 这段代码可以读取shape文件中的第一个图层(如果有多个图层,可以使用GetLayer(i)获取第i个图层),并分别读取线和面上的点。需要注意的是,读取面时需要分别处理面的外环和内环。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值