Skip to content

Commit

Permalink
Revert "Remove long double conversion work-arounds (HDFGroup#3097)"
Browse files Browse the repository at this point in the history
This reverts commit 1e1dac1.
  • Loading branch information
derobins committed Jun 15, 2023
1 parent 69e8c8f commit f1f17b2
Show file tree
Hide file tree
Showing 10 changed files with 733 additions and 25 deletions.
93 changes: 93 additions & 0 deletions config/cmake/ConfigureChecks.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,19 @@ if (HDF5_STRICT_FORMAT_CHECKS)
endif ()
MARK_AS_ADVANCED (HDF5_STRICT_FORMAT_CHECKS)

# ----------------------------------------------------------------------
# Decide whether the data accuracy has higher priority during data
# conversions. If not, some hard conversions will still be preferred even
# though the data may be wrong (for example, some compilers don't
# support denormalized floating values) to maximize speed.
#-----------------------------------------------------------------------------
option (HDF5_WANT_DATA_ACCURACY "IF data accuracy is guaranteed during data conversions" ON)
mark_as_advanced (HDF5_WANT_DATA_ACCURACY)
if (HDF5_WANT_DATA_ACCURACY)
set (${HDF_PREFIX}_WANT_DATA_ACCURACY 1)
endif ()
MARK_AS_ADVANCED (HDF5_WANT_DATA_ACCURACY)

# ----------------------------------------------------------------------
# Decide whether the presence of user's exception handling functions is
# checked and data conversion exceptions are returned. This is mainly
Expand Down Expand Up @@ -847,3 +860,83 @@ if (HDF5_BUILD_FORTRAN)
message (STATUS "maximum decimal precision for C var - ${${HDF_PREFIX}_PAC_C_MAX_REAL_PRECISION}")

endif()

#-----------------------------------------------------------------------------
# Macro to determine the various conversion capabilities
#-----------------------------------------------------------------------------
macro (H5ConversionTests TEST msg)
if (NOT DEFINED ${TEST})
TRY_RUN (${TEST}_RUN ${TEST}_COMPILE
${CMAKE_BINARY_DIR}
${HDF_RESOURCES_DIR}/ConversionTests.c
CMAKE_FLAGS -DCOMPILE_DEFINITIONS:STRING=-D${TEST}_TEST
OUTPUT_VARIABLE OUTPUT
)
if (${TEST}_COMPILE)
if (${TEST}_RUN EQUAL "0")
set (${TEST} 1 CACHE INTERNAL ${msg})
message (VERBOSE "${msg}... yes")
else ()
set (${TEST} "" CACHE INTERNAL ${msg})
message (VERBOSE "${msg}... no")
file (APPEND ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log
"Test ${TEST} Run failed with the following output and exit code:\n ${OUTPUT}\n"
)
endif ()
else ()
set (${TEST} "" CACHE INTERNAL ${msg})
message (VERBOSE "${msg}... no")
file (APPEND ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeError.log
"Test ${TEST} Compile failed with the following output:\n ${OUTPUT}\n"
)
endif ()

endif ()
endmacro ()

#-----------------------------------------------------------------------------
# Check various conversion capabilities
#-----------------------------------------------------------------------------

