fortran 64位十六进制BOZ [英] fortran 64 bit hex BOZ

查看:281
本文介绍了fortran 64位十六进制BOZ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中可以接受:

in C++ this is accepted:

uint64_t mask = 0x7FC0000FF80001FFLL;

但在fortran中

integer(kind=8), parameter :: mask = Z'7FC0000FF80001FF'

不适用于gfortan. 我认为它们都是64位值?还是不?

does not work with gfortan. I think both of them are 64bit values? or not?

gfortran抱怨:

gfortran complains:

从INTEGER(16)转换为INTEGER(8)的算术溢出

arithmetic overflow from converting INTEGER(16) to INTEGER(8)

因此,对于造成混乱的情况,我们深表歉意,这里是一些更扩展的问题说明. 我将在Fortran中进行一些移位,并在c ++中提供一些示例代码. 在示例c ++代码中,掩码的定义如下:

So, sorry for the confusion, here is some more extended problem description. I will do some bit shifting in Fortran and have some sample code in c++. There in the sample c++ code the masks are defines like:

typedef uint64_t mask;
static const mask dilate_2 = (mask)0x7FC0000FF80001FFLL ;
static const mask dilate_1 = (mask)0x01C0E070381C0E07LL ;
static const mask dilate_0 = (mask)0x9249249249249249LL ;

根据我对C ++的较差理解,我认为十六进制值是64位 整数值(它们的末尾有LL). 现在在Fortran中,我首先遇到的问题是,

From my poor c++ understanding, I think that the hex values are 64bit integer values (they have LL in the ending). Now in Fortran my problem first was, that the definition with

integer(kind=8), parameter ...  

像弗拉基米尔所说的那样没有用,因为

did not work, as Vladimir said, because

integer(kind=8), ...

可能不是64位整数.
比我测试过的Alexanders解决方案更适用于第一个和第二个 第二个(dilate_2,dilate_1)常量.
Vladimirs解决方案也适用于这两个.
现在,对于dilate_0,这些解决方案均无效.我想Vladimirs解决方案将投放0x9249249249249249LLLL(实际上是 大于INT64允许的整数)到INT64
如果我这样做:

might be no 64bit integer.
Than I tested Alexanders solution, which works for the first and the second (dilate_2, dilate_1) constant.
Also Vladimirs solution works for these two.
Now for dilate_0 none of these solutions work. I would suppose that Vladimirs solution will cast 0x9249249249249249LL (what is actually a greater integer than allowed in INT64) into a INT64
if I do:

integer(INT64), parameter :: dilate_0 = int(Z'9249249249249249', &
                             kind=kind(dilate_0)

但这也不起作用,gfortran给我一个错误:

But this also don't work and gfortran give me an error:

错误:在(1)处将INTEGER(16)转换为INTEGER(8)的算术溢出.

Error: Arithmetic overflow converting INTEGER(16) to INTEGER(8) at (1).

所以我的实际问题是如何在Fortran中实现此常量?

So my actual question is how to implement this constant in Fortran?

推荐答案

正如弗拉基米尔(Vladimir)在其评论中发布的,integer(kind=8)不可移植(编译器抱怨的也不是16类型).

As Vladimir posted in his comment integer(kind=8) is not portable (and not of kind 16 as the compiler complains).

作为一种补救措施,我建议使用内部模块ISO_Fortran_env(Fortran 2003),该模块具有许多

As a remedy I suggest to use the intrinsic module ISO_Fortran_env (Fortran 2003) which has many predefined constants for the compiler used. Using INT64 from this module solves your problem and results in portable code:

program test
  use,intrinsic :: ISO_Fortran_env, only: INT64
  integer(INT64), parameter :: mask = Z'7FC0000FF80001FF'

  print *,mask
end program

这篇关于fortran 64位十六进制BOZ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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