Friday, January 27, 2023

传习录

传习录

来书又有云:“人情机诈百出,御之以不疑,往往为所欺;觉则自入于逆、億。夫逆诈,即诈也;億不信,即非信也;为人欺,又非觉也。不逆、不億而常先觉,其惟良知莹彻乎(想做到不诈且先觉他人之诈,除非良知莹彻)?然而出入毫忽之间,背觉(未能先觉)合诈者多矣。”

“不逆不億而先觉”,此孔子因当时人专以逆诈、億不信为心,而自陷于诈与不信,又有不逆、不億者,然不知致良知之功,而往往又为人所欺诈,故有是言,非教人以是存心而专欲先觉人之诈与不信也(孔子说的先觉是教人致良知之功,而非刻意去防人)。以是存心,即是后世猜忌险薄者之事;而只此一念,已不可与入尧舜之道矣。不逆、不億而为人所欺者,尚亦不失为善,但不如能致其良知而自然先觉者之尤为贤耳。崇一谓“其惟良知莹彻”者,盖已得其旨矣。然亦颖悟所及,恐未实际也。盖良知之在人心,亘万古、塞宇宙而无不同。不虑而知,“恒易以知险”;不学而能,“恒简以知阻”。“先天而天不违,天且不违,而况于人乎?况于鬼神乎?”夫谓背觉合诈者,是虽不逆人而或未能无自欺也,虽不億人而或未能果自信也,是或常有求先觉之心,而未能常自觉也。常有求先觉之心,即已流于逆、億而足以自蔽其良知矣,此背觉合诈之所以未免也(刻意求先觉,已流于逆、億而足以自蔽其良知,这就是毫忽之间背觉合诈的原因)。君子学以为己,未尝虞人之欺己也,恒不自欺其良知而已;未尝虞人之不信己也,恒自信其良知而已;未尝求先觉人之诈与不信也,恒务自觉其良知而已。是故不欺则良知无所伪而诚,诚则明矣;自信则良知无所惑而明,明则诚矣。明、诚相生,是故良知常觉、常照。常觉、常照,则如明镜之悬,而物之来者自不能遁其妍媸矣。何者?不欺而诚,则无所容其欺,苟有欺焉而觉矣;自信而明,则无所容其不信,苟不信焉而觉矣。是谓易以知险、简以知阻,子思所谓“至诚如神”、“可以前知”者也。然子思谓“如神”、谓“可以前知”,犹二而言之,是盖推言思诚者之功效,是犹为不能先觉者说也。若就至诚而言,则至诚之妙用即谓之“神”,不必言“如神”;至诚则“无知而无不知”,不必言“可以前知”矣。

崇一:   想做到不诈且先觉他人之诈,除非良知莹彻;但是毫忽之间,很多人反而背觉(未能先觉)合诈了。
阳明:   孔子说的先觉是教人致良知之功,而非刻意去防人
阳明:   刻意求先觉,已流于逆、億而足以自蔽其良知,这就是毫忽之间诈的原因
阳明:   不欺则良知无所伪而诚,诚则明矣;自信则良知无所惑而明,明则诚矣。明、诚相生,是故良知常觉、常照。
阳明:   自信而明,则无所容其不信,苟不信焉而觉矣。

Saturday, January 21, 2023

Saturated Q15 and Q31 arithmetic

Saturated Q15 and Q31 arithmetic

A 32-bit signed value can be treated as having a binary point immediately after its sign bit. This is equivalent to dividing its signed integer value by 231, so that it can now represent numbers from –1 to (1 – 2–31). When a 32-bit value is used to represent a fractional number in this fashion, it is known as a Q31 number.

Saturated additions, subtractions, and doublings can be performed on Q31 numbers using the same instructions as are used for saturated integer arithmetic, since everything is simply scaled down by a factor of 2–31.

If two Q15 numbers are multiplied together as integers, the resulting integer needs to be scaled down by a factor of 2–15 × 2–15 == 2–30. For example, multiplying the Q15 number 0x8000 (representing –1) by itself using an integer multiplication instruction yields the value 0x40000000, which is 230 times the desired result of +1.

This means that the result of the integer multiplication instruction is not quite in Q31 form. To get it into Q31 form, it must be doubled, so that the required scaling factor becomes 2–31. Furthermore, it is possible that the doubling will cause integer overflow, so the result should in fact be doubled with saturation. In particular, the result 0x40000000 from the multiplication of 0x8000 by itself should be doubled with saturation to produce 0x7FFFFFFF (the closest possible Q31 number to the correct mathematical result of –1 × –1 == +1). If it were doubled without saturation, it would instead produce 0x80000000, which is the Q31 representation of –1.

