From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) (using TLSv1 with cipher CAMELLIA256-SHA (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id D53871A1E0B for ; Tue, 27 Sep 2016 10:27:12 -0700 (PDT) Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga104.fm.intel.com with ESMTP; 27 Sep 2016 10:27:13 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.30,405,1470726000"; d="scan'208";a="13775501" Received: from orsmsx102.amr.corp.intel.com ([10.22.225.129]) by fmsmga006.fm.intel.com with ESMTP; 27 Sep 2016 10:27:12 -0700 Received: from orsmsx113.amr.corp.intel.com ([169.254.9.161]) by ORSMSX102.amr.corp.intel.com ([169.254.3.44]) with mapi id 14.03.0248.002; Tue, 27 Sep 2016 10:27:12 -0700 From: "Kinney, Michael D" To: "afish@apple.com" , Eugene Cohen , "Kinney, Michael D" CC: Laszlo Ersek , Alexei Fedorov , "edk2-devel@lists.01.org" Thread-Topic: [edk2] What is the right way to print a UINTN? Thread-Index: AdIX/FZUkJ+/GDfSRZe7u7ShyrQtxgASWdEAACvtKYAABDdLAAADQGEAAAGJsAAADkOVQA== Date: Tue, 27 Sep 2016 17:27:11 +0000 Message-ID: References: <0de4dd03-faa7-1608-9625-369ab5d6e682@redhat.com> In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ctpclassification: CTP_IC x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiYjM4ZmVhODEtMzc0Ny00YTVmLTlhMWYtNDNmMGE0ODE4NTZjIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX0lDIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE1LjkuNi42IiwiVHJ1c3RlZExhYmVsSGFzaCI6IjRqN2RvQm91TjgzK1MrZXJYMElKWDI1d3FmMzVXd1VHSnc1UlFzdzV3R2M9In0= x-originating-ip: [10.22.254.138] MIME-Version: 1.0 Subject: Re: What is the right way to print a UINTN? X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 27 Sep 2016 17:27:13 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Andrew, Here is the comment in current code that explains some of the why. case 'd': if ((Flags & LONG_TYPE) =3D=3D 0) { // // 'd', 'u', 'x', and 'X' that are not preceded by 'l' or 'L' are= assumed to be type "int". // This assumption is made so the format string definition is com= patible with the ANSI C // Specification for formatted strings. It is recommended that t= he Base Types be used=20 // everywhere, but in this one case, compliance with ANSI C is mo= re important, and=20 // provides an implementation that is compatible with that larges= t possible set of CPU=20 // architectures. This is why the type "int" is used in this one= case. // If code uses type UINTN or INTN, then the max value that can be used=20 for portable sources is max value for the CPU arch with the smallest UINTN width. For the CPU archs in UEFI/PI specs today, the smallest UINTN width is 32-bits. Portable sources that use type UINTN must never use values larger than 32-bits. Same for type INTN. Only use values in signed 32-bit range. It is possible for 64-bit specific sources to use type UINTN with values larger than 32-bits, but those sources would not be portable and would need to be changed to UINT64 to be portable. Is this the case that are really discussing here? Thanks, Mike > -----Original Message----- > From: afish@apple.com [mailto:afish@apple.com] > Sent: Tuesday, September 27, 2016 9:47 AM > To: Eugene Cohen > Cc: Laszlo Ersek ; Kinney, Michael D ; > Alexei Fedorov ; edk2-devel@lists.01.org devel@ml01.01.org> > Subject: Re: [edk2] What is the right way to print a UINTN? >=20 >=20 > > On Sep 27, 2016, at 9:03 AM, Cohen, Eugene wrote: > > > >> Printing UINTN with %x *or* with %d are equally bugs. > >> > >> For X64 / AARCH64 / IA64 builds, they are actual bugs (that happen to > >> work most of the time). > > > > Feel free to file a Bugzilla on the extensive usage of this in edk2 [du= cking and > running]. :) > > > >>> I'm envisioning having to create a slide in the future for UEFI > >>> training about the proper use of UINTNs and describing "If you think > >>> it may exceed 2^32-1 then upcast to UINT64, otherwise don't worry > >>> about it" and it makes me squirm. > >> > >> It makes me squirm too. I think the slide should recommend the > >> casting > >> that I proposed. ;) "There is no conversion specifier dedicated to > >> UINTN; the portable way to print it is to cast it to UINT64, then prin= t > >> it with %Lx." > > > > This is reasonable although I expect to get asked why a lot of the othe= r code doesn't > adhere to this recommendation. > > >=20 > I think this is a historical artifact. The older version of %x in the EDK= (and early > edk2) implied UINTN. We hit an issue with C integer math resulting in an = int and that > seemed to bork some toolchains. That is when things changed from UINTN to= int. I guess > the cleanup was practical vs. pedantic. >=20 > Thanks, >=20 > Andrew Fish >=20 > > Thanks, > > > > Eugene > > _______________________________________________ > > edk2-devel mailing list > > edk2-devel@lists.01.org > > https://lists.01.org/mailman/listinfo/edk2-devel