xref: /titanic_50/usr/src/lib/libshell/common/features/math.sh (revision 3e14f97f673e8a630f076077de35afdd43dc1587)
1da2e3ebdSchin########################################################################
2da2e3ebdSchin#                                                                      #
3da2e3ebdSchin#               This software is part of the ast package               #
4*3e14f97fSRoger A. Faulkner#          Copyright (c) 1982-2010 AT&T Intellectual Property          #
5da2e3ebdSchin#                      and is licensed under the                       #
6da2e3ebdSchin#                  Common Public License, Version 1.0                  #
77c2fbfb3SApril Chin#                    by AT&T Intellectual Property                     #
8da2e3ebdSchin#                                                                      #
9da2e3ebdSchin#                A copy of the License is available at                 #
10da2e3ebdSchin#            http://www.opensource.org/licenses/cpl1.0.txt             #
11da2e3ebdSchin#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12da2e3ebdSchin#                                                                      #
13da2e3ebdSchin#              Information and Software Systems Research               #
14da2e3ebdSchin#                            AT&T Research                             #
15da2e3ebdSchin#                           Florham Park NJ                            #
16da2e3ebdSchin#                                                                      #
17da2e3ebdSchin#                  David Korn <dgk@research.att.com>                   #
18da2e3ebdSchin#                                                                      #
19da2e3ebdSchin########################################################################
20da2e3ebdSchin: generate the ksh math builtin table
21da2e3ebdSchin: include math.tab
22da2e3ebdSchin
2334f9b3eeSRoland Mainz# @(#)math.sh (AT&T Research) 2009-08-18
24da2e3ebdSchin
25da2e3ebdSchincommand=$0
2634f9b3eeSRoland Mainziffeflags="-n -v"
27da2e3ebdSchiniffehdrs="math.h ieeefp.h"
28da2e3ebdSchiniffelibs="-lm"
29da2e3ebdSchintable=/dev/null
30da2e3ebdSchin
31da2e3ebdSchineval $1
32da2e3ebdSchinshift
33da2e3ebdSchintable=$1
34da2e3ebdSchin
35da2e3ebdSchinnames=
36da2e3ebdSchintests=
37da2e3ebdSchin
387c2fbfb3SApril Chin: check long double
397c2fbfb3SApril Chin
407c2fbfb3SApril Chineval `iffe $iffeflags -c "$cc" - typ long.double 2>&$stderr`
417c2fbfb3SApril Chin
4234f9b3eeSRoland Mainz: check ast_standards.h
4334f9b3eeSRoland Mainz
4434f9b3eeSRoland Mainzeval `iffe $iffeflags -F ast_standards.h -c "$cc" - tst use_ast_standards -lm 'note{' 'math.h needs ast_standards.h' '}end' 'link{' '#include <math.h>' '#ifndef isgreater' '#define isgreater(a,b) 0' '#endif' 'int main() { return isgreater(0.0,1.0); }' '}end'`
4534f9b3eeSRoland Mainzcase $_use_ast_standards in
4634f9b3eeSRoland Mainz1)	iffeflags="$iffeflags -F ast_standards.h" ;;
4734f9b3eeSRoland Mainzesac
4834f9b3eeSRoland Mainz
49da2e3ebdSchin: read the table
50da2e3ebdSchin
51da2e3ebdSchinexec < $table
52da2e3ebdSchinwhile	read type args name aka comment
53da2e3ebdSchindo	case $type in
54da2e3ebdSchin	[fi])	names="$names $name"
557c2fbfb3SApril Chin		tests="$tests,$name"
567c2fbfb3SApril Chin		case $_typ_long_double in
577c2fbfb3SApril Chin		1)	tests="$tests,${name}l" ;;
587c2fbfb3SApril Chin		esac
59da2e3ebdSchin		eval TYPE_$name=$type ARGS_$name=$args AKA_$name=$aka
60da2e3ebdSchin		;;
61da2e3ebdSchin	esac
62da2e3ebdSchindone
63da2e3ebdSchin
64da2e3ebdSchin: check the math library
65da2e3ebdSchin
667c2fbfb3SApril Chineval `iffe $iffeflags -c "$cc" - lib $tests $iffehdrs $iffelibs 2>&$stderr`
67da2e3ebdSchinlib=
68da2e3ebdSchinfor name in $names
69da2e3ebdSchindo	eval x='$'_lib_${name}l y='$'_lib_${name}
70da2e3ebdSchin	case $x in
71da2e3ebdSchin	1)	lib="$lib,${name}l" ;;
72da2e3ebdSchin	esac
73da2e3ebdSchin	case $y in
74da2e3ebdSchin	1)	case $x in
75da2e3ebdSchin		'')	lib="$lib,${name}" ;;
76da2e3ebdSchin		esac
77da2e3ebdSchin		;;
78da2e3ebdSchin	esac
79da2e3ebdSchindone
80da2e3ebdSchineval `iffe $iffeflags -c "$cc" - dat,npt,mac $lib $iffehdrs $iffelibs 2>&$stderr`
81da2e3ebdSchin
82da2e3ebdSchincat <<!
83da2e3ebdSchin#pragma prototyped
84da2e3ebdSchin
85da2e3ebdSchin/* : : generated by $command from $table : : */
86da2e3ebdSchin
87da2e3ebdSchintypedef Sfdouble_t (*Math_f)(Sfdouble_t,...);
88da2e3ebdSchin
89da2e3ebdSchin!
9034f9b3eeSRoland Mainzcase $_use_ast_standards in
9134f9b3eeSRoland Mainz1)	echo "#include <ast_standards.h>" ;;
9234f9b3eeSRoland Mainzesac
93da2e3ebdSchinecho "#include <math.h>"
94da2e3ebdSchincase $_hdr_ieeefp in
9534f9b3eeSRoland Mainz1)	echo "#include <ieeefp.h>" ;;
96da2e3ebdSchinesac
9734f9b3eeSRoland Mainzecho
98da2e3ebdSchin
99da2e3ebdSchin: generate the intercept functions and table entries
100da2e3ebdSchin
101da2e3ebdSchinnl='
102da2e3ebdSchin'
103da2e3ebdSchinht='	'
104da2e3ebdSchintab=
105da2e3ebdSchinfor name in $names
106da2e3ebdSchindo	eval x='$'_lib_${name}l y='$'_lib_${name} r='$'TYPE_${name} a='$'ARGS_${name} aka='$'AKA_${name}
107da2e3ebdSchin	case $x:$y in
108da2e3ebdSchin	1:*)	f=${name}l
109da2e3ebdSchin		t=Sfdouble_t
110da2e3ebdSchin		local=
111da2e3ebdSchin		;;
112da2e3ebdSchin	*:1)	f=${name}
113da2e3ebdSchin		t=double
114da2e3ebdSchin		local=$_typ_long_double
115da2e3ebdSchin		;;
11634f9b3eeSRoland Mainz	*)	case $aka in
11734f9b3eeSRoland Mainz		*=*)	f=${aka%%=*}
11834f9b3eeSRoland Mainz			v=${aka#*=}
11934f9b3eeSRoland Mainz			eval x='$'_lib_${f}l y='$'_lib_${f}
12034f9b3eeSRoland Mainz			case $x:$y in
12134f9b3eeSRoland Mainz			1:*)	f=${f}l
12234f9b3eeSRoland Mainz				;;
12334f9b3eeSRoland Mainz			*:1)	;;
124da2e3ebdSchin			*)	continue
125da2e3ebdSchin				;;
126da2e3ebdSchin			esac
12734f9b3eeSRoland Mainz			L=local_$name r=int R=1
12834f9b3eeSRoland Mainz			echo "#ifdef $v${nl}static $r $L(Sfdouble_t x) { return $f(x) == $v; }${nl}#endif"
12934f9b3eeSRoland Mainz			tab="$tab$nl#ifdef $v$nl$ht\"\\0${R}${a}${name}\",$ht(Math_f)${L},${nl}#endif"
13034f9b3eeSRoland Mainz			;;
13134f9b3eeSRoland Mainz		esac
13234f9b3eeSRoland Mainz		continue
13334f9b3eeSRoland Mainz		;;
13434f9b3eeSRoland Mainz	esac
135da2e3ebdSchin	eval n='$'_npt_$f m='$'_mac_$f d='$'_dat_$f
136da2e3ebdSchin	case $r in
137da2e3ebdSchin	i)	L=int r=int R=1 ;;
138da2e3ebdSchin	*)	L=Sfdouble_t r=$t R=0 ;;
139da2e3ebdSchin	esac
140da2e3ebdSchin	case $d:$m:$n in
141da2e3ebdSchin	1:*:*|*:1:*)
142da2e3ebdSchin		;;
143da2e3ebdSchin	*:*:1)	code="extern $r $f("
144da2e3ebdSchin		sep=
145da2e3ebdSchin		for p in 1 2 3 4 5 6 7
146da2e3ebdSchin		do	code="$code${sep}$t"
147da2e3ebdSchin			case $a in
148da2e3ebdSchin			$p)	break ;;
149da2e3ebdSchin			esac
150da2e3ebdSchin			sep=","
151da2e3ebdSchin		done
152da2e3ebdSchin		code="$code);"
153da2e3ebdSchin		echo "$code"
154da2e3ebdSchin		;;
155da2e3ebdSchin	esac
156da2e3ebdSchin	case $local:$m:$n:$d in
1577c2fbfb3SApril Chin	1:*:*:*|*:1:*:*|*:*:1:)
158da2e3ebdSchin		args=
159da2e3ebdSchin		code="static $L local_$f("
160da2e3ebdSchin		sep=
161da2e3ebdSchin		for p in 1 2 3 4 5 6 7 8 9
162da2e3ebdSchin		do	args="$args${sep}a$p"
163da2e3ebdSchin			code="$code${sep}Sfdouble_t a$p"
164da2e3ebdSchin			case $a in
165da2e3ebdSchin			$p)	break ;;
166da2e3ebdSchin			esac
167da2e3ebdSchin			sep=","
168da2e3ebdSchin		done
169da2e3ebdSchin		code="$code){return $f($args);}"
170da2e3ebdSchin		echo "$code"
171da2e3ebdSchin		f=local_$f
172da2e3ebdSchin		;;
173da2e3ebdSchin	esac
174da2e3ebdSchin	for x in $name $aka
175da2e3ebdSchin	do	tab="$tab$nl$ht\"\\0${R}${a}${x}\",$ht(Math_f)$f,"
176da2e3ebdSchin	done
177da2e3ebdSchindone
178da2e3ebdSchintab="$tab$nl$ht\"\",$ht$ht(Math_f)0"
179da2e3ebdSchin
180da2e3ebdSchincat <<!
181da2e3ebdSchin
182da2e3ebdSchin/*
183da2e3ebdSchin * first byte is two-digit octal number.  Last digit is number of args
184da2e3ebdSchin * first digit is 0 if return value is double, 1 for integer
185da2e3ebdSchin */
186da2e3ebdSchinconst struct mathtab shtab_math[] =
187da2e3ebdSchin{$tab
188da2e3ebdSchin};
189da2e3ebdSchin!
190