xref: /titanic_50/usr/src/lib/libshell/common/features/math.sh (revision e5816e352629470f540696fb7aa56c52d6719e67)
1########################################################################
2#                                                                      #
3#               This software is part of the ast package               #
4#          Copyright (c) 1982-2009 AT&T Intellectual Property          #
5#                      and is licensed under the                       #
6#                  Common Public License, Version 1.0                  #
7#                    by AT&T Intellectual Property                     #
8#                                                                      #
9#                A copy of the License is available at                 #
10#            http://www.opensource.org/licenses/cpl1.0.txt             #
11#         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         #
12#                                                                      #
13#              Information and Software Systems Research               #
14#                            AT&T Research                             #
15#                           Florham Park NJ                            #
16#                                                                      #
17#                  David Korn <dgk@research.att.com>                   #
18#                                                                      #
19########################################################################
20: generate the ksh math builtin table
21: include math.tab
22
23# @(#)math.sh (AT&T Research) 2009-08-18
24
25command=$0
26iffeflags="-n -v"
27iffehdrs="math.h ieeefp.h"
28iffelibs="-lm"
29table=/dev/null
30
31eval $1
32shift
33table=$1
34
35names=
36tests=
37
38: check long double
39
40eval `iffe $iffeflags -c "$cc" - typ long.double 2>&$stderr`
41
42: check ast_standards.h
43
44eval `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'`
45case $_use_ast_standards in
461)	iffeflags="$iffeflags -F ast_standards.h" ;;
47esac
48
49: read the table
50
51exec < $table
52while	read type args name aka comment
53do	case $type in
54	[fi])	names="$names $name"
55		tests="$tests,$name"
56		case $_typ_long_double in
57		1)	tests="$tests,${name}l" ;;
58		esac
59		eval TYPE_$name=$type ARGS_$name=$args AKA_$name=$aka
60		;;
61	esac
62done
63
64: check the math library
65
66eval `iffe $iffeflags -c "$cc" - lib $tests $iffehdrs $iffelibs 2>&$stderr`
67lib=
68for name in $names
69do	eval x='$'_lib_${name}l y='$'_lib_${name}
70	case $x in
71	1)	lib="$lib,${name}l" ;;
72	esac
73	case $y in
74	1)	case $x in
75		'')	lib="$lib,${name}" ;;
76		esac
77		;;
78	esac
79done
80eval `iffe $iffeflags -c "$cc" - dat,npt,mac $lib $iffehdrs $iffelibs 2>&$stderr`
81
82cat <<!
83#pragma prototyped
84
85/* : : generated by $command from $table : : */
86
87typedef Sfdouble_t (*Math_f)(Sfdouble_t,...);
88
89!
90case $_use_ast_standards in
911)	echo "#include <ast_standards.h>" ;;
92esac
93echo "#include <math.h>"
94case $_hdr_ieeefp in
951)	echo "#include <ieeefp.h>" ;;
96esac
97echo
98
99: generate the intercept functions and table entries
100
101nl='
102'
103ht='	'
104tab=
105for name in $names
106do	eval x='$'_lib_${name}l y='$'_lib_${name} r='$'TYPE_${name} a='$'ARGS_${name} aka='$'AKA_${name}
107	case $x:$y in
108	1:*)	f=${name}l
109		t=Sfdouble_t
110		local=
111		;;
112	*:1)	f=${name}
113		t=double
114		local=$_typ_long_double
115		;;
116	*)	case $aka in
117		*=*)	f=${aka%%=*}
118			v=${aka#*=}
119			eval x='$'_lib_${f}l y='$'_lib_${f}
120			case $x:$y in
121			1:*)	f=${f}l
122				;;
123			*:1)	;;
124			*)	continue
125				;;
126			esac
127			L=local_$name r=int R=1
128			echo "#ifdef $v${nl}static $r $L(Sfdouble_t x) { return $f(x) == $v; }${nl}#endif"
129			tab="$tab$nl#ifdef $v$nl$ht\"\\0${R}${a}${name}\",$ht(Math_f)${L},${nl}#endif"
130			;;
131		esac
132		continue
133		;;
134	esac
135	eval n='$'_npt_$f m='$'_mac_$f d='$'_dat_$f
136	case $r in
137	i)	L=int r=int R=1 ;;
138	*)	L=Sfdouble_t r=$t R=0 ;;
139	esac
140	case $d:$m:$n in
141	1:*:*|*:1:*)
142		;;
143	*:*:1)	code="extern $r $f("
144		sep=
145		for p in 1 2 3 4 5 6 7
146		do	code="$code${sep}$t"
147			case $a in
148			$p)	break ;;
149			esac
150			sep=","
151		done
152		code="$code);"
153		echo "$code"
154		;;
155	esac
156	case $local:$m:$n:$d in
157	1:*:*:*|*:1:*:*|*:*:1:)
158		args=
159		code="static $L local_$f("
160		sep=
161		for p in 1 2 3 4 5 6 7 8 9
162		do	args="$args${sep}a$p"
163			code="$code${sep}Sfdouble_t a$p"
164			case $a in
165			$p)	break ;;
166			esac
167			sep=","
168		done
169		code="$code){return $f($args);}"
170		echo "$code"
171		f=local_$f
172		;;
173	esac
174	for x in $name $aka
175	do	tab="$tab$nl$ht\"\\0${R}${a}${x}\",$ht(Math_f)$f,"
176	done
177done
178tab="$tab$nl$ht\"\",$ht$ht(Math_f)0"
179
180cat <<!
181
182/*
183 * first byte is two-digit octal number.  Last digit is number of args
184 * first digit is 0 if return value is double, 1 for integer
185 */
186const struct mathtab shtab_math[] =
187{$tab
188};
189!
190