最佳编码实践. [英] Best coding practice.

查看:86
本文介绍了最佳编码实践.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MASM例程,该例程生成汇编代码以实现Quicksort.一切正常,包括支持记录中的嵌入式键,支持有符号/无符号,big-endian/little endian,BYTES/WORDS/DWORDS/FLOATS/DOUBLES/EXTENDED_FLOATING的键,BYTES或WORDS的空终止字符串(元素计数= 0),任何数据类型的多个元素(从1-2048开始计数),辅助键的多种数据类型,包括将其用作相等的主键的决胜局,以使QuickSort稳定.我将添加对MMX和XMM键的支持,但是当前的任务是添加以下功能:在主键或辅助键的任何混合形式中声明升序或降序(一个可以升序,另一个可以降序,或者两者都可以).相同-升序或降序).我可以只反转条件转移并保持比较顺序相同,或者反转比较顺序并保持条件转移相同,或者最后,只需在入口处交换索引并在出口(XCHG)处将它们还原,并保持所有其余扩展代码相同.

请注意,源代码是扩展代码,但是宏调用有条件地仅组装所需的内容,而没有太多检查下一步应该做什么.为每个比较插入XCHG将增加代码(较小)和执行周期(较小),但是比较逻辑是整个类别中使用率最高的代码.有条件地组合一个升序版本和一个不同的降序版本基本上是一种复制和粘贴,需要交换索引(并且没有额外的代码或循环),或条件转移的更改.似乎并行维护两个副本可能很危险-一组代码的修复必须始终应用于另一组代码,我们都知道该策略失败的频率-重复的版本不是!

所以问题是,我应该交换索引以及应该采用哪种方式-交换索引或交换比较顺序,还是应该创建两个版本,一个版本用于升序,另一个版本用于降序?

请随时用双脚跳入并踢一点灰尘,但这是一个Quicksort,所以我不想听到C ++或C#和重载运算符以及HLL带来的所有膨胀.

嗯,我刚刚想到了一种允许主键和辅助键都具有以null结尾的字符串的方法.以太键的最低偏移必须在较高的偏移处结束,而最高偏移必须在记录的结尾处结束.需要添加更多好处,但是要小心,因为辅助键逻辑是具有不同参数的主键逻辑的副本,而且我已经剥离了对以空终止的字符串(多行)的支持.还记得关于不是的重复版本的那一点吗?哦,好吧!

Dave.

I have a MASM routine that generates assembly code to implement a Quicksort. All is working, including support for embedded keys in records, support for signed/unsigned, big-endian/little endian, keys of BYTES/WORDS/DWORDS/FLOATS/DOUBLES/EXTENDED_FLOATING, null terminated strings for BYTES or WORDS (element count=0), multiple elements for any data type (counts from 1-2048), multiple data types for a secondary key including its use as a tie-breaker for equal primary keys to make the QuickSort Stable. I will add support for MMX and XMM keys, but the current quest is to add the capability to declare Ascending or Descending in any mixture for either the primary key or for the secondary key (one can be ascending, the other descending, or both the same - either ascending or descending). I could just invert the conditional transfers and keep the comparison order the same, or invert the comparison order and keep the conditional transfers the same, or finally, just swap the indexes at entry and restore them at exit (XCHG) and keep all of the rest of the extensive code the same.

Note, the source code is extensive, but the macro invocation conditionally assembles only what is required and there is not a lot of checking what should be done next. Inserting the XCHG''s for each comparison would add code (small) and execution cycles (small), but the compare logic is the highest use code of the whole sort. Conditionally assembling a version for ascending order and a different version for descending order is basically a copy and paste with an exchange of either the indices (and has no extra code or cycles), or a change of the conditional transfers. It seems that maintaining the two copies in parallel might be dangerous - fixes to one set of code would have to always be applied to the other set, and we all know how often that strategy fails - duplicate versions aren''t!

So the question is, should I swap the indices and which way should I do this - swap the indices or swap the comparison order, or should I create two versions, one for ascending and the other for descending?

Please feel free to jump in with both feet and kick up a little dust, but this is a Quicksort so I don''t want to hear about C++ or C# and overloaded operators with all of the bloat that goes along with HLL.

Hmm, I just thought of a way to allow both the primary key and the secondary key to have null terminated strings. The lowest offset of ether key has to end at the higher offset, and the highest offset has to end at the end of the record. More goodie to add, but carefully since the secondary key logic was a copy of the primary key logic, with different parameters, and I already stripped out the support for a null terminated string (more than one line). Remember that bit about duplicate versions that aren''t? Oh, well!

Dave.

推荐答案



哇.非常疑问.

不确定为什么要在汇编中执行所有这些操作,但是无论使用哪种语言,这都是我的方法:
对于每个比较,计算一个差,如果object1< object2是负数;如果相等,则为零;如果object1> object2
然后将其差乘以一个因数+1(用于升序)和-1(用于降序),则将其乘以正非零值.结果表示在前(负),不知道(零)或在后(正非零).

如果您有多个排序条件,请首先处理主要的一个条件不知道",继续下一个条件.

[由于HTML食用者而被编辑]

:)

Hi,

Wow. Quite a question.

not sure why you are doing all this in assembly, but whatever the language, this would be my approach:
for each comparison, calculate a difference, which is negative if object1<object2, zero if equal, and positive-non-zero when object1>object2

then multiply that difference by a factor which is +1 for ascending and -1 for descending. The result indicates precede (negative), don''t know (zero), or follow (positive-non-zero).

When you have multiple sort criteria, handle the primary one first, if that results in "don''t know", continue with the next criterion.



:)


这篇关于最佳编码实践.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