D语言中国主页  D语言编辑器SciTE4D   DWin库 D语言官方网站
D语言编译器1.x最新版 OpenSource   Tango   webnews  Wiki

查看完整版本: 29 D语言 移植性指南 Portability Guide

yidabu 2007-4-26 16:13

29 D语言 移植性指南 Portability Guide

29 D语言 移植性指南        Portability Guide知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070426点击下面网址查看原文:http://www.d-programming-language-china.org        by:        uFramer        http://www.d-programming-language-china.org        from:        http://www.digitalmars.com/d/portability.html        version:        基于D 1.013        (Apr 19, 2007)        从软件工程的观点来看,应尽量减少代码中那些可以避免的移植性问题。用于减少潜在的移植性问题的技术有:        应该将整数和浮点类型的大小视为下界。算法应该能够在相应的类型大小增长后依然运行良好。        浮点运算应该可以使用高于保存相应值的变量的精度。浮点算法应该在相应的类型的精度提高后依然运行良好。        避免依赖于计算中那些副作用的顺序,因为编译器可能会改变这些顺序。例如:        [Copy to clipboard] [ - ]CODE:                                a + b + c                可以按照各种顺序计算:(a + b) + c、a + (b + c)、(a + c) + b、(c + b) + a 等。括号控制运算符的优先级,但括号 不能 控制求值的顺序。        尤其是,函数参数既可能从左到右计算,也可能从右到左计算,这依赖于所采用的调用管理。        如果可结合的运算符 + 或 * 的操作数是浮点值,表达式的顺序不会被调整。        避免依赖于字节序;也就是,不要依赖于 CPU 是“低字节优先”还是“高字节优先”。        避免依赖于指针或者引用的大小,它们可不一定同某个整数一样大小。        如果不可避免的要依赖于类型的大小,应该在代码中放入一个 assert 进行验证:        [Copy to clipboard] [ - ]CODE:                                assert(int.sizeof == (int*).sizeof);        32 to 64 Bit Portability32 位平台向 64 平台移植        六十四 位处理器和操作系统正走向我们。初步的想法:( 本文出处: http://www.d-programming-language-china.org )        无论是在 32 位还是在 64 位代码中,整数类型的大小相同。        从 32 位迁移到 64 位后,指针和引用的大小将从 4 字节增为 8 字节。        使用 size_t 作为可以覆盖整个地址空间的那个无符号整数类型的别名。 Array indices should be of type size_t.        使用 ptrdiff_t 作为可以覆盖整个地址空间的那个无符号整数类型的别名。 A type representing the difference between two pointers should be of type ptrdiff_t.         .length, .size, .sizeof, .offsetof and .alignof 属性的类型为 size_t 。Endianness        Endianness refers to the order in which multibyte types are stored. The two main orders are big endian and little endian. The compiler predefines the version identifier BigEndian or LittleEndian depending on the order of the target system. The x86 systems are all little endian.        The times when endianness matters are:        When reading data from an external source (like a file) written in a different endian format.        When reading or writing individual bytes of a multibyte type like longs or doubles.OS Specific Code特定于 OS 的代码        特定于系统的代码应该单独放入各自独立的模块中。在编译时,导入特定于当前系统的模块。        处理较小的差异时,可以在特定于系统的模块内定义一个常量,然后使用 if 语句或静态语句处理各种情况。( lastupdate:20070426 最新文章请访问http://www.d-programming-language-china.org )关于一大步成功社区:yidabu提倡在交流中学习,在分享中提高收集感兴趣的知识,写下心得,通过网络与别人一起分享理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台
页: [1]
查看完整版本: 29 D语言 移植性指南 Portability Guide