# ----------------------------------------------------------------------
# Set the flag to indicate that the machine is using a special algorithm to convert
# 'long double' to '(unsigned) long' values. (This flag should only be set for
# the IBM Power6 Linux. When the bit sequence of long double is
# 0x4351ccf385ebc8a0bfcc2a3c3d855620, the converted value of (unsigned)long
# is 0x004733ce17af227f, not the same as the library's conversion to 0x004733ce17af2282.
# The machine's conversion gets the correct value. We define the macro and disable
# this kind of test until we figure out what algorithm they use.
#-----------------------------------------------------------------------------
H5ConversionTests (${HDF_PREFIX}_LDOUBLE_TO_LONG_SPECIAL "Checking IF your system converts long double to (unsigned) long values with special algorithm")
# ----------------------------------------------------------------------
# Set the flag to indicate that the machine is using a special algorithm
# to convert some values of '(unsigned) long' to 'long double' values.
# (This flag should be off for all machines, except for IBM Power6 Linux,
# when the bit sequences are 003fff..., 007fff..., 00ffff..., 01ffff...,
# ..., 7fffff..., the compiler uses a unknown algorithm. We define a
# macro and skip the test for now until we know about the algorithm.
#-----------------------------------------------------------------------------
H5ConversionTests (${HDF_PREFIX}_LONG_TO_LDOUBLE_SPECIAL "Checking IF your system can convert (unsigned) long to long double values with special algorithm")
# ----------------------------------------------------------------------
# Set the flag to indicate that the machine can accurately convert
# 'long double' to '(unsigned) long long' values. (This flag should be set for
# all machines, except for Mac OS 10.4 and SGI IRIX64 6.5. When the bit sequence
# of long double is 0x4351ccf385ebc8a0bfcc2a3c..., the values of (unsigned)long long
# start to go wrong on these two machines. Adjusting it higher to
# 0x4351ccf385ebc8a0dfcc... or 0x4351ccf385ebc8a0ffcc... will make the converted
# values wildly wrong. This test detects this wrong behavior and disable the test.
#-----------------------------------------------------------------------------
H5ConversionTests (${HDF_PREFIX}_LDOUBLE_TO_LLONG_ACCURATE "Checking IF correctly converting long double to (unsigned) long long values")
# ----------------------------------------------------------------------
# Set the flag to indicate that the machine can accurately convert
# '(unsigned) long long' to 'long double' values. (This flag should be set for
# all machines, except for Mac OS 10.4, when the bit sequences are 003fff...,
# 007fff..., 00ffff..., 01ffff..., ..., 7fffff..., the converted values are twice
# as big as they should be.
#-----------------------------------------------------------------------------
H5ConversionTests (${HDF_PREFIX}_LLONG_TO_LDOUBLE_CORRECT "Checking IF correctly converting (unsigned) long long to long double values")
# ----------------------------------------------------------------------
# Set the flag to indicate that the machine can accurately convert
# some long double values
#-----------------------------------------------------------------------------
H5ConversionTests (${HDF_PREFIX}_DISABLE_SOME_LDOUBLE_CONV "Checking IF the cpu is power9 and cannot correctly converting long double values")
284 changes: 284 additions & 0 deletions config/cmake/ConversionTests.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Copyright by The HDF Group. *
* All rights reserved. *
* *
* This file is part of HDF5. The full HDF5 copyright notice, including *
* terms governing use, modification, and redistribution, is contained in *
* the COPYING file, which can be found at the root of the source code *
* distribution tree, or in https://www.hdfgroup.org/licenses. *
* If you do not have access to either file, you may request a copy from *
* [email protected]. *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#if defined(__has_attribute)
# if __has_attribute(no_sanitize)
# define HDF_NO_UBSAN __attribute__((no_sanitize("undefined")))
# else
# define HDF_NO_UBSAN
# endif
#else
# define HDF_NO_UBSAN
#endif

#ifdef H5_LDOUBLE_TO_LONG_SPECIAL_TEST

#include <string.h>
#include <stdlib.h>