To implement a saturated Q15 × Q15 --> Q31 multiplication, therefore, an integer multiply instruction should be followed by a saturated integer doubling. The latter can be performed by a QADD instruction adding the multiply result to itself.

Friday, January 6, 2023

Global Environment Record

Global Environment Record

A global Environment Record is used to represent the outer most scope that is shared by all of the ECMAScript Script elements that are processed in a common realm. A global Environment Record provides the bindings for built-in globals, properties of the global object(FunctionDeclaration or VariableStatement), and for all top-level declarations(the let and const declarations) that occur within a Script.

A global Environment Record is logically a single record but it is specified as a composite encapsulating of object Environment Record([[ObjectRecord]]) and a declarative Environment Record([[DeclarativeRecord]]).

The object Environment Record has as its base object the global object of the associated Realm Record. This global object is the value returned by the global Environment Record's GetThisBinding concrete method:

 Let envRec be the global Environment Record for which the method was invoked.
 Return envRec.[[GlobalThisValue]]
 zxxu: according to ecma, Regular object Environment Records do not provide a this binding, so global Environment Record has a  [[GlobalThisValue]] field

The object Environment Record contains the bindings for all built-in globals and all bindings introduced by a FunctionDeclaration or VariableStatement contained in global code. The bindings for all other ECMAScript declarations(the let and const declarations) in global code are contained in the declarative Environment Record component.

Properties may exist upon a global object that were directly created rather than being declared using a var or function declaration. so global Environment Record also has a field [[VarNames]] for string names bound by FunctionDeclaration or VariableDeclaration.

Tuesday, January 3, 2023

GetValue abstract operation

GetValue abstract operation GetValue ( V )
  1. ReturnIfAbrupt(V).
  2. If Type(V) is not Reference, return V.
  3. If IsUnresolvableReference(V) is true, throw a ReferenceError exception.
  4. Let base be GetBase(V).
  5. If HasPrimitiveBase(V) is true, Set base to !ToObject(base)
  6. If IsPropertyReference(V) is true, Return ? base.[[Get]](GetReferencedName(V), Receiver = GetThisValue(V)).
  7. Return ? base.GetBindingValue(GetReferencedName(V), IsStrictReference(V))

note If IsSuperReference(V), GetThisValue(V) returns the thisValue component of the reference V; else returns GetBase(V).

abstract operation O.[[Get]](P, Receiver) works as:

  • Let desc be ? O.[[GetOwnProperty]](P).
  • If desc is undefined, Return ?O.[[Prototype]].[[Get]](P, Receiver).
  • If IsDataDescriptor(desc) is true, return desc.[[Value]].
  • Return ? Call(desc.[[Get]], Receiver)// Receiver is only used by AccessorDescriptor

above all

  1. super is parent and super.super is grandpa
  2. desc.[[Get]] can accept base, base.super, base.super.super... as Receiver
  3. refer to new.target for more info regarding the super keyword

ToString.FromNum

ToString.FromNum

abstract operation

  1. If m is NaN, return the String "NaN".
  2. If m is +0 or -0, return the String "0".
  3. If m is less than zero, return the String concatenation of the String "-" and ToString(-m).
  4. If m is infinity, return the String "Infinity".
  5. let n, k, and s be integers such that k >= 1, s in [10^(k-1), 10^k), the Number value for s * 10^(n-k) is m. Note that k is the number of digits in the decimal representation of s, that s is not divisible by 10.
  6. if both k and n are no greater than 21:
    • if k is no greater than n, return the string-concatenation of:
      • the k digits of the decimal representation of s
      • n−k occurrences of the character 0
    • if n is greater than 0, return the string-concatenation of:
      • the code units of the most significant n digits of the decimal representation of s
      • the code unit 0x002E (FULL STOP)
      • the code units of the remaining k - n digits of the decimal representation of s
  7. ...

example 0.12300

k=3, s=123, n=0

example 123.4

n=k1=3, k2=1, s=123.4

example 0.0423

k=3, s=423, n=-1 ==> k1=n=-1,pow(10,k1-1)=0.04

example 42300

k=3, s=423, n=5 ==> pow(10, n-1) = 4