11.3Smaya/* $NetBSD: nan.c,v 1.3 2017/03/07 11:15:08 maya Exp $ */
21.1Skleink
31.1Skleink/*-
41.1Skleink * Copyright (c) 2006 The NetBSD Foundation, Inc.
51.1Skleink * All rights reserved.
61.1Skleink *
71.1Skleink * This code is derived from software contributed to The NetBSD Foundation
81.1Skleink * by Klaus Klein.
91.1Skleink *
101.1Skleink * Redistribution and use in source and binary forms, with or without
111.1Skleink * modification, are permitted provided that the following conditions
121.1Skleink * are met:
131.1Skleink * 1. Redistributions of source code must retain the above copyright
141.1Skleink *    notice, this list of conditions and the following disclaimer.
151.1Skleink * 2. Redistributions in binary form must reproduce the above copyright
161.1Skleink *    notice, this list of conditions and the following disclaimer in the
171.1Skleink *    documentation and/or other materials provided with the distribution.
181.1Skleink *
191.1Skleink * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
201.1Skleink * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
211.1Skleink * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
221.1Skleink * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
231.1Skleink * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
241.1Skleink * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
251.1Skleink * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
261.1Skleink * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
271.1Skleink * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
281.1Skleink * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
291.1Skleink * POSSIBILITY OF SUCH DAMAGE.
301.1Skleink */
311.1Skleink
321.1Skleink#include <sys/cdefs.h>
331.1Skleink#if defined(LIBM_SCCS) && !defined(lint) && !defined(NAN_FUNCTION)
341.3Smaya__RCSID("$NetBSD: nan.c,v 1.3 2017/03/07 11:15:08 maya Exp $");
351.1Skleink#endif /* LIBM_SCCS and not lint */
361.1Skleink
371.1Skleink#include <assert.h>
381.1Skleink#include <math.h>
391.1Skleink#include <stddef.h>
401.1Skleink#include <stdlib.h>
411.1Skleink#include <string.h>
421.1Skleink
431.1Skleink
441.1Skleink#ifndef NAN_FUNCTION
451.1Skleink#define	NAN_FUNCTION	nan
461.1Skleink#define	NAN_TYPE	double
471.1Skleink#define	NAN_STRTOD	strtod
481.1Skleink#endif
491.1Skleink
501.1SkleinkNAN_TYPE
511.1SkleinkNAN_FUNCTION(const char *tagp)
521.1Skleink{
531.1Skleink	const char *nstr;
541.1Skleink	char *buf;
551.1Skleink	NAN_TYPE res;
561.1Skleink
571.1Skleink	_DIAGASSERT(tagp != NULL);
581.1Skleink
591.1Skleink	nstr = "NAN()";
601.1Skleink	buf = NULL;
611.1Skleink
621.1Skleink	if (tagp[0] != '\0') {
631.1Skleink		size_t l;
641.1Skleink
651.1Skleink		l = strlen(tagp);
661.1Skleink		buf = malloc(5 + l + 1);
671.1Skleink
681.1Skleink		if (buf != NULL) {
691.1Skleink			/* Avoiding stdio in libm. */
701.1Skleink			memcpy(buf,		"NAN(",	4);
711.1Skleink			memcpy(buf + 4,		tagp,	l);
721.1Skleink			memcpy(buf + 4 + l,	")",	2);
731.1Skleink			nstr = buf;
741.1Skleink		} else {
751.1Skleink			/* Best effort: Fall back to "NAN()". */
761.1Skleink		}
771.1Skleink	}
781.1Skleink
791.1Skleink	res = NAN_STRTOD(nstr, NULL);
801.1Skleink
811.3Smaya	free(buf);
821.1Skleink
831.1Skleink	return res;
841.1Skleink}
85