Simplified uniform GPU selection in CMake
[gromacs.git] / src / gromacs / fileio / gmx_internal_xdr.cpp
blob2ae62422b54b9cd64064020a0af9d5f4776fc0da
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2018,2019, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gmxpre.h"
39 #include "config.h"
41 #if GMX_INTERNAL_XDR
44 # include "gmx_internal_xdr.h"
46 # include <cstdlib>
47 # include <cstring>
50 /* NB - THIS FILE IS ONLY USED ON MICROSOFT WINDOWS, since that
51 * system doesn't provide any standard XDR system libraries. It will
52 * most probably work on other platforms too, but make sure you
53 * test that the xtc files produced are ok before using it.
55 * This header file contains Gromacs versions of the definitions for
56 * Sun External Data Representation (XDR) headers and routines.
58 * On most UNIX systems this is already present as part of your
59 * system libraries, but since we want to make Gromacs portable to
60 * platforms like Microsoft Windows we have created a private version
61 * of the necessary routines and distribute them with the Gromacs source.
63 * Although the rest of Gromacs is LGPL, you can copy and use the XDR
64 * routines in any way you want as long as you obey Sun's license:
66 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
67 * unrestricted use provided that this legend is included on all tape
68 * media and as a part of the software program in whole or part. Users
69 * may copy or modify Sun RPC without charge, but are not authorized
70 * to license or distribute it to anyone else except as part of a product or
71 * program developed by the user.
73 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
74 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
75 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
77 * Sun RPC is provided with no support and without any obligation on the
78 * part of Sun Microsystems, Inc. to assist in its use, correction,
79 * modification or enhancement.
81 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
82 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
83 * OR ANY PART THEREOF.
85 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
86 * or profits or other special, indirect and consequential damages, even if
87 * Sun has been advised of the possibility of such damages.
89 * Sun Microsystems, Inc.
90 * 2550 Garcia Avenue
91 * Mountain View, California 94043
96 * for unit alignment
98 static char xdr_zero[BYTES_PER_XDR_UNIT] = { 0, 0, 0, 0 };
100 static xdr_uint32_t xdr_swapbytes(xdr_uint32_t x)
102 xdr_uint32_t y;
103 int i;
104 char* px = reinterpret_cast<char*>(&x);
105 char* py = reinterpret_cast<char*>(&y);
107 for (i = 0; i < 4; i++)
109 py[i] = px[3 - i];
112 return y;
115 static xdr_uint32_t xdr_htonl(xdr_uint32_t x)
117 short s = 0x0F00;
118 if (*(reinterpret_cast<char*>(&s)) == static_cast<char>(0x0F))
120 /* bigendian, do nothing */
121 return x;
123 else
125 /* smallendian,swap bytes */
126 return xdr_swapbytes(x);
130 static xdr_uint32_t xdr_ntohl(xdr_uint32_t x)
132 short s = 0x0F00;
133 if (*(reinterpret_cast<char*>(&s)) == static_cast<char>(0x0F))
135 /* bigendian, do nothing */
136 return x;
138 else
140 /* smallendian, swap bytes */
141 return xdr_swapbytes(x);
147 * Free a data structure using XDR
148 * Not a filter, but a convenient utility nonetheless
150 void xdr_free(xdrproc_t proc, char* objp)
152 XDR x;
154 x.x_op = XDR_FREE;
155 (*proc)(&x, objp);
159 * XDR nothing
161 bool_t xdr_void()
163 return TRUE;
167 * XDR integers
169 bool_t xdr_int(XDR* xdrs, int* ip)
171 xdr_int32_t l;
173 switch (xdrs->x_op)
175 case XDR_ENCODE: l = static_cast<xdr_int32_t>(*ip); return xdr_putint32(xdrs, &l);
177 case XDR_DECODE:
178 if (!xdr_getint32(xdrs, &l))
180 return FALSE;
182 *ip = static_cast<int>(l);
183 return TRUE;
185 case XDR_FREE: return TRUE;
187 return FALSE;
192 * XDR unsigned integers
194 bool_t xdr_u_int(XDR* xdrs, unsigned int* up)
196 xdr_uint32_t l;
198 switch (xdrs->x_op)
200 case XDR_ENCODE: l = static_cast<xdr_uint32_t>(*up); return xdr_putuint32(xdrs, &l);
202 case XDR_DECODE:
203 if (!xdr_getuint32(xdrs, &l))
205 return FALSE;
207 *up = static_cast<unsigned int>(l);
208 return TRUE;
210 case XDR_FREE: return TRUE;
212 return FALSE;
217 * XDR short integers
219 bool_t xdr_short(XDR* xdrs, short* sp)
221 xdr_int32_t l;
223 switch (xdrs->x_op)
225 case XDR_ENCODE: l = static_cast<xdr_int32_t>(*sp); return xdr_putint32(xdrs, &l);
227 case XDR_DECODE:
228 if (!xdr_getint32(xdrs, &l))
230 return FALSE;
232 *sp = static_cast<short>(l);
233 return TRUE;
235 case XDR_FREE: return TRUE;
237 return FALSE;
242 * XDR unsigned short integers
244 bool_t xdr_u_short(XDR* xdrs, unsigned short* usp)
246 xdr_uint32_t l;
248 switch (xdrs->x_op)
250 case XDR_ENCODE: l = static_cast<xdr_uint32_t>(*usp); return xdr_putuint32(xdrs, &l);
252 case XDR_DECODE:
253 if (!xdr_getuint32(xdrs, &l))
255 return FALSE;
257 *usp = static_cast<unsigned short>(l);
258 return TRUE;
260 case XDR_FREE: return TRUE;
262 return FALSE;
267 * XDR a char
269 bool_t xdr_char(XDR* xdrs, char* cp)
271 int i;
273 i = (*cp);
274 if (!xdr_int(xdrs, &i))
276 return FALSE;
278 *cp = i;
279 return TRUE;
283 * XDR an unsigned char
285 bool_t xdr_u_char(XDR* xdrs, unsigned char* cp)
287 unsigned int u;
289 u = (*cp);
290 if (!xdr_u_int(xdrs, &u))
292 return FALSE;
294 *cp = u;
295 return TRUE;
299 * XDR booleans
301 bool_t xdr_bool(XDR* xdrs, int* bp)
303 # define XDR_FALSE ((xdr_int32_t)0)
304 # define XDR_TRUE ((xdr_int32_t)1)
306 xdr_int32_t lb;
308 switch (xdrs->x_op)
310 case XDR_ENCODE: lb = *bp ? XDR_TRUE : XDR_FALSE; return xdr_putint32(xdrs, &lb);
312 case XDR_DECODE:
313 if (!xdr_getint32(xdrs, &lb))
315 return FALSE;
317 *bp = (lb == XDR_FALSE) ? FALSE : TRUE;
318 return TRUE;
320 case XDR_FREE: return TRUE;
322 return FALSE;
323 # undef XDR_FALSE
324 # undef XDR_TRUE
329 * XDR opaque data
330 * Allows the specification of a fixed size sequence of opaque bytes.
331 * cp points to the opaque object and cnt gives the byte length.
333 bool_t xdr_opaque(XDR* xdrs, char* cp, unsigned int cnt)
335 unsigned int rndup;
336 char crud[BYTES_PER_XDR_UNIT];
339 * if no data we are done
341 if (cnt == 0)
343 return TRUE;
347 * round byte count to full xdr units
349 rndup = cnt % BYTES_PER_XDR_UNIT;
350 if (rndup > 0)
352 rndup = BYTES_PER_XDR_UNIT - rndup;
355 switch (xdrs->x_op)
357 case XDR_DECODE:
358 if (!xdr_getbytes(xdrs, cp, cnt))
360 return FALSE;
362 if (rndup == 0)
364 return TRUE;
366 return xdr_getbytes(xdrs, crud, rndup);
368 case XDR_ENCODE:
369 if (!xdr_putbytes(xdrs, cp, cnt))
371 return FALSE;
373 if (rndup == 0)
375 return TRUE;
377 return xdr_putbytes(xdrs, xdr_zero, rndup);
379 case XDR_FREE: return TRUE;
381 return FALSE;
386 * XDR null terminated ASCII strings
387 * xdr_string deals with "C strings" - arrays of bytes that are
388 * terminated by a NULL character. The parameter cpp references a
389 * pointer to storage; If the pointer is null, then the necessary
390 * storage is allocated. The last parameter is the max allowed length
391 * of the string as specified by a protocol.
393 bool_t xdr_string(XDR* xdrs, char** cpp, unsigned int maxsize)
395 char* sp = *cpp; /* sp is the actual string pointer */
396 unsigned int size = 0;
397 unsigned int nodesize = 0;
400 * first deal with the length since xdr strings are counted-strings
402 switch (xdrs->x_op)
404 case XDR_FREE:
405 if (sp == nullptr)
407 return TRUE; /* already free */
409 size = std::strlen(sp);
410 break;
412 case XDR_ENCODE:
413 if (sp == nullptr)
415 return FALSE;
417 size = std::strlen(sp);
418 break;
419 case XDR_DECODE: break;
422 if (!xdr_u_int(xdrs, &size))
424 return FALSE;
426 if (size > maxsize)
428 return FALSE;
430 nodesize = size + 1;
433 * now deal with the actual bytes
435 switch (xdrs->x_op)
437 case XDR_DECODE:
438 if (nodesize == 0)
440 return TRUE;
442 if (sp == nullptr)
444 *cpp = sp = static_cast<char*>(std::malloc(nodesize));
446 if (sp == nullptr)
448 (void)fputs("xdr_string: out of memory\n", stderr);
449 return FALSE;
451 sp[size] = 0;
452 return xdr_opaque(xdrs, sp, size);
454 case XDR_ENCODE: return xdr_opaque(xdrs, sp, size);
456 case XDR_FREE:
457 free(sp);
458 *cpp = nullptr;
459 return TRUE;
461 return FALSE;
465 /* Floating-point stuff */
467 bool_t xdr_float(XDR* xdrs, float* fp)
469 xdr_int32_t tmp;
471 switch (xdrs->x_op)
474 case XDR_ENCODE:
475 tmp = *(reinterpret_cast<xdr_int32_t*>(fp));
476 return (xdr_putint32(xdrs, &tmp));
478 case XDR_DECODE:
479 if (xdr_getint32(xdrs, &tmp))
481 *(reinterpret_cast<xdr_int32_t*>(fp)) = tmp;
482 return (TRUE);
485 break;
487 case XDR_FREE: return (TRUE);
489 return (FALSE);
493 bool_t xdr_double(XDR* xdrs, double* dp)
496 /* Windows and some other systems dont define double-precision
497 * word order in the header files, so unfortunately we have
498 * to calculate it!
500 * For thread safety, we calculate it every time: locking this would
501 * be more expensive.
503 /*static int LSW=-1;*/ /* Least significant fp word */
504 int LSW = -1; /* Least significant fp word */
507 int* ip;
508 xdr_int32_t tmp[2];
510 if (LSW < 0)
512 double x = 0.987654321; /* Just a number */
514 /* Possible representations in IEEE double precision:
515 * (S=small endian, B=big endian)
517 * Byte order, Word order, Hex
518 * S S b8 56 0e 3c dd 9a ef 3f
519 * B S 3c 0e 56 b8 3f ef 9a dd
520 * S B dd 9a ef 3f b8 56 0e 3c
521 * B B 3f ef 9a dd 3c 0e 56 b8
524 unsigned char ix = *(reinterpret_cast<char*>(&x));
526 if (ix == 0xdd || ix == 0x3f)
528 LSW = 1; /* Big endian word order */
530 else if (ix == 0xb8 || ix == 0x3c)
532 LSW = 0; /* Small endian word order */
534 else /* Catch strange errors */
536 printf("Error when detecting floating-point word order.\n"
537 "Do you have a non-IEEE system?\n"
538 "If possible, use the XDR libraries provided with your system,\n"
539 "instead of the GROMACS fallback XDR source.\n");
540 exit(0);
544 switch (xdrs->x_op)
547 case XDR_ENCODE:
548 ip = reinterpret_cast<int*>(dp);
549 tmp[0] = ip[bool(LSW == 0)];
550 tmp[1] = ip[LSW];
551 return static_cast<bool_t>(bool(xdr_putint32(xdrs, tmp)) && bool(xdr_putint32(xdrs, tmp + 1)));
553 case XDR_DECODE:
554 ip = reinterpret_cast<int*>(dp);
555 if (xdr_getint32(xdrs, tmp + !LSW) && xdr_getint32(xdrs, tmp + LSW))
557 ip[0] = tmp[0];
558 ip[1] = tmp[1];
559 return (TRUE);
562 break;
564 case XDR_FREE: return (TRUE);
566 return (FALSE);
570 /* Array routines */
573 * xdr_vector():
575 * XDR a fixed length array. Unlike variable-length arrays,
576 * the storage of fixed length arrays is static and unfreeable.
577 * > basep: base of the array
578 * > size: size of the array
579 * > elemsize: size of each element
580 * > xdr_elem: routine to XDR each element
582 bool_t xdr_vector(XDR* xdrs, char* basep, unsigned int nelem, unsigned int elemsize, xdrproc_t xdr_elem)
584 # define LASTUNSIGNED ((unsigned int)0 - 1)
585 unsigned int i;
586 char* elptr;
588 elptr = basep;
589 for (i = 0; i < nelem; i++)
591 if (!(*xdr_elem)(xdrs, elptr, LASTUNSIGNED))
593 return FALSE;
595 elptr += elemsize;
597 return TRUE;
598 # undef LASTUNSIGNED
602 static bool_t xdrstdio_getbytes(XDR* /*xdrs*/, char* /*addr*/, unsigned int /*len*/);
603 static bool_t xdrstdio_putbytes(XDR* /*xdrs*/, char* /*addr*/, unsigned int /*len*/);
604 static unsigned int xdrstdio_getpos(XDR* /*xdrs*/);
605 static bool_t xdrstdio_setpos(XDR* /*xdrs*/, unsigned int /*pos*/);
606 static xdr_int32_t* xdrstdio_inline(XDR* /*xdrs*/, int /*len*/);
607 static void xdrstdio_destroy(XDR* /*xdrs*/);
608 static bool_t xdrstdio_getint32(XDR* /*xdrs*/, xdr_int32_t* /*ip*/);
609 static bool_t xdrstdio_putint32(XDR* /*xdrs*/, xdr_int32_t* /*ip*/);
610 static bool_t xdrstdio_getuint32(XDR* /*xdrs*/, xdr_uint32_t* /*ip*/);
611 static bool_t xdrstdio_putuint32(XDR* /*xdrs*/, xdr_uint32_t* /*ip*/);
614 * Destroy a stdio xdr stream.
615 * Cleans up the xdr stream handle xdrs previously set up by xdrstdio_create.
617 static void xdrstdio_destroy(XDR* xdrs)
619 fflush(reinterpret_cast<FILE*>(xdrs->x_private));
620 /* xx should we close the file ?? */
624 static bool_t xdrstdio_getbytes(XDR* xdrs, char* addr, unsigned int len)
626 if ((len != 0)
627 && (fread(addr, static_cast<int>(len), 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1))
629 return FALSE;
631 return TRUE;
634 static bool_t xdrstdio_putbytes(XDR* xdrs, char* addr, unsigned int len)
636 if ((len != 0)
637 && (fwrite(addr, static_cast<int>(len), 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1))
639 return FALSE;
641 return TRUE;
644 static unsigned int xdrstdio_getpos(XDR* xdrs)
646 return static_cast<int>(ftell(reinterpret_cast<FILE*>(xdrs->x_private)));
649 static bool_t xdrstdio_setpos(XDR* xdrs, unsigned int pos)
651 return fseek(reinterpret_cast<FILE*>(xdrs->x_private), static_cast<xdr_int32_t>(pos), 0) < 0 ? FALSE
652 : TRUE;
655 static xdr_int32_t* xdrstdio_inline(XDR* xdrs, int len)
657 (void)xdrs;
658 (void)len;
660 * Must do some work to implement this: must insure
661 * enough data in the underlying stdio buffer,
662 * that the buffer is aligned so that we can indirect through a
663 * long *, and stuff this pointer in xdrs->x_buf. Doing
664 * a fread or fwrite to a scratch buffer would defeat
665 * most of the gains to be had here and require storage
666 * management on this buffer, so we don't do this.
668 return nullptr;
671 static bool_t xdrstdio_getint32(XDR* xdrs, xdr_int32_t* ip)
673 xdr_int32_t mycopy;
675 if (fread(&mycopy, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
677 return FALSE;
679 *ip = xdr_ntohl(mycopy);
680 return TRUE;
683 static bool_t xdrstdio_putint32(XDR* xdrs, xdr_int32_t* ip)
685 xdr_int32_t mycopy = xdr_htonl(*ip);
687 ip = &mycopy;
688 if (fwrite(ip, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
690 return FALSE;
692 return TRUE;
695 static bool_t xdrstdio_getuint32(XDR* xdrs, xdr_uint32_t* ip)
697 xdr_uint32_t mycopy;
699 if (fread(&mycopy, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
701 return FALSE;
703 *ip = xdr_ntohl(mycopy);
704 return TRUE;
707 static bool_t xdrstdio_putuint32(XDR* xdrs, xdr_uint32_t* ip)
709 xdr_uint32_t mycopy = xdr_htonl(*ip);
711 ip = &mycopy;
712 if (fwrite(ip, 4, 1, reinterpret_cast<FILE*>(xdrs->x_private)) != 1)
714 return FALSE;
716 return TRUE;
720 * Ops vector for stdio type XDR
722 static struct XDR::xdr_ops xdrstdio_ops = {
723 xdrstdio_getbytes, /* deserialize counted bytes */
724 xdrstdio_putbytes, /* serialize counted bytes */
725 xdrstdio_getpos, /* get offset in the stream */
726 xdrstdio_setpos, /* set offset in the stream */
727 xdrstdio_inline, /* prime stream for inline macros */
728 xdrstdio_destroy, /* destroy stream */
729 xdrstdio_getint32, /* deserialize a int */
730 xdrstdio_putint32, /* serialize a int */
731 xdrstdio_getuint32, /* deserialize a int */
732 xdrstdio_putuint32 /* serialize a int */
736 * Initialize a stdio xdr stream.
737 * Sets the xdr stream handle xdrs for use on the stream file.
738 * Operation flag is set to op.
740 void xdrstdio_create(XDR* xdrs, FILE* file, enum xdr_op op)
742 xdrs->x_op = op;
743 xdrs->x_ops = &xdrstdio_ops;
744 xdrs->x_private = reinterpret_cast<char*>(file);
745 xdrs->x_handy = 0;
746 xdrs->x_base = nullptr;
748 #endif /* GMX_INTERNAL_XDR */