int HDF_NO_UBSAN main(void)
{
long double ld = 20041683600089727.779961L;
long ll;
unsigned long ull;
unsigned char s[16];
unsigned char s2[8];
int ret = 1;

if (sizeof(long double) == 16 && sizeof(long) == 8) {
/* Make sure the long double type has 16 bytes in size and
* 11 bits of exponent. If it is, the bit sequence should be
* like below. It's not a decent way to check but this info
* isn't available.
*/
memcpy(s, &ld, 16);
if (s[0]==0x43 && s[1]==0x51 && s[2]==0xcc && s[3]==0xf3 &&
s[4]==0x85 && s[5]==0xeb && s[6]==0xc8 && s[7]==0xa0 &&
s[8]==0xbf && s[9]==0xcc && s[10]==0x2a && s[11]==0x3c) {

/* Assign the hexadecimal value of long double type. */
s[0]=0x43; s[1]=0x51; s[2]=0xcc; s[3]=0xf3;
s[4]=0x85; s[5]=0xeb; s[6]=0xc8; s[7]=0xa0;
s[8]=0xbf; s[9]=0xcc; s[10]=0x2a; s[11]=0x3c;
s[12]=0x3d; s[13]=0x85; s[14]=0x56; s[15]=0x20;

memcpy(&ld, s, 16);

ll = (long)ld;
memcpy(s2, &ll, 8);

/* The library's algorithm converts it to 0x 00 47 33 ce 17 af 22 82
* and gets wrong value 20041683600089730 on the IBM Power6 Linux.
* But the IBM Power6 Linux converts it to 0x00 47 33 ce 17 af 22 7f
* and gets the correct value 20041683600089727. It uses some special
* algorithm. We're going to define the macro and skip the test until
* we can figure out how they do it.
*/
if (s2[0]==0x00 && s2[1]==0x47 && s2[2]==0x33 && s2[3]==0xce &&
s2[4]==0x17 && s2[5]==0xaf && s2[6]==0x22 && s2[7]==0x7f)

ret = 0;

ull = (unsigned long)ld;
memcpy(s2, &ull, 8);

/* The unsigned long is the same as signed long */
if(s2[0]==0x00 && s2[1]==0x47 && s2[2]==0x33 && s2[3]==0xce &&
s2[4]==0x17 && s2[5]==0xaf && s2[6]==0x22 && s2[7]==0x7f)

ret = 0;
}
}

done:
exit(ret);
}

#endif

#ifdef H5_LONG_TO_LDOUBLE_SPECIAL_TEST

#include <string.h>
#include <stdlib.h>

int HDF_NO_UBSAN main(void)
{
long double ld;
long ll;
unsigned long ull;
unsigned char s[16];
int flag=0, ret=1;

/* Determine if long double has 16 byte in size, 11 bit exponent, and
* the bias is 0x3ff
*/
if (sizeof(long double) == 16) {
ld = 1.0L;
memcpy(s, &ld, 16);

if (s[0]==0x3f && s[1]==0xf0 && s[2]==0x00 && s[3]==0x00 &&
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00)

flag = 1;
}

if (flag==1 && sizeof(long)==8) {
ll = 0x003fffffffffffffL;
ld = (long double)ll;
memcpy(s, &ld, 16);

/* The library converts the value to 0x434fffffffffffff8000000000000000.
* In decimal it is 18014398509481982.000000, one value short of the original.
* The IBM Power6 Linux converts it to 0x4350000000000000bff0000000000000.
* The value is correct in decimal. It uses some special
* algorithm. We're going to define the macro and skip the test until
* we can figure out how they do it.
*/
if (s[0]==0x43 && s[1]==0x50 && s[2]==0x00 && s[3]==0x00 &&
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00 &&
s[8]==0xbf && s[9]==0xf0 && s[10]==0x00 && s[11]==0x00 &&
s[12]==0x00 && s[13]==0x00 && s[14]==0x00 && s[15]==0x00)

ret = 0;
}

if (flag==1 && sizeof(unsigned long)==8) {
ull = 0xffffffffffffffffUL;
ld = (long double)ull;
memcpy(s, &ld, 16);

/* Use a different value from signed long to test. The problem is the same
* for both long and unsigned long. The value is 18446744073709551615.
* The library converts the value to 0x43effffffffffffffe000000000000000.
* In decimal it's 18446744073709548544.000000, very different from the original.
* The IBM Power6 Linux converts it to 0x43f0000000000000bff0000000000000.
* The value is correct in decimal. It uses some special
* algorithm. We're going to define the macro and skip the test until
* we can figure out how they do it.
*/
if (s[0]==0x43 && s[1]==0xf0 && s[2]==0x00 && s[3]==0x00 &&
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00 &&
s[8]==0xbf && s[9]==0xf0 && s[10]==0x00 && s[11]==0x00 &&
s[12]==0x00 && s[13]==0x00 && s[14]==0x00 && s[15]==0x00)

ret = 0;
}
done:
exit(ret);
}

#endif

#ifdef H5_LDOUBLE_TO_LLONG_ACCURATE_TEST

#include <string.h>
#include <stdlib.h>

int HDF_NO_UBSAN main(void)
{
long double ld = 20041683600089727.779961L;
long long ll;
unsigned long long ull;
unsigned char s[16];
int ret = 0;

if (sizeof(long double) == 16) {
/* Make sure the long double type is the same as the failing type
* which has 16 bytes in size and 11 bits of exponent. If it is,
* the bit sequence should be like below. It's not
* a decent way to check but this info isn't available.
*/
memcpy(s, &ld, 16);

if (s[0]==0x43 && s[1]==0x51 && s[2]==0xcc && s[3]==0xf3 &&
s[4]==0x85 && s[5]==0xeb && s[6]==0xc8 && s[7]==0xa0 &&
s[8]==0xbf && s[9]==0xcc && s[10]==0x2a && s[11]==0x3c) {

/* Slightly adjust the bit sequence (s[8]=0xdf). The converted
* values will go wild on Mac OS 10.4 and IRIX64 6.5.
*/
s[0]=0x43; s[1]=0x51; s[2]=0xcc; s[3]=0xf3;
s[4]=0x85; s[5]=0xeb; s[6]=0xc8; s[7]=0xa0;
s[8]=0xdf; s[9]=0xcc; s[10]=0x2a; s[11]=0x3c;
s[12]=0x3d; s[13]=0x85; s[14]=0x56; s[15]=0x20;

memcpy(&ld, s, 16);
ll = (long long)ld;
ull = (unsigned long long)ld;

if (ll != 20041683600089728 || ull != 20041683600089728)
ret = 1;
}
}
done:
exit(ret);
}
#endif

#ifdef H5_LLONG_TO_LDOUBLE_CORRECT_TEST

#include <string.h>
#include <stdlib.h>

int HDF_NO_UBSAN main(void)
{
long double ld;
long long ll;
unsigned long long ull;
unsigned char s[16];
int flag=0, ret=0;

/* Determine if long double has 16 byte in size, 11 bit exponent, and
* the bias is 0x3ff
*/
if (sizeof(long double) == 16) {
ld = 1.0L;
memcpy(s, &ld, 16);
if (s[0]==0x3f && s[1]==0xf0 && s[2]==0x00 && s[3]==0x00 &&
s[4]==0x00 && s[5]==0x00 && s[6]==0x00 && s[7]==0x00)

flag = 1;
}

if (flag==1 && sizeof(long long)==8) {
ll = 0x01ffffffffffffffLL;
ld = (long double)ll;
memcpy(s, &ld, 16);

/* Check if the bit sequence is as expected*/
if (s[0]!=0x43 || s[1]!=0x7f || s[2]!=0xff || s[3]!=0xff ||
s[4]!=0xff || s[5]!=0xff || s[6]!=0xff || s[7]!=0xff ||
s[8]!=0xf0 || s[9]!=0x00 || s[10]!=0x00 || s[11]!=0x00)

ret = 1;
}
if (flag==1 && sizeof(unsigned long long)==8) {
ull = 0x01ffffffffffffffULL;
ld = (long double)ull;
memcpy(s, &ld, 16);

if (s[0]!=0x43 || s[1]!=0x7f || s[2]!=0xff || s[3]!=0xff ||
s[4]!=0xff || s[5]!=0xff || s[6]!=0xff || s[7]!=0xff ||
s[8]!=0xf0 || s[9]!=0x00 || s[10]!=0x00 || s[11]!=0x00)

ret = 1;
}

done:
exit(ret);
}
#endif

#ifdef H5_DISABLE_SOME_LDOUBLE_CONV_TEST

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int HDF_NO_UBSAN main(void)
{
FILE *fp;
char cpu[64];

fp = popen("uname -m", "r");

fgets(cpu, sizeof(cpu)-1, fp);

pclose(fp);

if (strncmp(cpu, "ppc64le", 7) == 0)
return 0;

return 1;
}

#endif
Loading

0 comments on commit f1f17b2

Please sign in to comment.