简体   繁体   English

将 Float32 转换为 Float16

[英]Converting Float32 to Float16

This is more a follow up to https://stackoverflow.com/a/5587983/13586005 .这更像是对https://stackoverflow.com/a/5587983/13586005的跟进。 @sam hocevar or anybody else who understands this: Would you mind explaining what is happening here: @sam hocevar 或其他任何了解这一点的人:您介意解释这里发生的事情吗:

tmp = (tmp - 0x70) & ((unsigned int)((int)(0x70 - tmp) >> 4) >> 27);

I'm not sure I fully follow it.我不确定我是否完全遵循它。 I understand that (tmp - 0x70) is correcting for the 127->15 bias, but I don't understand the 2nd part( (unsigned int)((int)(0x70 - tmp) >> 4) >> 27 ) and therefore don't understand the & with the corrected bias in the last step.我知道(tmp - 0x70)正在纠正 127->15 偏差,但我不明白第二部分( (unsigned int)((int)(0x70 - tmp) >> 4) >> 27 )和因此,在最后一步中不理解带有更正偏差的 &。 Thanks!谢谢!

(unsigned int)((int)(0x70 - tmp) >> 4) >> 27

is equivalent 1 to相当于1

(int)(0x70 - tmp) < 0 ? 0x1f : 0

but guarenteed to not involve a branch -- it instead extracts the sign bit from the result of the extraction, replicates it 4 times, then downshifts to get either 0x1f or 0 depending.但保证不涉及分支——而是从提取结果中提取符号位,复制 4 次,然后降档以获得 0x1f 或 0,具体取决于。


1 Under the assumption that signed right shifts of negative numbers do a proper arithmetic shift -- not guarenteed by the C spec, but common on most implementations 1在负数的有符号右移进行适当算术移位的假设下——不受 C 规范的保证,但在大多数实现中都很常见

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM